【matlab_郭彦甫课程答案】

@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:afor j= 1:bif BW(i,j)> level;BW(i,j)=1;elseBW(i,j)= 0;endend
end
subplot(1,2,1);imshow(I);
subplot(1,2,2);imshow(BW);

以上百分号后为注释内容,本人使用matlab2021b可以直接跑,其他版本matlab可以尝试copy以上代码跑一跑。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部