How to apply Otsu method to a stacked images

4 次查看(过去 30 天)
Dear all,
I have 21 RGB microscopic images located in one folder. I want to apply the Otsu thresholding to each image in the folder (each image has different Otsu thresholding value).
How to make a "for loop" applied to the 21 images?
I tried this code but it finds the threshold value of the first image and applies it to all 21 images.
Any idea?
Meshoo
*****************************************************************
files = dir('*.tif');
for k = 1:(numel(files));
thresh = [];
image = imread(files(k).name);
thresh = graythresh(image);
newImage = im2bw(image, thresh);
imwrite(newImage, ['NewImage\', files(k).name,]);
end

采纳的回答

Image Analyst
Image Analyst 2012-10-16
编辑:Image Analyst 2012-10-16
Why are you applying monochrome thresholding to RGB images? You need to extract a color channel first, or use rgb2gray().
Assuming they're monochrome, I don't see any reason why this would not work. Take the semicolon off this line:
thresh = graythresh(image)
and see what values get written to the command window.
Also, don't use image as the name of your variable because you'll blow away the built-in image() function.
  15 个评论
Meshooo
Meshooo 2012-12-20
编辑:Meshooo 2012-12-20
Hi again and thank you for your reply. I am trying by myself and things are going well. However, I would like to ask you how to make two For-loops inside each other?
For example I want to load the whole 21 intensity images (Intensity) from the folder and find their intensity value in 6 points which I already know them.
I wrote the following program which works very well but I want to use a for loop because the number of points is not always 6.
Could you please check this code to make a for loop
for k = 1:(numel(filelist));
Intensity = imread(filelist(k).name);
P_Centro_1 = []; %1st centroid
P_Centro_1 = impixel(Intensity,(center(1,1)),(center(1,2)));
P_Centro_1 = P_Centro_1(:,1); %Intensity value
A1(:,k)= P_Centro_1;
P_Centro_2 = []; %2nd centroid
P_Centro_2 = impixel(Intensity,(center(2,1)),(center(2,2)));
P_Centro_2 = P_Centro_2(:,1);
A2(:,k)= P_Centro_2;
P_Centro_3 = []; %3rd centroid
P_Centro_3 = impixel(Intensity,(center(3,1)),(center(3,2)));
P_Centro_3 = P_Centro_3(:,1);
A3(:,k)= P_Centro_3;
P_Centro_4 = []; %4th centroid
P_Centro_4 = impixel(Intensity,(center(4,1)),(center(4,2)));
P_Centro_4 = P_Centro_4(:,1);
A4(:,k)= P_Centro_4;
P_Centro_5 = []; %5th centroid
P_Centro_5 = impixel(Intensity,(center(5,1)),(center(5,2)));
P_Centro_5 = P_Centro_5(:,1);
A5(:,k)= P_Centro_5;
P_Centro_6 = []; %6th centroid
P_Centro_6 = impixel(Intensity,(center(6,1)),(center(6,2)));
P_Centro_6 = P_Centro_6(:,1);
A6(:,k)= P_Centro_6;
end
hold on
plot(A1,'b-')
plot(A2,'c-')
plot(A3,'g-')
plot(A4,'y-')
plot(A5,'r-')
plot(A6,'m-')
hold off
Thank you very much for your time.
Meshooo
Meshooo 2012-12-20
编辑:Meshooo 2012-12-20
The following program seems to be the answer, but how to store the results from every loop in A1?
for k = 1:21
Intensity = imread(filelist(k).name);
for c = 1:6
P_Centro_1 = []; % list of centroids
P_Centro_1 = impixel(Intensity,(center(c,1)),(center(c,2)));
P_Centro_1 = P_Centro_1(:,1); %Intensity value
A1(:,k)= P_Centro_1;
end
end

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by