matlab GUI界面设计

2024-05-31 15:52
文章标签 matlab gui 界面设计

本文主要是介绍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,即可在“检测图像”的位置显示边缘检测后的图像,最后在“文件”菜单中选择“保存”,即可保存最终分割后的边缘检测图。

 

这篇关于matlab GUI界面设计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1018376

相关文章

Java 创建图形用户界面(GUI)入门指南(Swing库 JFrame 类)概述

概述 基本概念 Java Swing 的架构 Java Swing 是一个为 Java 设计的 GUI 工具包,是 JAVA 基础类的一部分,基于 Java AWT 构建,提供了一系列轻量级、可定制的图形用户界面(GUI)组件。 与 AWT 相比,Swing 提供了许多比 AWT 更好的屏幕显示元素,更加灵活和可定制,具有更好的跨平台性能。 组件和容器 Java Swing 提供了许多

matlab读取NC文件(含group)

matlab读取NC文件(含group): NC文件数据结构: 代码: % 打开 NetCDF 文件filename = 'your_file.nc'; % 替换为你的文件名% 使用 netcdf.open 函数打开文件ncid = netcdf.open(filename, 'NC_NOWRITE');% 查看文件中的组% 假设我们想读取名为 "group1" 的组groupName

利用matlab bar函数绘制较为复杂的柱状图,并在图中进行适当标注

示例代码和结果如下:小疑问:如何自动选择合适的坐标位置对柱状图的数值大小进行标注?😂 clear; close all;x = 1:3;aa=[28.6321521955954 26.2453660695847 21.69102348512086.93747104431360 6.25442246899816 3.342835958564245.51365061796319 4.87

C# double[] 和Matlab数组MWArray[]转换

C# double[] 转换成MWArray[], 直接赋值就行             MWNumericArray[] ma = new MWNumericArray[4];             double[] dT = new double[] { 0 };             double[] dT1 = new double[] { 0,2 };

Golang GUI入门——andlabs ui

官方不提供gui标准库,只好寻求第三方库。 https://github.com/google/gxui 这个gui库是谷歌内部人员提供的,并不是谷歌官方出品,现在停止维护,只好作罢。 第三方gui库 找了好多,也比较了好多,最终决定使用的是还是 https://github.com/andlabs/ui 相信golang gui还会发展的更好,期待更优秀的gui库 由于andlabs

libsvm在matlab中的使用方法

原文地址:libsvm在matlab中的使用方法 作者: lwenqu_8lbsk 前段时间,gyp326曾在论坛里问libsvm如何在matlab中使用,我还奇怪,认为libsvm是C的程序,应该不能。没想到今天又有人问道,难道matlab真的能运行libsvm。我到官方网站看了下,原来,真的提供了matlab的使用接口。 接口下载在: http://www.csie.ntu.edu.

Matlab/Simulink中PMSM模型的反电动势系数和转矩系数

Matlab/Simulink中PMSM模型的反电动势系数和转矩系数_matlab pmsm-CSDN博客

GUI编程08:画笔paint

本节内容视频链接:10、画笔paint_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1DJ411B75F?p=10&vd_source=b5775c3a4ea16a5306db9c7c1c1486b5 package com.yundait.lesson03;import java.awt.*;import java.awt.event.Wind

MATLAB层次聚类分析法

转自:http://blog.163.com/lxg_1123@126/blog/static/74841406201022774051963/ 层次聚类是基于距离的聚类方法,MATLAB中通过pdist、linkage、dendrogram、cluster等函数来完成。层次聚类的过程可以分这么几步: (1) 确定对象(实际上就是数据集中的每个数据点)之间的相似性,实际上就是定义一个表征

C++入门(06)安装QT并快速测试体验一个简单的C++GUI项目

文章目录 1. 清华镜像源下载2. 安装3. 开始菜单上的 QT 工具4. 打开 Qt Creator5. 简单的 GUI C++ 项目5.1 打开 Qt Creator 并创建新项目5.2 设计界面5.3 添加按钮的点击事件5.4 编译并运行项目 6. 信号和槽(Signals and Slots) 这里用到了C++类与对象的很多概念 1. 清华镜像源下载 https://