how to make code global

1 Ansicht (letzte 30 Tage)
Lalit Patil
Lalit Patil am 21 Nov. 2012
clear all;
clc;
RGB = imread('Image100.jpg');
GRAY = rgb2gray(RGB);
threshold = graythresh(GRAY);
originalImage = im2bw(GRAY, threshold);
i = bwareaopen(originalImage,350);
imshow(i)
m = max(max(i));
[r c] = find(i == m);
fid = fopen('lalit1.txt','wt');
for j=1:length(r)
fprintf(fid,'%f %f\n',r(j),c(j));
end
fclose(fid);
data = textread('lalit1.txt');
r = unique(data);
for i=r',
c = data(data(:,1)==i,2);
z(i,1) = mean([min(c) max(c)]);
end
fid = fopen('lalit2.txt','wt');
for j=1:length(r)
fprintf(fid,'%f %f\n',r(j),z(j));
end
fclose(fid);
B = 1:480;
B = B';
missingvalues = setdiff(B,r);
fid = fopen('lalit3.txt','wt');
for j=1:length(missingvalues)
q(j) = 0;
fprintf(fid,'%f %f\n',missingvalues(j),q(j));
end
fclose(fid);
outfid = fopen('all_Image.txt', 'wt');
for j = 2 : 3
filename = ['lalit' num2str(j),'.txt'];
fwrite(outfid, fileread(filename));
end
fclose(outfid);
This is the code i have written for one image.. I want to use this code for 100 images named Image1,Image2,Image3... So, i used global for this and i completed half as below.. and it works..
clear all;
clc;
for k = 1:200
global filename
filename = ['Image' num2str(k),'.jpg' ];
RGB = imread(filename);
GRAY = rgb2gray(RGB);
threshold = graythresh(GRAY);
originalImage = im2bw(GRAY, threshold);
i = bwareaopen(originalImage,350);
m = max(max(i));
[r c] = find(i == m);
global filename
filename = ['Image' num2str(k),'.txt' ];
fid = fopen(filename,'wt');
for j=1:length(r)
fprintf(fid,'%f %f\n',r(j),c(j));
end fclose(fid);
global filename
filename = ['Image' num2str(k),'.txt' ];
data = textread(filename);
r = unique(data);
for i=r',
c = data(data(:,1)==i,2);
z(i,1) = mean([min(c) max(c)]);
end
global filename
filename = ['data' num2str(k),'.txt' ];
fid = fopen(filename,'wt');
for j=1:length(r)
fprintf(fid,'%f %f\n',r(j),z(j));
end
But i can't
understand next what to do.. I tried but can't do.. So,please help me...!
  1 Kommentar
Jan
Jan am 21 Nov. 2012
Please format your code properly. Currently it is not readable.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Pedro Villena
Pedro Villena am 21 Nov. 2012
Bearbeitet: Pedro Villena am 22 Nov. 2012
>>arrayfun(@(i) myFunction(i),1:100);
myFunction.m
function [] = myFunction(imageNumber)
%RGB = imread('Image100.jpg');
RGB = imread(sprintf('Image%d.jpg',imageNumber));
GRAY = rgb2gray(RGB);
threshold = graythresh(GRAY);
originalImage = im2bw(GRAY, threshold);
i = bwareaopen(originalImage,350);
%imshow(i),
m = max(max(i));
[r c] = find(i == m);
%fid = fopen('lalit1.txt','wt');
fid = fopen(sprintf('Image%d_lalit1.txt',imageNumber),'wt');
for j=1:length(r),
fprintf(fid,'%f %f\n',r(j),c(j));
end
fclose(fid);
%data = textread('lalit1.txt');
data = textread(sprintf('Image%d_lalit1.txt',imageNumber));
r = unique(data);
%z = zeros(size(r));
for i=r',
c = data(data(:,1)==i,2);
z(i,1) = mean([min(c) max(c)]);
end
%fid = fopen('lalit2.txt','wt');
fid = fopen(sprintf('Image%d_lalit2.txt',imageNumber),'wt');
for j=1:length(r),
fprintf(fid,'%f %f\n',r(j),z(j));
end
fclose(fid);
B = 1:480;
B = B';
missingvalues = setdiff(B,r);
%fid = fopen('lalit3.txt','wt');
fid = fopen(sprintf('Image%d_lalit3.txt',imageNumber),'wt');
%q = zeros(1,length(missingvalues));
for j=1:length(missingvalues),
q(j) = 0;
fprintf(fid,'%f %f\n',missingvalues(j),q(j));
end
fclose(fid);
%outfid = fopen('all_Image.txt', 'wt');
outfid = fopen(sprintf('Image%d_all.txt',imageNumber), 'wt');
for j = 2:3,
%filename = ['lalit' num2str(j),'.txt'];
filename = sprintf('Image%d_lalit%d.txt',imageNumber,j);
fwrite(outfid, fileread(filename));
end
fclose(outfid);
  1 Kommentar
Lalit Patil
Lalit Patil am 21 Nov. 2012
Thank you very much.. I never thinked about to make a function.. This helps me a lot, and gives me an idea to use code as a function..!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 21 Nov. 2012
I do not see a reason to use global variables here. But it is hard to see anything in the code without a proper formatting.
Which detail of your code does not work? What happens when you run the code?
It is a good idea to omit all parts of the code, which are working correctly, when you post it in the forum. Then we can see much faster, where the modifications are needed.
  1 Kommentar
Lalit Patil
Lalit Patil am 21 Nov. 2012
Bearbeitet: Lalit Patil am 21 Nov. 2012
Actually the above code without global is only for one image.. If i want to use the same code for 10 images then how to use loops in that code.. I think global can do this and so that i write another code.. But i can write only half code and i want help to write remaining code and stucked at half of this code..
I am attaching 10 images here... http://speedy.sh/7Zc2P/Image.zip

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu App Building finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by