MATLAB递归将数字一个个输出,数米粒个数和每个米粒面积的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;
MATLAB递归将数字一个个输出,数米粒个数和每个米粒面积的matlab算法实现(递归)。 | 学步园...相关推荐
- 利用matlab数米粒数量,数米粒个数和每个米粒面积的matlab算法实现(递归)。
使用Matlab软件自带的rice.png图片进行处理. 不知道使用的函数利用help function-name 或者 lookfor function-name 查看 这里是实现的主要代码段 %T ...
- 如何用matlab进行部分式展开_高数简单问题:真分式化成部分分式之和题目,MATLAB中对多项式进行部分分式展开。...
用MATLAB进行部分分式展开 MATLAB有1个命令用于求B(s)/A(s)的部分分式展开式. 设s的有理分式为 式中 (i=)和(j=)的某些值可能为零.在MATLAB的行向量中,num和den分 ...
- java输出体重指数_Android开发–身高体重指数(BIM)计算–完成BMI程序 | 学步园...
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:修改表达用户界 ...
- php求完数,php算法:求完全数 | 学步园
完全数:如果一个数的所有因数(不包括本身)的和刚好等于这个数自身,那么这个数就叫完全数 求完全数 function get_mul($num) { for($i=1;$i<=$num;$i++) ...
- matlab knn,MATLAB K近邻算法 — knnsearch() 函数 | 学步园
K近邻 IDX = knnsearch(X,Y) finds the nearest neighbor in X for each point in Y. X is an MX-by-N matrix ...
- 数米粒个数和测米粒大小
clear,close all I=imread('Image/mili.bmp'); imshow(I); %%%估计背景图象 background=imopen(I,strel('disk',15 ...
- 关于MATLAB实现的数字信号处理(二)
上一篇:关于MATLAB实现的数字信号处理(一) 下一篇:关于MATLAB实现的数字信号处理(三) 文章目录 上一篇:关于MATLAB实现的数字信号处理(一) 下一篇:关于MATLAB实现的数字信号处 ...
- matlab 输出数,matlab入门(变量、数值运算、输出)
变量 变量命名原则 1.以字母开头 2.后面可以跟 字母.数字和下划线 3.长度不超过 63 个字符 4.区分字母的 大小写 分号和续行符 若不想在屏幕上输出结果,可以在语句最后加分号 如果语句很长, ...
- AMNO.6 给出一个不多于5位的整数,要求 1、求出它是几位数 2、分别输出每一位数字 3、按逆序输出各位数字,例如原数为321,应输出123 输入 一个不大于5位的数字
题目描述 给出一个不多于5位的整数,要求 1.求出它是几位数 2.分别输出每一位数字 3.按逆序输出各位数字,例如原数为321,应输出123 输入 一个不大于5位的数字 输出 三行 第一行 位数 第二 ...
最新文章
- python程序员职业考试_最新10大Python面试常问的问题,60%的人都不会
- 7-9 用天平找小球 (C语言)
- JavaScript公共运行库
- C/C++ 一段代码区分数组指针|指针数组|函数指针|函数指针数组
- MaxCompute 多租户数据安全体系
- 光进铜退下的“更高”与“更低”,锐捷发布企业极简以太全光网解决方案
- (转)JVM监控工具介绍
- 谁将拥有移动互联网的下一个机会?…
- java 按顺序读取文件夹_java读取某个文件夹下的所有文件实例代码
- java程序拦截dde漏洞问题_Office DDE漏洞学习笔记
- 局域网 访问计算机 软件,局域网查看工具
- 精心挑选的100多种机器学习数据集
- stm32通过 一线串口通信控制JQ8900-16P语音模块
- accuracy.eval
- 服务器都没有显卡型号吗,云服务器都没显卡么?
- 无线安全WLAN WEP破解(一)
- 开源许可违反:案例说明(Apache License 2.0)
- js(JavaScript)操作字符串的方法
- 特定偏好的效用函数——CES效用函数
- 让数字内容被平等获取 | Android 开发者故事