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

来源:互联网 发布:永久禁止程序访问网络 编辑:程序博客网 时间:2024/05/01 08:33

使用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 erodeerode_copy = erode;imtool(erode_copy);%   c is the number of the rice grainsc = 0;%   rice_arr is an array which stores the area of each rice grainrice_arr = zeros(1, 100);%% Calculate the number of the rice and the area of each rice graincount = 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    endenddisp('米粒的个数')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    endend%   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    endend%   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    endend%   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    endend%   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    endend%   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    endend%   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    endend%   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    endendrice_area = count;imchanged = im;


原创粉丝点击