小言_互联网的博客

【水果识别】基于灰度直方图水果识别matlab源码含 GUI

202人阅读  评论(0)

一、简介

基于matlab GUI 灰度、二值化、腐蚀、直方图处理之水果识别

二、源代码


  
  1. function varargout = untitled(varargin)
  2. % UNTITLED MATLAB code for untitled.fig
  3. % UNTITLED, by itself, creates a new UNTITLED or raises the existing
  4. % singleton*.
  5. %
  6. % H = UNTITLED returns the handle to a new UNTITLED or the handle to
  7. % the existing singleton*.
  8. %
  9. % UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
  10. % function named CALLBACK in UNTITLED.M with the given input arguments.
  11. %
  12. % UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
  13. % existing singleton*. Starting from the left, property value pairs are
  14. % applied to the GUI before untitled_OpeningFcn gets called. An
  15. % unrecognized property name or invalid value makes property application
  16. % stop. All inputs are passed to untitled_OpeningFcn via varargin.
  17. %
  18. % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
  19. % instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES
  22. % Edit the above text to modify the response to help untitled
  23. % Last Modified by GUIDE v2.5 15-Jan-2017 01:03:42
  24. % Begin initialization code - DO NOT EDIT
  25. gui_Singleton = 1;
  26. gui_State = struct( 'gui_Name', mfilename, ...
  27. 'gui_Singleton', gui_Singleton, ...
  28. 'gui_OpeningFcn', @untitled_OpeningFcn, ...
  29. 'gui_OutputFcn', @untitled_OutputFcn, ...
  30. 'gui_LayoutFcn', [] , ...
  31. 'gui_Callback', []);
  32. if nargin && ischar(varargin{ 1})
  33. gui_State.gui_Callback = str2func(varargin{ 1});
  34. end
  35. if nargout
  36. [varargout{ 1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  37. else
  38. gui_mainfcn(gui_State, varargin{:});
  39. end
  40. % End initialization code - DO NOT EDIT
  41. % --- Executes just before untitled is made visible.
  42. function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
  43. % This function has no output args, see OutputFcn.
  44. % hObject handle to figure
  45. % eventdata reserved - to be defined in a future version of MATLAB
  46. % handles structure with handles and user data (see GUIDATA)
  47. % varargin command line arguments to untitled (see VARARGIN)
  48. % Choose default command line output for untitled
  49. handles.output = hObject;
  50. % Update handles structure
  51. guidata(hObject, handles);
  52. % UIWAIT makes untitled wait for user response (see UIRESUME)
  53. % uiwait(handles.figure1);
  54. % --- Outputs from this function are returned to the command line.
  55. function varargout = untitled_OutputFcn(hObject, eventdata, handles)
  56. % varargout cell array for returning output args (see VARARGOUT);
  57. % hObject handle to figure
  58. % eventdata reserved - to be defined in a future version of MATLAB
  59. % handles structure with handles and user data (see GUIDATA)
  60. % Get default command line output from handles structure
  61. varargout{ 1} = handles.output;
  62. % --- Executes on button press in pushbutton3.
  63. function pushbutton3_Callback(hObject, eventdata, handles)
  64. % hObject handle to pushbutton3 (see GCBO)
  65. % eventdata reserved - to be defined in a future version of MATLAB
  66. % handles structure with handles and user data (see GUIDATA)
  67. global I FilledLabel HSV MeanHue Ecllipseratio Label num Premeter Area
  68. for i = 1 : num
  69. Premeter(i) = 0;
  70. end
  71. [row,col] = size(Label);
  72. for i = 1 : row
  73. for j = 1 : col
  74. if(Label(i,j) > 0)
  75. Premeter(Label(i,j)) = Premeter(Label(i,j)) + 1; %计算标记后的各块图形边界中像素的个数的总数
  76. end
  77. end
  78. end
  79. %计算各个图形单元的面积
  80. FilledLabel=imfill(Label, 'holes'); %填充打过标记的边界线中间围成的图形区域
  81. for i = 1 : num
  82. Area(i) = 0;
  83. end
  84. [row,col] = size(FilledLabel);
  85. for i = 1 : row
  86. for j = 1 : col
  87. if(FilledLabel(i,j) > 0)
  88. Area(FilledLabel(i,j)) = Area(FilledLabel(i,j)) + 1; %通过统计像素点个数的方式来求各形状的面积
  89. end
  90. end
  91. end
  92. %计算各个图形单元的圆度
  93. for i = 1 : num
  94. Ecllipseratio(i) = 4*pi*Area(i)/Premeter(i)^ 2;
  95. end
  96. %计算各个图像的颜色(色度)
  97. HSV=rgb2hsv(I); %转换为HSV,为后面的颜色元素的提取做准备
  98. [row,col] = size(FilledLabel); %统计填充后的图形中各块图形所含像素的个数的多少
  99. MeanHue = zeros( 1,num);
  100. for i = 1 : num
  101. Hue = zeros(Area(i), 1);
  102. nPoint = 0;
  103. for j = 1 : row
  104. for k = 1 : col
  105. if(FilledLabel(j,k) == i)
  106. nPoint = nPoint + 1;
  107. Hue(nPoint, 1) = HSV(j,k, 1);
  108. end
  109. end
  110. end
  111. Hue(:,i) = sort(Hue(:, 1));
  112. for j = floor(nPoint* 0.1) : floor(nPoint* 0.9)
  113. MeanHue(i) = MeanHue(i) + Hue(j, 1);
  114. end
  115. MeanHue(i) = MeanHue(i) / ( 0.8*nPoint); %计算出平均的色度值
  116. end
  117. %识别菠胡萝卜
  118. %构建胡萝卜的分类器,在二维特征空间对各个图像进行类别区分
  119. huluobo= 1;
  120. mazarea=Area( 1);
  121. for i= 1:num
  122. if(mazarea<Area(i)) %分类器识别桃的准则:判断各个图形中面积最大的为胡萝卜
  123. mazarea=Area(i);
  124. huluobo=i;
  125. end
  126. end
  127. %对分出来的类别分别构建相应的图像掩膜,并用对原图的亮度图像进行掩膜操作
  128. huluoboHSV=HSV;
  129. for j = 1 : row
  130. for k = 1 : col
  131. if(FilledLabel(j,k) ~=huluobo)
  132. %huluoboHSV(j,k,2)=0;
  133. huluoboHSV(j,k, 3)= 0;
  134. end
  135. end
  136. end
  137. %变换生成最终的结果图像,图像中显示的结果即对应分类器中指定的类别
  138. huluobomatrix = hsv2rgb(huluoboHSV); %转换为RGB彩图,彩图中已经滤去了其余水果,只剩下胡萝卜
  139. subplot( 2, 2, 4),imshow(huluobomatrix);
  140. % --- Executes on button press in pushbutton4.
  141. function pushbutton4_Callback(hObject, eventdata, handles)
  142. % hObject handle to pushbutton4 (see GCBO)
  143. % eventdata reserved - to be defined in a future version of MATLAB
  144. % handles structure with handles and user data (see GUIDATA)
  145. global I Area FilledLabel HSV MeanHue Ecllipseratio Label num Premeter
  146. %计算各个图形单元的周长 用连接像素点或数边界像素点个数的方法 numPoints数组表示各个图形边界的像素个数(即用个数来表示周长)
  147. %num = max(max(Label));
  148. for i = 1 : num
  149. Premeter(i) = 0;
  150. end
  151. [row,col] = size(Label);
  152. for i = 1 : row
  153. for j = 1 : col
  154. if(Label(i,j) > 0)
  155. Premeter(Label(i,j)) = Premeter(Label(i,j)) + 1; %计算标记后的各块图形边界中像素的个数的总数
  156. end
  157. end
  158. end
  159. %计算各个图形单元的面积
  160. FilledLabel=imfill(Label, 'holes'); %填充打过标记的边界线中间围成的图形区域
  161. for i = 1 : num
  162. Area(i) = 0;
  163. end
  164. [row,col] = size(FilledLabel);
  165. for i = 1 : row
  166. for j = 1 : col
  167. if(FilledLabel(i,j) > 0)
  168. Area(FilledLabel(i,j)) = Area(FilledLabel(i,j)) + 1; %通过统计像素点个数的方式来求各形状的面积
  169. end
  170. end
  171. end

三、运行结果




四、备注

完整代码或者代写添加QQ 1575304183


转载:https://blog.csdn.net/weixin_50197058/article/details/116425919
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场