Borrar filtros
Borrar filtros

How to continuously update the variable when input given by user?

2 visualizaciones (últimos 30 días)
Usman
Usman el 27 de Nov. de 2015
Editada: Geoff Hayes el 29 de Nov. de 2015
Scenario is I have 5 images in folder. I have to read image1,calculate its pixels calculation then calculate the ratio of black pixels and stored it in variable "R", then MATLAB ask user input "Press enter", on user input execution read image2,again do same calculation and update the ratio of 2nd image in "R".(eg: R=[2 4 5 6 9])and do same upto 5th image. I mean it continuously update on user request . I have tried to do but "R" variable only stores and shows the value of last image. Can anyone have idea how to do it?
% Specify the folder where the files live.
clear all
myFolder = 'D:\MATLAB Files\MATLAB\New folder\images';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
thres_level=graythresh(imageArray); % find the threshold level of image
bw=im2bw(imageArray,thres_level); % converts an image into binary
[L,num] = bwlabel( bw ); %returns num, the number of connected objects found in BW.
counts = sum(bsxfun(@eq,L(:),1:num));%do element by element binary operation equal to L and num
B1 = bsxfun(@eq,L,permute(find(counts>thres_level),[1 3 2]));
NewImg = sum(B1,3)>0;
imshow(NewImg)
totnumpix=numel(NewImg); % calculate total no of pixels in image
nwhite_open=sum(NewImg(:)); % calculate the black pixels in image;
nblack_open=totnumpix-nwhite_open; %calculate white pixels in image;
R(k)=(nblack_open/(nblack_open+nwhite_open))*100;
input('enter');
end

Respuestas (1)

Geoff Hayes
Geoff Hayes el 27 de Nov. de 2015
Usman - initialize R as a cell array and then update each element of that cell array on each iteration of the for loop. Something like
myFolder = 'D:\MATLAB Files\MATLAB\New folder\images';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
R = cell(1,length(theFiles);
for k=1:length(theFiles)
% etc.
R{k} =(nblack_open/(nblack_open+nwhite_open))*100;
input('enter');
end
The above should allow you to manage all updated/processed images.
  2 comentarios
Usman
Usman el 29 de Nov. de 2015
can you please update your code, according to my code. because when I am trying to done with your code, there is no effect on results of R. still it update 5 same values in R.?
Geoff Hayes
Geoff Hayes el 29 de Nov. de 2015
Editada: Geoff Hayes el 29 de Nov. de 2015
Usman - please attach your updated code so that I can see how you have implemented the above cell array. Also, please clarify what you mean by still it update 5 same values in R. Does R have five elements (since there are presumably five files)? And is each of the five the same? If you step through the code using the Debugger, does it show that your calculations for
(nblack_open/(nblack_open+nwhite_open))*100;
are correct?

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by