数米粒个数和每个米粒面积的matlab算法实现(递归)。

使用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 + 1;
            [rice_arr(c),erode_copy] = cal_rice_num(erode_copy, j, i, count);
        end
    end
end
disp('米粒的个数')
disp(c);
disp('米粒的大小')
disp(rice_arr);
这里是要调用的函数cal_rice_num();

function [rice_area, imchanged]  = cal_rice_num(im, i, j, count)
%CAL_RICE_NUM Calculate the rice grains of the image 'rice.png'
%   IM is the logical edge-fillhole-erode image of the 'rice.png'
%   I, J is the position of the pixel whose value is 1 
im(i,j) = 0;
[rows, columns] = size(im);
%   For (i-1, j-1)
if i-1 > 0 && i-1 <= rows 
    if j-1 > 0 && j-1 <= columns
        if im(i-1, j-1) == 1
            count = count + 1;
            [count, im] = cal_rice_num(im, i-1, j-1, count);
        end
    end
end
%   For (i-1, j)
if i-1 > 0 && i-1 <= rows
    if j > 0 && j <= columns
        if im(i-1, j) == 1
            count = count + 1;
            [count, im] = cal_rice_num(im, i-1, j, count);
        end
    end
end
%   For (i-1, j+1)
if i-1 > 0 && i-1 <= rows
    if j+1 > 0 && j+1 <= columns
        if im(i-1, j+1) == 1
            count = count + 1;
            [count, im] = cal_rice_num(im, i-1, j+1, count);
        end
    end
end
%   For (i, j-1)
if i > 0 && i <= rows
    if j-1 > 0 && j-1 <= columns
        if im(i, j-1) == 1
            count = count + 1;
            [count, im] = cal_rice_num(im, i, j-1, count);
        end
    end
end
%   For (i, j+1)
if i-1 > 0 && i-1 <= rows
    if j+1 > 0 && j+1 <= columns
        if im(i-1, j+1) == 1
            count = count + 1;
            [count, im] = cal_rice_num(im, i-1, j+1, count);
        end
    end
end
%   For (i+1, j-1)
if i+1 > 0 && i+1 <= rows
    if j-1 > 0 && j-1 <= columns
        if im(i+1, j-1) == 1
            count = count + 1;
            [count, im] = cal_rice_num(im, i+1, j-1, count);
        end
    end
end
%   For(i+1, j)
if i+1 > 0 && i+1 <= rows
    if j > 0 && j <= columns
        if im(i+1, j) == 1
            count = count + 1;
            [count, im] = cal_rice_num(im, i+1, j, count);
        end
    end
end
%   For(i+1, j+1)
if i+1 > 0 && i+1 <= rows
    if j+1 > 0 && j+1 <= columns
        if im(i+1, j+1) == 1
            count = count + 1;
            [count, im] = cal_rice_num(im, i+1, j+1, count);
        end
    end
end
rice_area = count;
imchanged = im;


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭