how to make code global
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Réponse acceptée
Pedro Villena
le 21 Nov 2012
Modifié(e) : Pedro Villena
le 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);
Plus de réponses (1)
Jan
le 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 commentaire
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!