Clear Filters
Clear Filters

How can I use Otsuthresh correctly to convert an image to binary?

5 views (last 30 days)
I have been trying to convert an image to binary by separating out the image into RGB, plotting a histogram for each colour then using Ostuthresh to get a value to use in im2bw. My threshold limits have always been wrong and I'm not sure why. When I plot the histogram and use BinLimits I get a lower value of 118 and upper of 205. I know that the limit needs to be near 0.7 however I cannot seem to achieve this. Although I am practicing this on one image the aim to to make it general.
bac_1 = imread('multibac.png');
r1 = bac_1(:,:,1);
g1 = bac_1(:,:,2);
b1 = bac_1(:,:,3);
red_testogram = histogram(r1);
if red_testogram.BinWidth ==1 %this loop decides how to adjust the number of Bins to make BinWidth equal to 1
new_red_binnumber = red_testogram.Numbins;
else
new_red_binnumber = (red_testogram.NumBins * red_testogram.BinWidth); %chooses the bin width
end
r_imogram = histogram(r1, new_red_binnumber)
avg = mean(r1(:));
stdev = std(double(r1(:)));
lower = r_imogram.BinLimits(1);
upper = r_imogram.BinLimits(2);
t = otsuthresh(histcounts(r1,lower:upper));
bwr = im2bw(r1,t);
imshow(bwr)

Accepted Answer

Thorsten
Thorsten on 19 Jul 2016
Edited: Thorsten on 19 Jul 2016
I your aim is to separate the bacteria from the background, I think you cannot do this with a simple thresholding, because the interior of the bacteria have about the same gray level as the background.
  2 Comments
Nathan Costin
Nathan Costin on 19 Jul 2016
That is what I'm aiming to do however, I need to be able to count the bacteria and plot their centers later on (I have already written this code) so resulting image needs to be separate with no parts touching, I've used strel elements to do this in the past but I would like to automate the process
Nathan Costin
Nathan Costin on 19 Jul 2016
I'm trying to calculate the values of lower and upper correectly

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!