我想将灰度图像(uint16)转换为黑白图像。
[level, ] = graythresh(I);
I_bw = im2bw(I, level);
请注意,在调用 graythresh(I)后 level 等于0。
编辑:我上传了包含原始图片的.mat文件。 file
答案 0 :(得分:1)
原因是: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
以下代码有效:
将I
转换为double
,并将其标准化为[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
以下代码也适用:
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
理解为什么level = 0,在原始代码中需要进一步调查......