【参看:https://www.bilibili.com/video/av14503445/?p=9】
目录
影像变为黑白影像:
graythresh()和im2bw()
I = imread('rice.png');
level=graythresh(I);
bw =im2bw(I,level); subplot(1,2,1); imshow(I);
subplot(1,2,2); imshow(bw);
预测背景
>> BG =imopen(I,strel('disk',15));
>> imshow(BG);
>>
I = imread('rice.png'); level = graythresh(I);
bw = im2bw(I,level); subplot(1,2,1);
imshow(bw); BG = imopen(I,strel('disk',15));
I2 = imsubtract(I,BG); level = graythresh(I2);
bw2 = im2bw(I2,level);
subplot(1,2,2); imshow(bw2);
计算一共有多少米:
bwlabel()
>> I = imread('rice.png');
>> BG =imopen(I,strel('disk',15));
>> I2 = imsubtract(I,BG); level = graythresh(I2);
>> BW = im2bw(I2,level);
>> [labeled, numObjects] = bwlabel(BW,8);
>> numObjects
numObjects =
99
>>
【99粒米】
用颜色标注:label2rgb()
>> I = imread('rice.png');
>> BG =imopen(I,strel('disk',15));
>> I2 = imsubtract(I,BG); level = graythresh(I2);
>> BW = im2bw(I2,level);
>> [labeled, numObjects] = bwlabel(BW,8);
>> RGB_label = label2rgb(labeled);imshow(RGB_label);
regionprops()
得到图片的一些特性:
>> I = imread('rice.png');
>> BG =imopen(I,strel('disk',15));
>> I2 = imsubtract(I,BG); level = graythresh(I2);
>> BW = im2bw(I2,level);
>> [labeled, numObjects] = bwlabel(BW,8);
>> graindata = regionprops(labeled,'basic');
>> graindata(51)
ans =
Area: 155
Centroid: [112.4258 245.8645]
BoundingBox: [108.5000 234.5000 8 22]
>>
>> I = imread('rice.png');
>> level = graythresh(I);
>> BG =imopen(I,strel('disk',15));
>> I2 = imsubtract(I,BG);
>> BW = im2bw(I2,graythresh(I2));
>> ObjI = bwselect(BW); imshow(ObjI);
【可以选择要显示的米粒。】