Matlab图像处理创新实践-实验4【综合设计大实验——GUI综合设计】
- Matlab實驗4——GUI程序操作說明
目? ?錄
一、實驗目的
二、實驗內容
三、實驗過程及結果
步驟1:啟動Matlab,在命令行窗口中輸入“guide”,新建GUI(創建空白GUI)。或者點擊“新建”再點擊“圖形用戶界面”,新建GUI。
步驟2:在左邊空間欄中選擇“Static Text”(靜態文本),將控件放入面板中,并右鍵單擊該控件,選擇“Property Inspector”(屬性檢查器)。
步驟3:在彈出的屬性對話框中,設置控件的屬性值。
步驟4:添加靜態文本控件后,再添加:可編輯文本(Edit Text)、軸(Axes)、按鈕(Push Button)等控件,右鍵點開其屬性框,修改他們的String屬性。右鍵點擊“打開”按鈕,選擇“查看回調”,點擊“Callback”。
步驟5:由于是先創建的“打開”按鈕,所以,“打開”按鈕對應的控件名稱編號是1;控件名稱編號為1的下面編號為2的Callback對應的則是“處理”按鈕。
步驟6:保存程序代碼,單擊菜單上的運行圖形(Ctrl+T)按鈕,實現效果圖如下:
步驟7:在GUI圖形界面上,添加更多的控件,顯示多種濾波器的濾波效果。
步驟8:在GUI圖形界面上,添加更多的控件,顯示圖像的灰度圖、銳化效果圖。
四、分析與思考
附錄:代碼匯總
一、實驗目的
掌握matlab環境下,圖形用戶界面(GUI)的設計方法;同時將學過的圖像處理方法和理論進行綜合運用和設計。
二、實驗內容
第一階段:根據為你們提供好的GUI說明文檔,按照要求一步步進行設計,并最終完成一個基本的具有圖像處理功能的可視化程序。
第二階段:在第一階段工作基礎上,根據自己能力將該程序進行適當的功能擴展。(比如,可以在用戶界面上實現其他濾波的操作、或者實現可調節的銳化操作等等)。盡量多發揮創造性,保證程序的個性化和獨特性,以防千篇一律導致分數受影響。
相關主要函數說明如下:
[filename pathname] = uigetfile({'*.jpg';'*.bmp';'*.png';'*.*'},'打開圖片'); % 打開文件對話框
str = [pathname filename]; % 將兩個字符串合并成一個字符串
axes(handles.axes1); % 打開axes1的句柄,對axes1進行操作
三、實驗過程及結果
【在這里描述下你的實驗操作過程和相應代碼,以及你得到的最終結果,需圖文并茂】
步驟1:啟動Matlab,在命令行窗口中輸入“guide”,新建GUI(創建空白GUI)。或者點擊“新建”再點擊“圖形用戶界面”,新建GUI。
?
步驟2:在左邊空間欄中選擇“Static Text”(靜態文本),將控件放入面板中,并右鍵單擊該控件,選擇“Property Inspector”(屬性檢查器)。
?? ??
步驟3:在彈出的屬性對話框中,設置控件的屬性值。
步驟4:添加靜態文本控件后,再添加:可編輯文本(Edit Text)、軸(Axes)、按鈕(Push Button)等控件,右鍵點開其屬性框,修改他們的String屬性。右鍵點擊“打開”按鈕,選擇“查看回調”,點擊“Callback”。
步驟5:由于是先創建的“打開”按鈕,所以,“打開”按鈕對應的控件名稱編號是1;控件名稱編號為1的下面編號為2的Callback對應的則是“處理”按鈕。
在“打開”按鈕的callback函數下編寫代碼,完成對話框打開文件,并顯示在axes1控件框內。然后在“處理”按鈕的callback函數下編寫代碼,讓其實現:將兩個Edit Text文本框內的數字轉換為高斯濾波器的參數,并對打開的圖像進行高斯濾波并顯示在axes2上。
步驟6:保存程序代碼,單擊菜單上的運行圖形(Ctrl+T)按鈕,實現效果圖如下:
?
?
步驟7:在GUI圖形界面上,添加更多的控件,顯示多種濾波器的濾波效果。
步驟8:在GUI圖形界面上,添加更多的控件,顯示圖像的灰度圖、銳化效果圖。
利用double()將原圖像類型轉換為浮點型,然后按照“原圖像 + 邊緣圖像”的模式,進行計算。利用uint8()或mat2gray()將得到的結果圖像,進行類型轉換,并將原圖像和銳化后的圖像一同顯示在窗口中。
lotus.png四、分析與思考
【在此描述下你從該實驗中的總結內容,得出的結論,并對整個系統功能進行評述,可以查閱資料,最后總結下整個課程的收獲。該部分占最終成績的50%】
此次試驗,獨立完成,收獲甚多。從0開發一個可以對圖片進行濾波的GUI程序,令我對“圖像處理創新實踐”這門課有了更多的認識。一個GUI程序,主要由兩個文件所構成(.figure:界面文件;.m:程序文件)。使用Matlab可以對圖像進行很多的操作,如:旋轉、裁剪、縮小及放大等基本功能,高斯濾波、平滑濾波、銳化濾波等濾波操作。
在GUI程序中,可以添加很多控件,分別實現不同的功能。在步驟8的程序中,設置了5個按鈕,分別實現“打開圖片”、“設置參數對圖片進行濾波”、“顯示多種濾波效果”、“灰度處理”、“圖像銳化”等功能。在對圖像進行處理的同時,給圖像處理結果設置標題,可以令使用者對圖像處理結果有更加直觀的體驗。在“設置參數對圖片進行濾波”的功能實現中,使用者可以設置濾波器的類型、尺寸以及方差,可以看到多種濾波器的不同濾波效果。
關于此GUI程序的評述:此GUI程序較為穩定,內容較為豐富,實現的功能涉及到本門課程的4篇實驗報告,耗時較長,設計合理。
課程收獲總結:非常感謝老師的指導,我在本門課程中學到了很多知識,學會了“使用Matlab對圖像進行多種濾波處理”“使用Matlab涉及GUI程序”等重點內容。但在課程的學習中,仍有很多不足,課下我會好好學習、多查資料、勤加練習。
評分標準:①是否按時出勤實驗課,實驗任務是否全部完成,占50%;② 實驗代碼及報告的格式是否規范,代碼是否注釋,是否美觀,是否詳盡,是否真實,占40%;③? 實驗作業及報告是否按時提交,占10%。
報告文件的命名格式:完整學號
例如:541807010101
附錄:代碼匯總
function varargout = experiment04(varargin) % EXPERIMENT04 MATLAB code for experiment04.fig % EXPERIMENT04, by itself, creates a new EXPERIMENT04 or raises the existing % singleton*. % % H = EXPERIMENT04 returns the handle to a new EXPERIMENT04 or the handle to % the existing singleton*. % % EXPERIMENT04('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in EXPERIMENT04.M with the given input arguments. % % EXPERIMENT04('Property','Value',...) creates a new EXPERIMENT04 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before experiment04_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to experiment04_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 experiment04% Last Modified by GUIDE v2.5 21-Nov-2020 20:06:19% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @experiment04_OpeningFcn, ...'gui_OutputFcn', @experiment04_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []); if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1}); endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); elsegui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT% --- Executes just before experiment04 is made visible. function experiment04_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 experiment04 (see VARARGIN)% Choose default command line output for experiment04 handles.output = hObject;% Update handles structure guidata(hObject, handles);% UIWAIT makes experiment04 wait for user response (see UIRESUME) % uiwait(handles.figure1);% --- Outputs from this function are returned to the command line. function varargout = experiment04_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;function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit1 as text % str2double(get(hObject,'String')) returns contents of edit1 as a double% --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white'); endfunction edit2_Callback(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit2 as text % str2double(get(hObject,'String')) returns contents of edit2 as a double% --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white'); end% --- 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)axis off % 去除刻度 % uigetfile:常規打開文件列表函數【filename:文件名;pathname:路徑;*.*:對應所有文件】 [filename pathname] = uigetfile({'*.jpg';'*.bmp';'*.png';'*.*'}, '打開圖片'); % 函數返回值:文件名稱、文件路徑 str = [pathname filename]; % 合并字符串(合并文件名稱與路徑)global im; % 全局變量,其它函數也可以使用 im = imread(str); axes(handles.axes1); % 打開axes1(坐標軸名)的句柄,對axes1進行操作 設定圖像顯示到哪個組件上 axes:顯示圖像處理效果 str1 = ['圖片位置:' str]; imshow(im); title(str1);% --- 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)global im; gsize = get(handles.edit1, 'String'); % 通過句柄找到對應的組件-尺寸 gsigma = get(handles.edit2, 'String'); % 通過句柄找到對應的組件-方差 gtype = get(handles.edit3, 'String'); % 通過句柄找到對應的組件-濾波器類型 f = fspecial(gtype, str2num(gsize), str2num(gsigma)); % str2num():字符串轉數字 im_f = imfilter(im, f);axes(handles.axes2); imshow(im_f); str = [gtype '濾波器;' '尺寸:' gsize ';方差:' gsigma]; % 合并字符串(合并文件名稱與路徑) title(str);% --- 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)global im;x = im; % 讀取圖片:將圖像數據讀取到Matlab環境中 smallx = imresize(x, 0.05); imshow(smallx); gx = rgb2gray(smallx);f1 = fspecial('average', 5); % 1、均值濾波器 gx1 = filter2(f1, gx); f2 = fspecial('gaussian'); % 2、高斯濾波器 gx2 = filter2(f2, gx); f3 = fspecial('laplacian'); % 3、拉普拉斯濾波器 gx3 = filter2(f3, gx); f4 = fspecial('sobel'); % 4、sobel濾波器 gx4 = filter2(f4, gx); f5 = fspecial('prewitt'); % 5、prewitt濾波器 gx5 = filter2(f5, gx); f6 = fspecial('disk'); % 6、disk濾波器 gx6 = filter2(f6, gx); f7 = fspecial('log'); % 7、log濾波器 gx7 = filter2(f7, gx); f8 = fspecial('motion'); % 8、motion濾波器 gx8 = filter2(f8, gx);axes(handles.axes3); imshow(smallx); title('縮小至原來的0.05倍'); axes(handles.axes4); imshow(uint8(gx1)); title('1、均值濾波器'); axes(handles.axes5); imshow(uint8(gx2)); title('2、高斯濾波器'); axes(handles.axes6); imshow(uint8(gx3)); title('3、拉普拉斯濾波器'); axes(handles.axes7); imshow(uint8(gx4)); title('4、sobel濾波器'); axes(handles.axes8); imshow(uint8(gx5)); title('5、prewitt濾波器'); axes(handles.axes9); imshow(uint8(gx6)); title('6、disk濾波器'); axes(handles.axes10); imshow(uint8(gx7)); title('7、log濾波器'); axes(handles.axes11); imshow(uint8(gx8)); title('8、motion濾波器');function edit3_Callback(hObject, eventdata, handles) % hObject handle to edit3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit3 as text % str2double(get(hObject,'String')) returns contents of edit3 as a double% --- Executes during object creation, after setting all properties. function edit3_CreateFcn(hObject, eventdata, handles) % hObject handle to edit3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white'); end% --- 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)global im; x = im; % 讀取圖片:將圖像數據讀取到Matlab環境中 gx = rgb2gray(x); f1 = fspecial('average'); gx1 = filter2(f1, gx); axes(handles.axes12); imshow(uint8(gx1)); title('均值濾波器-灰度圖像處理');% --- 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) global im; x = im; % 讀取圖片:將圖像數據讀取到Matlab環境中 gx = rgb2gray(x);F = fspecial('average', 66); % F = fspecial('prewitt', Size); y = filter2(F, gx); xy = double(gx) - y; % 進行數據轉換,保證數據類型一致;高頻圖、輪廓、邊緣 sharpx = double(gx) + xy; % 原圖+輪廓:清晰度更高axes(handles.axes13); imshow(uint8(sharpx)); title('average濾波器圖像銳化');當時寫了很久...
自己電腦💻上的Matlab沒辦法連接攝像頭,有點小遺憾...
總結
以上是生活随笔為你收集整理的Matlab图像处理创新实践-实验4【综合设计大实验——GUI综合设计】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bootstrap4+MySQL前后端综
- 下一篇: Bootstrap4+MySQL前后端综