MATLAB: How to apply Otsu method to a stacked images

Image Processing Toolboxotsu stackedthresholding

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

Best Answer

  • 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.