简体   繁体   中英

Converting gray scale image to BW image

I want to convert a gray scale image (uint16) to a black and white image.

[level, ] = graythresh(I);
I_bw      = im2bw(I, level);

Below the image I : 在此处输入图片说明

I don't understand how is possible that image I_bw result as follows: 在此处输入图片说明

Note that level is equal to 0 after calling graythresh(I) .

EDIT: I have uploaded the .mat file containing the original image. file

The reason is: level = 0

load Image.mat

I = z;
figure;imshow(I, []);

[level, ] = graythresh(I); %level = 0
I_bw      = im2bw(I, level);
figure;imshow(I_bw);impixelinfo

The following code works:
Convert I to double , and normalize it to range [0, 1].

load Image.mat

I = z;
figure;imshow(I, []);

I = double(I)/double(max(I(:))); %Convert to double, and divide by maximum value - set range to [0, 1].

[level, ] = graythresh(I);

I_bw      = im2bw(I, level);
figure;imshow(I_bw);impixelinfo

Result:
在此处输入图片说明

The following code works as well:

load Image.mat

I = z;
figure;imshow(I, []);

I = double(I)/double(max(I(:))); %Convert to double, and divide by maximum value - set range to [0, 1].
I = uint16(I*2^16-1); %Expand range to [0, 2^16-1] and convert to uint16.

[level, ] = graythresh(I);

I_bw      = im2bw(I, level);
figure;imshow(I_bw);impixelinfo

Understanding why level = 0, in the original code requires farther investigation...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM