利用MATLAB数米粒数量

I = imread('rice.png');
imshow(I);
background = imopen(I,strel('disk',15));
figure
surf(double(background(1:8:end,1:8:end))),zlim([0 255]);
set(gca,'ydir','reverse');
I2 = I - background;
imshow(I2)
J = imadjust(I2);
imshow(J);
bw =imbinarize(J);
bw = bwareaopen(bw, 50);
imshow(bw);
cc = bwconncomp(bw, 4);
cc.NumObjects;
grain = false(size(bw));
grain(cc.PixelIdxList{50}) = true;
imshow(grain);
labeled = labelmatrix(cc);
RGB_label = label2rgb(labeled, @spring, 'c', 'shuffle');
imshow(RGB_label)
graindata = regionprops(cc, 'basic')
graindata(50).Area
grain_areas = [graindata.Area];
[min_area, idx] = min(grain_areas)
grain = false(size(bw));
grain(cc.PixelIdxList{idx}) = true;
imshow(grain);
figure
histogram(grain_areas)
title('Histogram of Rice Grain Area');

你可能感兴趣的:(利用MATLAB数米粒数量)