Q:
How does MATLAB binarize a picture , and turn the black in the picture into white after binarization , while the original white remains unchanged, and the black outline between black and white remains unchanged. A:
- Use MATLAB
imbinarize 函数
to binarize images. Sets pixel values below the specified threshold to 0 and other pixel values to 1. - Then you can use the logical negation operator ~ (wavy line) to turn the binarized black into white, and at the same time turn the original white into black.
- Finally, the outline is adjusted according to the characteristics of black and white after binarization.
% 读入图片
img = imread('test.jpg');
% 将图片二值化,设定阈值为 0.5
bw = imbinarize(img, 0.5);
% 将黑色变成白色,白色变成黑色
bw = ~bw;
% 调整轮廓,将二值化后的黑色与白色调整为特定数值,同时保留原本的白色
bw = bw * 255;
% 显示结果
imshow(bw);