使用Matlab软件自带的rice.png图片进行处理。 不知道使用的函数利用help function-name 或者 lookfor function-name 查看
这里是实现的主要代码段
%The procedure below is to calculate the number of rice in the image
%'rice.png';
rice = imread('rice.png');
ed = edge(rice, 'canny');
fillhole = imfill(ed, 'hole');
se = strel('disk', 3);
% erode is the processed image of 'rice.png'
erode = imopen(fillhole, se);
figure, imshow(erode);
% imtool(erode);
% erode_copy is the copy of erode
erode_copy = erode;
imtool(erode_copy);
% c is the number of the rice grains
c = 0;
% rice_arr is an array which stores the area of each rice grain
rice_arr = zeros(1, 100);
%% Calculate the number of the rice and the area of each rice grain
count = 0;
flagarr = zeros(256);
for i = 1:256
for j = 1:256
flag = erode_copy(j, i);
if flag == 1
c = c +