当前位置:   article > 正文

matlab GUI界面设计_matlabgui界面

matlabgui界面

【实验内容】

用MATLAB的GUI程序设计一个具备图像边缘检测功能的用户界面,该设计程序有以下基本功能:

(1)图像的读取和保存。

(2)设计图形用户界面,让用户对图像进行彩色图像到灰度图像的转换,并显示原图和灰度图像。

(3)设计图形用户界面,让用户能够根据需要来选择边缘检测算子,即选择边缘检测的方法。

(4)设计图形用户界面,让用户能够自行设定检测的阈值和方向。

(5)显示边缘检测后的图像,并与原图和灰度图像进行对比。

【实验步骤】

1、建立菜单,选项包括“文件”(打开、保存、退出)、“检测方法”(sobel、prewitt、roberts、canny)和“帮助”。

建立3个坐标轴对象,用于显示原始图像、灰度图像和边缘检测后的图像。

建立1个按钮,用于将原始图像转换为灰度图像。

建立1个文本编辑框,用于输入数据。

建立菜单,选项包括“文件”(打开、保存、退出)、“检测方法”(sobel、prewitt、roberts、canny)和“帮助”。

五个静态文本框的string属性分别为“原图”、“灰度图像”、“检测图像”、“设定阈值”和“检测方向”。

三个坐标轴的Tag标识分别为original_image、gray_image、test_image。

按钮控件的string属性为“灰度转换”,Tag标识为rgbtogray。

文本编辑框的Tag标识为thresh_value。

列表框的string属性为horizontal、vertical、both,Tag标识为direction。

编写代码完成程序中的变量赋值、输入、输出等工作,打开对应文件,在对应函数位置添加如下程序,其他代码不变。

  1. function varargout = m240531(varargin)
  2. % M240531 MATLAB code for m240531.fig
  3. % M240531, by itself, creates a new M240531 or raises the existing
  4. % singleton*.
  5. %
  6. % H = M240531 returns the handle to a new M240531 or the handle to
  7. % the existing singleton*.
  8. %
  9. % M240531('CALLBACK',hObject,eventData,handles,...) calls the local
  10. % function named CALLBACK in M240531.M with the given input arguments.
  11. %
  12. % M240531('Property','Value',...) creates a new M240531 or raises the
  13. % existing singleton*. Starting from the left, property value pairs are
  14. % applied to the GUI before m240531_OpeningFcn gets called. An
  15. % unrecognized property name or invalid value makes property application
  16. % stop. All inputs are passed to m240531_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 m240531
  23. % Last Modified by GUIDE v2.5 31-May-2024 11:43:02
  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', @m240531_OpeningFcn, ...
  29. 'gui_OutputFcn', @m240531_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 m240531 is made visible.
  42. function m240531_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 m240531 (see VARARGIN)
  48. % Choose default command line output for m240531
  49. handles.output = hObject;
  50. % Update handles structure
  51. guidata(hObject, handles);
  52. % UIWAIT makes m240531 wait for user response (see UIRESUME)
  53. % uiwait(handles.figure1);
  54. % --- Outputs from this function are returned to the command line.
  55. function varargout = m240531_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 rgbtogray.
  63. function rgbtogray_Callback(hObject, eventdata, handles)
  64. % hObject handle to rgbtogray (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. gray=rgb2gray(handles.img)
  68. set(handles.gray_image, 'HandleVisibility', 'ON');
  69. axes(handles.gray_image)
  70. imshow(gray);
  71. handles.img=gray;
  72. guidata(hObject,handles);
  73. function thresh_value_Callback(hObject, eventdata, handles)
  74. % hObject handle to thresh_value (see GCBO)
  75. % eventdata reserved - to be defined in a future version of MATLAB
  76. % handles structure with handles and user data (see GUIDATA)
  77. % Hints: get(hObject,'String') returns contents of thresh_value as text
  78. % str2double(get(hObject,'String')) returns contents of thresh_value as a double
  79. % --- Executes during object creation, after setting all properties.
  80. function thresh_value_CreateFcn(hObject, eventdata, handles)
  81. % hObject handle to thresh_value (see GCBO)
  82. % eventdata reserved - to be defined in a future version of MATLAB
  83. % handles empty - handles not created until after all CreateFcns called
  84. % Hint: edit controls usually have a white background on Windows.
  85. % See ISPC and COMPUTER.
  86. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  87. set(hObject,'BackgroundColor','white');
  88. end
  89. % --- Executes on selection change in direction.
  90. function direction_Callback(hObject, eventdata, handles)
  91. % hObject handle to direction (see GCBO)
  92. % eventdata reserved - to be defined in a future version of MATLAB
  93. % handles structure with handles and user data (see GUIDATA)
  94. % Hints: contents = cellstr(get(hObject,'String')) returns direction contents as cell array
  95. % contents{get(hObject,'Value')} returns selected item from direction
  96. % --- Executes during object creation, after setting all properties.
  97. function direction_CreateFcn(hObject, eventdata, handles)
  98. % hObject handle to direction (see GCBO)
  99. % eventdata reserved - to be defined in a future version of MATLAB
  100. % handles empty - handles not created until after all CreateFcns called
  101. % Hint: popupmenu controls usually have a white background on Windows.
  102. % See ISPC and COMPUTER.
  103. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  104. set(hObject,'BackgroundColor','white');
  105. end
  106. % --------------------------------------------------------------------
  107. function Untitled_1_Callback(hObject, eventdata, handles)
  108. % hObject handle to Untitled_1 (see GCBO)
  109. % eventdata reserved - to be defined in a future version of MATLAB
  110. % handles structure with handles and user data (see GUIDATA)
  111. % --------------------------------------------------------------------
  112. function Untitled_2_Callback(hObject, eventdata, handles)
  113. % hObject handle to Untitled_2 (see GCBO)
  114. % eventdata reserved - to be defined in a future version of MATLAB
  115. % handles structure with handles and user data (see GUIDATA)
  116. % --------------------------------------------------------------------
  117. function Untitled_3_Callback(hObject, eventdata, handles)
  118. % hObject handle to Untitled_3 (see GCBO)
  119. % eventdata reserved - to be defined in a future version of MATLAB
  120. % handles structure with handles and user data (see GUIDATA)
  121. % --------------------------------------------------------------------
  122. function Sobel_Callback(hObject, eventdata, handles)
  123. % hObject handle to Sobel (see GCBO)
  124. % eventdata reserved - to be defined in a future version of MATLAB
  125. % handles structure with handles and user data (see GUIDATA)
  126. v1=str2double(get(handles.thresh_value, 'string' ));
  127. contents=get(handles.direction,'string');
  128. v2=contents{(get(handles.direction,'value'))};
  129. edge_sobel=edge(handles.img,'sobel',v1,v2);
  130. set(handles.test_image, 'HandleVisibility','ON');
  131. axes(handles.test_image);
  132. imshow(edge_sobel)
  133. handles.img=edge_sobel;
  134. guidata(hObject,handles);
  135. % --------------------------------------------------------------------
  136. function Prewitt_Callback(hObject, eventdata, handles)
  137. % hObject handle to Prewitt (see GCBO)
  138. % eventdata reserved - to be defined in a future version of MATLAB
  139. % handles structure with handles and user data (see GUIDATA)
  140. v1=str2double(get(handles.thresh_value,'string'))
  141. contents=get(handles.direction, 'string');
  142. v2=contents{(get(handles.direction,'value' ))};
  143. edge_prewitt=edge(handles.img, 'prewitt' ,v1,v2);
  144. set(handles.test_image, 'HandleVisibility','ON');
  145. axes(handles.test_image);
  146. imshow(edge_prewitt)
  147. handles.img=edge_prewitt;
  148. guidata(hObject,handles);
  149. % --------------------------------------------------------------------
  150. function Roberts_Callback(hObject, eventdata, handles)
  151. % hObject handle to Roberts (see GCBO)
  152. % eventdata reserved - to be defined in a future version of MATLAB
  153. % handles structure with handles and user data (see GUIDATA)
  154. v1=str2double(get(handles.thresh_value,'string'))
  155. contents=get(handles.direction, 'string');
  156. v2=contents{(get(handles.direction,'value'))};
  157. edge_roberts=edge(handles.img, 'roberts' ,v1,v2);
  158. set(handles.test_image, 'HandleVisibility','ON');
  159. axes(handles.test_image);
  160. imshow(edge_roberts)
  161. handles.img=edge_roberts;
  162. guidata(hObject,handles);
  163. % --------------------------------------------------------------------
  164. function Canny_Callback(hObject, eventdata, handles)
  165. % hObject handle to Canny (see GCBO)
  166. % eventdata reserved - to be defined in a future version of MATLAB
  167. % handles structure with handles and user data (see GUIDATA)
  168. v1=str2double(get(handles.thresh_value, 'string'));
  169. contents=get(handles.direction,'string');
  170. v2=contents{(get(handles.direction,'value'))};
  171. edge_canny=edge(handles.img,'canny',v1, v2);
  172. set(handles.test_image, 'HandleVisibility','ON');
  173. axes(handles.test_image);
  174. imshow(edge_canny);
  175. handles.img=edge_canny;
  176. guidata(hObject,handles);
  177. % --------------------------------------------------------------------
  178. function open_Callback(hObject, eventdata, handles)
  179. % hObject handle to open (see GCBO)
  180. % eventdata reserved - to be defined in a future version of MATLAB
  181. % handles structure with handles and user data (see GUIDATA)
  182. [filename,pathname] = uigetfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'载入图像');
  183. if isequal(filename,0) || isequal(pathname,0)
  184. errordlg('没有选中文件','出错');
  185. return;
  186. else
  187. file=[pathname,filename];
  188. global S
  189. S=file;
  190. x=imread(file);
  191. set(handles.original_image,'HandleVisibility','ON');
  192. axes(handles.original_image);
  193. imshow(x);
  194. handles.img=x;
  195. guidata(hObject,handles);
  196. end
  197. % --------------------------------------------------------------------
  198. function save_Callback(hObject, eventdata, handles)
  199. % hObject handle to save (see GCBO)
  200. % eventdata reserved - to be defined in a future version of MATLAB
  201. % handles structure with handles and user data (see GUIDATA)
  202. [sfilename,sfilepath]=uiputfile({'*.jpg';'*.bmp';'*.tif';'*.*'}, ...
  203. '保存图像文件','untitled.jpg');
  204. if ~isequal([sfilename,sfilepath],[0,0])
  205. sfileullname = [sfilepath,sfilename];
  206. imwrite(handles.img, sfilefullname);
  207. else
  208. msgbox('你按了取消键','保存失败');
  209. end
  210. % --------------------------------------------------------------------
  211. function exit_Callback(hObject, eventdata, handles)
  212. % hObject handle to exit (see GCBO)
  213. % eventdata reserved - to be defined in a future version of MATLAB
  214. % handles structure with handles and user data (see GUIDATA)
  1. 执行程序后,单击菜单栏中的文件,打开图片,在原图位置会显示彩色图像,单击“灰度转换”按钮,在灰度图像位置会显示转换后的灰度图像,在“设定阈值”框输入0.1,选择“检测方向”为both,再在“检测方法”菜单中选择Canny,即可在“检测图像”的位置显示边缘检测后的图像,最后在“文件”菜单中选择“保存”,即可保存最终分割后的边缘检测图。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/837655
推荐阅读
相关标签
  

闽ICP备14008679号