学习笔记_Matlab图像处理

最近在刷b站的matlab教程 贴一下图像处理2的小作业。
简单但比较inspiring。


数米粒原图
数米粒原图。


pic=imread('rice.png');
bg=imopen(pic,strel('disk',15));
temp=imsubtract(pic,bg);
level=graythresh(temp);
bw=im2bw(temp,level);
subplot(1,3,1);imshow(bw);title('b-w image');hold on;        %???
[label, num]=bwlabel(bw,8);    %??
disp(['the number of grains is',num2str(num)])

%practice:size of grains
for i=1:99
    count(i)=0;
    for j=1:size(label,1)
        for k=1:size(label,2)
            if label(j,k)==i
                count(i)=count(i)+1;
            end
        end
    end
end
subplot(1,3,2);
histogram(count);
title('the size of grains');
hold on;

%practise:painting
green=zeros(256,256);
blue=zeros(256,256);
red=label;
    for j1=1:size(red,1)
        for k=1:size(red,2)
            if red(j,k)==0
                red(j,k)=0;
            else
                red(j,k)=255;
            end
        end
    end

test=cat(3,red,green,blue);
subplot(1,3,3);imshow(test);title('painting');

output:
num=99

这里写图片描述


%%bwlabel误差体现在两粒米重叠处 可根据直方图修正。

猜你喜欢

转载自blog.csdn.net/weixin_42882826/article/details/81416169