Why intensity of the image has changed, after masking out image area (using binary mask)?

Hello,
I have used following code:
new_im=im; % Size (80x84) Bytes(13440) Class (uint16)
bw_mask; % Size (80x84) Bytes(53760) Class (double)
new_im(~bw_mask) = 0; %
After masking out I have following results (zoomed).
As you can see the intensity of the images are different, could anyone tell me please, what I have done wrong?
MATLAB version [2015b]

 Accepted Answer

Actually it is just about displaying the image, if you check the values of im(60,65) and new_im(60,65) for im and new_im matrices you will see these two values are equal, but when Matlab is displaying a double or uint16 image, it maps each range of values to a specific value probably by using the histogram of the image. So for different images, a specific value can map to different values when Matlab wants to display it. But it’s not the case with uin8 images.

6 Comments

It does not use a histogram.
If you use image(new_im) on uint8 then it is going to display with 0 mapped to the lowest color in the color table and 255 mapped to the highest color in the color table. If you use imagesc(new_im) or image(new_im,[]) then for uint8 it is going to display with the smallest value in the image (which would be 0 in this case because of the assignment of 0) mapped to the lowest color and the largest value actually used in the image mapped to the highest color in the color table.
Yes you are right Walter! It does not use the histogram or any other useful thing, is just uses a linear mapping, and that's stupid! Because only one unwanted white patch on a uint16 image can lead to a completely black output(including that white patch). I've never let Matlab do that for me. Thank you.
@Ivan Shorokhov: So this effect is because the minimum and/or maximum of your images after and before masking are different. But still your matrices have same values for those pixels.
Hamoon, you probably use imadjust() and stretchlim() rather than [] in imshow(). That will let you use the histogram to go between x% and 100-x% so that a single outlier won't mess up the image.
@Image Analyst: Actually I don't use imadjust, I use the histogram of the image to split more crowded areas of the histogram to smaller ranges and deserted areas to larger ranges, then assign values in each range to a specific value between 0 to 255, so I can better monitor the changes. Actually it was better for my proposes, maybe not so useful for others', as it has a higher computational cost.
@Hamoon, @Image Analyst, just woke up, and found so many great answers thank you so much!
So as I understood, there is a problem displaying the image, and you recommend me to use following code:
figure, imshow(im_new,[min(im(:)) max(im(:))]);
or
new_im = imadjust(im,stretchlim(im),[]);imshow(new_im);
or should I convert both images to uint8:
new_im=uint8(im);
The images you have are already uint8. Note that my reply was about using image() and imagesc() not about imshow()
If you using your first possibility, where you show im_new according to the range of im, then that would use the same brightness for both, which is good. But it would also imply that two different original images might not use the same brightness for the same stored value, as you are adjusting each according to its brightness range.

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!