@matlab郭彦甫第九课p5,写一个程序将大米图片变成二进制图片,不使用im2bw();尝试不同的阈值。
PRACTICE: write a program to convert the image rice.png into a binary image using a threshold
Do not use im2bw();不使用内置函数im2bw()
Try different threshold values to see if you program works%已经解决
clear all, close all
I = imread('rice.png');
BW = imbinarize(I);%将灰阶图像转换imbinarize 使用 Otsu 的方法,该方法选择阈值以最小化阈值化的黑白像素的类内方差
level = 0.3 %(user-defined level,可自定义)
[a,b]=size(I)
for i=1:a
for j= 1:b
if BW(i,j)> level;
BW(i,j)=1;
else
BW(i,j)= 0;
end
end
end
subplot(1,2,1);imshow(I);
subplot(1,2,2);imshow(BW);
以上百分号后为注释内容,本人使用matlab2021b可以直接跑,其他版本matlab可以尝试copy以上代码跑一跑。