Из документации im2bw (выделено мое):
The input image IMG can either be a grayscale or RGB image. <b>In the
later case, IMG is first converted to grayscale with 'rgb2gray'</b>.
Input can also be an indexed image X in which case the colormap
CMAP needs to be specified.
The value of THRESHOLD should be in the range [0,1] independently
of the class of IMG. <b>Values from other classes can be converted to
the correct value with 'im2double'</b>:
bw = im2bw (img_of_class_uint8, <b>im2double (thresh_of_uint8_class)</b>);
Изображения в градациях серого могут иметь пиксельные значения класса double в диапазоне [0,1] или значения класса uint8 в диапазоне [0,255].
Функция rgb2gray
возвращает первый тип (т. Е. Значения в диапазоне [0,1]).
Следовательно, ваш в этом диапазоне необходимо определить порог.
Как видно из документации, приведенной выше, вы можете преобразовать свой порог "127" uint8 в двойной, используя функцию im2double
.
Другими словами , ваш код, вероятно, должен выглядеть так:
MO = imread('test.jpg');
M = rgb2gray (MO); % Note this isn't really necessary, im2bw does it for you
M3 = im2bw(M, im2double( uint8(127) ) ); % or simply im2bw(M, 0.5)
imshow(M3);