How can I run the same code for all my images?

2 views (last 30 days)
Ge
Ge on 23 Nov 2014
Commented: Image Analyst on 25 Nov 2014
Now I have my codes to run for a single image, but I have more than 1000 images.
The problem is how can I write a code to make all the images run the codes I wrote and get more than 1000 results.
Thank you very much. The codes below is what i have done and it can be run for a single image, and i attached my original and result image.
% read and shows the image
X= imread('1.tif');
imshow(X)
% segment image into three levels using two thresholds
% calculate two threshold levels
thresh = multithresh(X,2);
% Segment the image into three levels using imquantize
seg_X = imquantize(X,thresh);
% Convert segmented image into color image using label2rgb and display it
RGB = label2rgb(seg_X);
imshow(RGB)
%low pass filter the image to make it blurred
h = ones(5,5) / 25;
a = imfilter(RGB,h);
imshow(a)
% Convert the image to black and white
I = rgb2gray(a);
threshold = graythresh(I);
bw = im2bw(I,threshold);
imshow(bw)
% remove all object containing fewer than 5000 pixels
b = bwareaopen(bw,5000);
imshow(b)
% remove all object containing fewer than 1000 pixels
e = bwareaopen(~b,1000);
imshow(e)
% filter the extra part except the droplet
se = strel('disk',14);
f = imopen(e,se);
imshow(f,[])
%calculate the max width and max height of the droplet
%max width is the length of the droplet
%max height is the inner diameter of the tube
[w,h]=size(~f);
w_max=0;h_max=0;
for k=1:w
mx=h-sum(~f(k,:));
w_max=max([w_max mx]);
end
for k=1:h
mx=w-sum(~f(:,k));
h_max=max([h_max mx]);
end
disp(' width height');
disp([w_max h_max]);
x = (w_max*100)/h_max;
disp(' The length of droplet')
disp( x );
  4 Comments
Geoff Hayes
Geoff Hayes on 25 Nov 2014
See the answer from Image Analyst on how to read/process a sequence of files. You can adapt that for your code. And since you are recording the length of the droplets, just define an array outside of your loop (that which will read the files) and populate that array on each iteration
dropletLengths = zeros(1000,1);
for k=1:10000
% load image
% process image (code from above)
% save droplet length
dropletLengths(k) = x;
end
% now plot the droplet lengths
Image Analyst
Image Analyst on 25 Nov 2014
And while you're at it, try to pick descriptive variable names, like Geoff and I do. It's hard to follow your code when you have an alphabet soup of one and two letter variable names - you don't instantly know what they are or what they mean or represent.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 23 Nov 2014

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!