matlab GUI界面設計

【實驗內容】

用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。

編寫代碼完成程序中的變量賦值、輸入、輸出等工作,打開對應文件,在對應函數位置添加如下程序,其他代碼不變。

function varargout = m240531(varargin)
% M240531 MATLAB code for m240531.fig
%      M240531, by itself, creates a new M240531 or raises the existing
%      singleton*.
%
%      H = M240531 returns the handle to a new M240531 or the handle to
%      the existing singleton*.
%
%      M240531('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in M240531.M with the given input arguments.
%
%      M240531('Property','Value',...) creates a new M240531 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before m240531_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to m240531_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 m240531% Last Modified by GUIDE v2.5 31-May-2024 11:43:02% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...'gui_Singleton',  gui_Singleton, ...'gui_OpeningFcn', @m240531_OpeningFcn, ...'gui_OutputFcn',  @m240531_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 m240531 is made visible.
function m240531_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 m240531 (see VARARGIN)% Choose default command line output for m240531
handles.output = hObject;% Update handles structure
guidata(hObject, handles);% UIWAIT makes m240531 wait for user response (see UIRESUME)
% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.
function varargout = m240531_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 rgbtogray.
function rgbtogray_Callback(hObject, eventdata, handles)
% hObject    handle to rgbtogray (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
gray=rgb2gray(handles.img)
set(handles.gray_image, 'HandleVisibility', 'ON');
axes(handles.gray_image)
imshow(gray);
handles.img=gray;
guidata(hObject,handles);function thresh_value_Callback(hObject, eventdata, handles)
% hObject    handle to thresh_value (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 thresh_value as text
%        str2double(get(hObject,'String')) returns contents of thresh_value as a double% --- Executes during object creation, after setting all properties.
function thresh_value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to thresh_value (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 selection change in direction.
function direction_Callback(hObject, eventdata, handles)
% hObject    handle to direction (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)% Hints: contents = cellstr(get(hObject,'String')) returns direction contents as cell array
%        contents{get(hObject,'Value')} returns selected item from direction% --- Executes during object creation, after setting all properties.
function direction_CreateFcn(hObject, eventdata, handles)
% hObject    handle to direction (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called% Hint: popupmenu 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% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------
function Untitled_3_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------
function Sobel_Callback(hObject, eventdata, handles)
% hObject    handle to Sobel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
v1=str2double(get(handles.thresh_value, 'string' ));
contents=get(handles.direction,'string');
v2=contents{(get(handles.direction,'value'))};
edge_sobel=edge(handles.img,'sobel',v1,v2);
set(handles.test_image, 'HandleVisibility','ON');
axes(handles.test_image);
imshow(edge_sobel)
handles.img=edge_sobel;
guidata(hObject,handles);% --------------------------------------------------------------------
function Prewitt_Callback(hObject, eventdata, handles)
% hObject    handle to Prewitt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
v1=str2double(get(handles.thresh_value,'string'))
contents=get(handles.direction, 'string');
v2=contents{(get(handles.direction,'value' ))};
edge_prewitt=edge(handles.img, 'prewitt' ,v1,v2);
set(handles.test_image, 'HandleVisibility','ON');
axes(handles.test_image);
imshow(edge_prewitt)
handles.img=edge_prewitt;
guidata(hObject,handles);% --------------------------------------------------------------------
function Roberts_Callback(hObject, eventdata, handles)
% hObject    handle to Roberts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
v1=str2double(get(handles.thresh_value,'string'))
contents=get(handles.direction, 'string');
v2=contents{(get(handles.direction,'value'))};
edge_roberts=edge(handles.img, 'roberts' ,v1,v2);
set(handles.test_image, 'HandleVisibility','ON');
axes(handles.test_image);
imshow(edge_roberts)
handles.img=edge_roberts;
guidata(hObject,handles);% --------------------------------------------------------------------
function Canny_Callback(hObject, eventdata, handles)
% hObject    handle to Canny (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
v1=str2double(get(handles.thresh_value, 'string'));
contents=get(handles.direction,'string');
v2=contents{(get(handles.direction,'value'))};
edge_canny=edge(handles.img,'canny',v1, v2);
set(handles.test_image, 'HandleVisibility','ON');
axes(handles.test_image);
imshow(edge_canny);
handles.img=edge_canny;
guidata(hObject,handles);% --------------------------------------------------------------------
function open_Callback(hObject, eventdata, handles)
% hObject    handle to open (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename,pathname] = uigetfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'載入圖像');
if isequal(filename,0) || isequal(pathname,0)errordlg('沒有選中文件','出錯');return;
elsefile=[pathname,filename];global SS=file;x=imread(file);set(handles.original_image,'HandleVisibility','ON');axes(handles.original_image);imshow(x);handles.img=x;guidata(hObject,handles);
end% --------------------------------------------------------------------
function save_Callback(hObject, eventdata, handles)
% hObject    handle to save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[sfilename,sfilepath]=uiputfile({'*.jpg';'*.bmp';'*.tif';'*.*'}, ...'保存圖像文件','untitled.jpg');
if ~isequal([sfilename,sfilepath],[0,0])sfileullname = [sfilepath,sfilename];imwrite(handles.img, sfilefullname);
elsemsgbox('你按了取消鍵','保存失敗');
end% --------------------------------------------------------------------
function exit_Callback(hObject, eventdata, handles)
% hObject    handle to exit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
  1. 執行程序后,單擊菜單欄中的文件,打開圖片,在原圖位置會顯示彩色圖像,單擊“灰度轉換”按鈕,在灰度圖像位置會顯示轉換后的灰度圖像,在“設定閾值”框輸入0.1,選擇“檢測方向”為both,再在“檢測方法”菜單中選擇Canny,即可在“檢測圖像”的位置顯示邊緣檢測后的圖像,最后在“文件”菜單中選擇“保存”,即可保存最終分割后的邊緣檢測圖。

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/20223.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/20223.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/20223.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

3-哈希表-21-兩個數組的交集-LeetCode349

3-哈希表-21-兩個數組的交集-LeetCode349 參考:代碼隨想錄 LeetCode: 題目序號349 更多內容歡迎關注我(持續更新中,歡迎Star?) Github:CodeZeng1998/Java-Developer-Work-Note 技術公眾號:CodeZeng1998&…

2.1 OpenCV隨手簡記(二)

為后續項目學習做準備,我們需要了解LinuxOpenCV、Mediapipe、ROS、QT等知識。 一、圖像顯示與保存 1、基本原理 1.1 圖像像素存儲形式 首先得了解下圖像在計算機中存儲形式:(為了方便畫圖,每列像素值都寫一樣了)。對于只有黑白顏色的灰度…

[有監督學習]2.詳細圖解正則化

正則化 正則化是防止過擬合的一種方法,與線性回歸等算法配合使用。通過向損失函數增加懲罰項的方式對模型施加制約,有望提高模型的泛化能力。 概述 正則化是防止過擬合的方法,用于機器學習模型的訓練階段。過擬合是模型在驗證數據上產生的誤…

Java文件IO

White graces:個人主頁 🙉專欄推薦:Java入門知識🙉 🙉 內容推薦:JUC常見類🙉 🐹今日詩詞:東風吹柳日初長,雨馀芳草斜陽🐹 ??點贊 ??收藏??關注💬卑微小博主&…

Three.js 研究:4、創建設備底部旋轉的科技感圓環

1、實現效果 2、PNG轉SVG 2.1、原始物料 使用網站工具https://convertio.co/zh/png-svg/進行PNG轉SVG 3、導入SVG至Blender 4、制作旋轉動畫 4.1、給圓環著色 4.2、修改圓環中心位置 4.3、讓圓環旋轉起來 參考一下文章 Three.js 研究:1、如何讓物體動起來 Thre…

LeetCode # 1070. 產品銷售分析 III

1070. 產品銷售分析 III 題目 銷售表 Sales: ------------------ | Column Name | Type | ------------------ | sale_id | int | | product_id | int | | year | int | | quantity | int | | price | int | ------------------ (sale_id, year) 是這張表的主鍵&am…

“論SOA在企業集成架構設計中的應用”必過模板,突擊2024軟考高項論文

考題部分 企業應用集成(Enterprise Application Integration, EAI)是每個企業都必須要面對的實際問題。面向服務的企業應用集成是一種基于面向服務體系結構(Service-OrientedArchitecture,SOA)的新型企業應用集成技術,強調將企業和組織內部的資源和業務功…

VSCode界面Outline只顯示類名和函數名,隱藏變量名

參考鏈接 https://blog.csdn.net/Zjhao666/article/details/120523879https://blog.csdn.net/Williamcsj/article/details/122401996 VSCode中界面左下角的Outline能夠方便快速跳轉到文件的某個類或函數,但默認同時顯示變量,導致找某個函數時很不方便。…

mimkatz獲取windows10明文密碼

目錄 mimkatz獲取windows10明文密碼原理 lsass.exe進程的作用 mimikatz的工作機制 Windows 10的特殊情況 實驗 實驗環境 實驗工具 實驗步驟 首先根據版本選擇相應的mimikatz 使用管理員身份運行cmd 修改注冊表 ?編輯 重啟 重啟電腦后打開mimikatz 在cmd切換到mi…

Seq2Seq模型:詳述其發展歷程、深遠影響與結構深度剖析

Seq2Seq(Sequence-to-Sequence)模型是一種深度學習架構,專為處理從一個輸入序列到一個輸出序列的映射任務設計。這種模型最初應用于機器翻譯任務,但因其靈活性和有效性,現已被廣泛應用于自然語言處理(NLP&a…

醫院該如何應對網絡安全?

在線醫生咨詢受到很多人的關注,互聯網醫療行業的未來發展空間巨大,但隨著醫院信息化建設高速發展 醫院積累了大量的患者基本信息、化驗結果、電子處方、生產數據和運營信息等數據 這些數據涉及公民隱私、醫院運作和發展等多因素,醫療行業辦…

【QEMU中文文檔】1.關于QEMU

本文由 AI 翻譯(ChatGPT-4)完成,并由作者進行人工校對。如有任何問題或建議,歡迎聯系我。聯系方式:jelin-shoutlook.com。 QEMU 是一款通用的開源機器仿真器和虛擬化器。 QEMU 可以通過幾種不同的方式使用。最常見的用…

OrangePi AIpro--新手上路

目錄 一、SSH登錄二、安裝VNC Sevice(經測試Xrdp遠程桌面安裝不上)2.1安裝xface桌面2.2 配置vnc服務2.2.1 設置vnc server6-8位的密碼2.2.2 創建vnc文件夾,寫入xstartup文件2.2.3 給xstartup文件提高權限2.2.4 在安裝產生的vnc文件夾創建xsta…

C# 工廠模式學習

工廠模式(Factory Pattern)是一種創建型設計模式,它提供了一種創建對象的接口,而不是通過具體類來實例化對象。工廠模式可以將對象的創建過程封裝起來,使代碼更具有靈活性和可擴展性。 工廠模式有幾種常見的實現方式&…

Go 如何通過 Kafka 客戶端庫 生產與消費消息

文章目錄 0.前置說明1. confluent-kafka-go2. sarama3. segmentio/kafka-go4. franz-go選擇建議 1.啟動 kafka 集群2.安裝 confluent-kafka-go 庫3.創建生產者特殊文件說明如何查看.log文件內容 4.創建消費者 0.前置說明 Go 語言中有一些流行的 Kafka 客戶端庫。以下是幾個常用…

【Uniapp小程序】自定義導航欄uni-nav-bar滾動漸變色

效果圖 新建activityScrollTop.js作為mixins export default {data() {return {navBgColor: "rgba(0,0,0,0)", // 初始背景顏色為完全透明navTextColor: "rgba(0,0,0,1)", // 初始文字顏色};},onPageScroll(e) {// 設置背景const newAlpha Math.min((e.s…

踩坑:6年后為何不用GraphQL了?

GraphQL 是一項令人難以置信的技術,自從我在 2018 年首次開始將其投入生產以來,它就吸引了很多人的注意力。 在一大堆無類型的 JSON REST API 上構建了許多 React SPA 之后,我發現 GraphQL 是一股清新的空氣。 然而,隨著時間的推…

mybatis用map接收返回對象,不想讓數據類型為tinyint自動轉換為boolean,如何處理

在 MyBatis 中,當使用 Map 來接收查詢結果時,MyBatis 會根據列的數據類型自動選擇合適的 Java 類型來映射這些值。默認情況下,如果數據庫列是 TINYINT(1),MyBatis 可能會錯誤地將其映射為 boolean,因為它經常被誤解為只…

PPP認證兩種:PAP和CHAP,兩次握手和三次握手

CHAP(Challenge-Handshake Authentication Protocol,質詢握手認證協議)的設計理念是增強網絡認證過程的安全性。在CHAP的三次握手過程中,不直接傳送用戶的明文密碼,以此來提高安全性,具體步驟如下&#xff…

開源大模型源代碼

開源大模型的源代碼可以在多個平臺上找到,以下是一些知名的開源大模型及其源代碼的獲取方式: 1. **艾倫人工智能研究所的開放大語言模型(Open Language Model,OLMo)**: - 提供了完整的模型權重、訓練代…