Question

I need to Binarize an image in matlab with a static threshold of 10% of mean intensity. I find mean intensity using mean2(Image) and this returns a mean let say 15.10 in one of the image. Thus my mean threshold is 1.51.im2bw(image,level) takes threshold between 0 to 1. How to binarize my image in this case in matlab?

Was it helpful?

Solution 2

You can binarize the image with a simple logical statement. For completeness, I've added the threshold determination as well.

threshold = mean(Image(:));

binaryMask = Image > 0.1 * threshold;

OTHER TIPS

1) you can first convert the original image to double format using im2double(). Then all the pixels values will be between 0 and 1. Then you can use im2bw(im,level).

2) If you do not want to convert the image to double, then you can do it in this way. Let's say the threshold is 10 % of the the mean value, say threshold = 1.51. Let's denote the image you have is im. Then im(im<threshold) = 0; im(im>=threshold)=1. After these two operations, im will become a binary image.

You need to normalize the result of the mean vs the max intensity of the image if you want to use im2bw (the other solutions mentioned are of course correct and work):

ImageN=Image./max(Image(:))
t = mean2(ImageN) * 0.1 % Find your threshold value 
im2bw(Image,t)

Let's say your image is a matrix img, you can do the following:

t = mean2(img) * 0.1 % Find your threshold value
img(img < t) = 0 % Set everything below the treshold value to 0
img(img ̃= 0) = 1 % Set the rest to 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top