• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

matlab实现种子识别、计数(附GUI界面)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.整体方案设计

1.1 种子数量统计的应用背景

我国作为一个农业大国,粮食产量大但粮食以及植物的种子的数量统计和质量辨别在之前都主要以人力主观判断,任务繁琐重复率高,误差大,效率低,耗时少,为农业工作者增加负担。而随着颗粒图像处理技术的发展,很多工具已经能自动,快速的书别图像颗粒,客观统计颗粒数量,并且提取颗粒各种特征参数,在辅助以其他处理软件,进行分析处理,极大的减少人工的工作量并且提高工作效果以及准确度。
这次的设计内容是希望利用数字图像的内容简单地实现对植物种子以及不同颗粒数量上的统计以及颗粒细节上的处理。

1.2 主要步骤说明

(1)读入种子图片 (timg2.png);
(2)将图片进行灰度和二值化处理;
(3)加入高斯噪声,其中;
(4)滤波去噪(中值滤波);
(5)对步骤(4)中去噪的图像,用开运算,二值化和贴标签等方法,自动统计种子的数量。

2.实验与分析

2.1 实验分布实现

2.1.1 读入种子图片(timg2.png)

利用matlab中自带的图像读取函数,函数说明:A=imread(IMGFILENAME,FMT),IMGFILENAME 指定图像文件的完整路径和文件名。如果在work工作目录下只需提供文件名,否则应提供完整路径,且路径中不能包含中文符号。FMT为图像文件的格式对应的标准扩展名。

程序代码如下:
I = imread(‘timg2.png’); %调用imread()读取图像timg2.png

2.1.2对图像进行灰度和二值化处理

程序代码如下:
G=rgb2gray(I);
level = graythresh(G);
bw = im2bw(G,level);
运行结果:

2.1.3 加入高斯噪声并滤波去噪

Matlab中加噪声函数使用说明:h=imnoise(I,type,parameters),I为需要加噪的图像,type为噪声类型,有gaussian、localva、poisson、salt & pepper、speckle,parameters为所加噪声参数。本论文中指定添加的噪声为高斯噪声N(μ,σ2),其中,μ=0,σ2=0.01 。Matlab中自带中值滤波函数medfilt2(I,[m,n]),其中I代表待滤波图像,[m,n]指窗口大小,默认大小为3x3。

程序代码如下:
H = im2double(bw);
J=imnoise(H, ‘gaussian’);
K=medfilt2(J,[10,10]);
运行结果:

2.1.4 统计种子数量

对上面去噪后的图像用开、闭运算,二值化和贴标签等方法,自动统计种子的数量。
开运算:
  先腐蚀后膨胀的过程称为开运算。用来消除小物体、在纤细点处分离物体、平滑较大物体的边界的同时并不明显改变其面积。
  
闭运算:
先膨胀后腐蚀的过程称为闭运算。用来填充物体内细小空洞、连接邻近物体、平滑其边界的同时并不明显改变其面积。
  
种子统计可以采用二值化、贴标签的方法实现。

为了二值化处理的误差尽可能小,可以对待处理图像进行对比度增强处理,可以调用matlab自带的对比度增强函数imadjust()。

程序代码:
I2 = imadjust(K);
运行结果:

程序代码:
background = imopen(K,strel(‘disk’,8));
axes(handles.axes8);
imshow(background);
handles.img=background;
guidata(hObject,handles);

[labeled,numObjects] = bwlabeln(background,4);
disp(‘种子数为’);
disp(numObjects)
运行结果:

2.2 误差分析

在多次程序仿真过程中,我们发现统计结果不是绝对的稳定,会有一个波动范围,即表示该统计结果存在一定误差,在多次测试实验中,我们发现误差大小会因滤波方法及其它参数等的改变而异。
故作了如下讨论:首先,原种子图像经过加噪滤波处理后用开运算、图像增强、二值化、贴标签方法,统计种子数目,得到种子图像的数目为43,与本人计数值相符。

接着,探究不同滤波模板大小和开运算结构半径对图像的影响。开运算半径5~10之间对图像进行处理,同时讨论了中值滤波模板大小的影响,如下图所示。得出结论:在此例中,8半径的开运算对图像的计算较为准确;且以滤波模板大小为10x10的统计误差较小。
一、不同开运算结构半径结果图

二、不同滤波模板大小统计结果图(左边3列分别为模板大小为 8x8, 9x9, 11x11 的中值滤波后种子统计结果,最后则为对应的模板大小为10x10统计结果)




再接着。我们用其他相似图片也进行了测试,发现误差范围都在可接受的范围内:




在研究过程中,我发现:开运算结构单元半径的选取会影响统计结果,当半径过小或过大时,都会引起较大误差;贴标签采用的连通域大小的选取也会给统计结果带来不一样的误差,在本例中,采用4连通比采用8连通域处理的误差较小。

function varargout = fseed(varargin)
% FSEED MATLAB code for fseed.fig
% FSEED, by itself, creates a new FSEED or raises the existing
% singleton*.
%
% H = FSEED returns the handle to a new FSEED or the handle to
% the existing singleton*.
%
% FSEED(‘CALLBACK’,hObject,eventData,handles,…) calls the local
% function named CALLBACK in FSEED.M with the given input arguments.
%
% FSEED(‘Property’,‘Value’,…) creates a new FSEED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before fseed_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to fseed_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE’s Tools menu. Choose “GUI allows only one
% instance to run (singleton)”.
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help fseed

% Last Modified by GUIDE v2.5 01-Jul-2020 20:41:36

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(‘gui_Name’, mfilename, …
‘gui_Singleton’, gui_Singleton, …
‘gui_OpeningFcn’, @fseed_OpeningFcn, …
‘gui_OutputFcn’, @fseed_OutputFcn, …
‘gui_LayoutFcn’, [] , …
‘gui_Callback’, []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% — Executes just before fseed is made visible.
function fseed_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to fseed (see VARARGIN)

% Choose default command line output for fseed
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes fseed wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% — Outputs from this function are returned to the command line.
function varargout = fseed_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% — Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I = imread(‘F:\新建文件夹\timg2.png’);
axes(handles.axes1); %将图像显示在axes1坐标区中
imshow(I);
handles.img=I; %显现的图像为I
guidata(hObject,handles); %告诉系统更新axes1上的图像

% — Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I = imread(‘F:\新建文件夹\timg2.png’);
G=rgb2gray(I); %灰度图
axes(handles.axes2);
imshow(G);
handles.img=G;
guidata(hObject,handles);

% — Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I = imread(‘F:\新建文件夹\timg2.png’);
G=rgb2gray(I);
level = graythresh(G); %推算二值化阈值
bw = im2bw(G,level); %图像二值化处理
axes(handles.axes3);
imshow(bw);
handles.img=bw;
guidata(hObject,handles);

% — Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I = imread(‘F:\新建文件夹\timg2.png’);
G=rgb2gray(I);
level = graythresh(G); %推算二值化阈值
bw = im2bw(G,level); %图像二值化处理
H = im2double(bw); %转换成双精度图像才可以加入噪声
J=imnoise(H, ‘gaussian’); %向图像中加入高斯噪声N(μ=0,σ=0.1)
axes(handles.axes4);
imshow(J);
handles.img=J;
guidata(hObject,handles);

% — Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% — Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I = imread(‘F:\新建文件夹\timg2.png’);
G=rgb2gray(I);
level = graythresh(G); %推算二值化阈值
bw = im2bw(G,level); %图像二值化处理
H = im2double(bw); %转换成双精度图像才可以加入噪声
J=imnoise(H, ‘gaussian’); %向图像中加入高斯噪声N(μ=0,σ=0.1)
K=medfilt2(J,[10,10]); %对加噪后图像进行中值滤波
axes(handles.axes6);
imshow(K);
handles.img=K;
guidata(hObject,handles);

% — Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I = imread(‘F:\新建文件夹\timg2.png’);
G=rgb2gray(I);
level = graythresh(G); %推算二值化阈值
bw = im2bw(G,level); %图像二值化处理
H = im2double(bw); %转换成双精度图像才可以加入噪声
J=imnoise(H, ‘gaussian’); %向图像中加入高斯噪声N(μ=0,σ=0.1)
K=medfilt2(J,[10,10]); %对加噪后图像进行中值滤波
I2 = imadjust(K); %对比度增强处理
axes(handles.axes7);
imshow(I2);
handles.img=I2;
guidata(hObject,handles);

% — Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I = imread(‘F:\新建文件夹\timg2.png’);
G=rgb2gray(I);
level = graythresh(G); %推算二值化阈值
bw = im2bw(G,level); %图像二值化处理
H = im2double(bw); %转换成双精度图像才可以加入噪声
J=imnoise(H, ‘gaussian’); %向图像中加入高斯噪声N(μ=0,σ=0.1)
K=medfilt2(J,[10,10]); %对加噪后图像进行中值滤波
I2 = imadjust(K); %对比度增强处理
background = imopen(K,strel(‘disk’,8)); %读入图像,并对图像进行开运算
axes(handles.axes8);
imshow(background);
handles.img=background;
guidata(hObject,handles);

[labeled,numObjects] = bwlabeln(background,4); % 4-connected贴标签并统计图像中种子数目,返回一个和labeled一样矩阵大小和background一样一个读入二值图像后产生的background数组,4表示如果像素的位置在其他像素相邻的上、下、左、右,num返回的就是background中连通域的个数
disp(‘种子数为:’);
disp(numObjects) %显示图像中种子数目

结果图

实验所用的原图如下:

想要运行的朋友,打开matlab将I = imread(‘F:\新建文件夹\timg2.png’);双引号中图片的路径都改成上面的原图在你的电脑中的路径即可。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
delphicmd发布时间:2022-07-18
下一篇:
Delphi10Seattleplus新特性——System.JSON.Builders发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap