数字图像处理:matlab编写简易GUI软件

通过GUI界面创建简易GUI软件,命令窗口输入guide,打开untitled.fig进入控件编辑界面,运行进入使用界面,以彩色图像RGBimageB.tiff为例,实现九种功能:图片的打开;转换为灰度图像;增加对比度;图片旋转(逆时针);增加高斯噪声;添加椒盐噪声;消除噪声;图像增强;边缘检测(可选五种边缘检测算子)。


代码(matlab)

function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
%      UNTITLED, by itself, creates a new UNTITLED or raises the existing
%      singleton*.
%
%      H = UNTITLED returns the handle to a new UNTITLED or the handle to
%      the existing singleton*.
%
%      UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UNTITLED.M with the given input arguments.
%
%      UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before untitled_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled_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 untitled

% Last Modified by GUIDE v2.5 10-Jun-2021 11:13:53

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @untitled_OpeningFcn, ...
                   'gui_OutputFcn',  @untitled_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before untitled is made visible.
function untitled_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 untitled (see VARARGIN)

% Choose default command line output for untitled
%初始化
handles.output = hObject;
handles.imgfilename = [];
handles.imgdata = [];
handles.imgoutput = [];

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = untitled_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 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)
% 按钮一的运行代码(打开图片)
[imgfilename imgpathname]=uigetfile({'*.jpg;*.png;*.tiff;*.tif;*.bmp'},'Select a RGB image');
if imgfilename
    imgdata=imread([imgpathname '\' imgfilename]);
    image(handles.axes1,imgdata);
    handles.imgfilename=imgfilename;
    handles.imgdata=imgdata;    
end
guidata(hObject,handles)

% --- Executes on button press in pushbutton2.
function pushbutton2_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)
% 按钮二的运行代码(转换为灰度图像)
if ~isempty(handles.imgfilename)
    imgoutput=rgb2gray(handles.imgdata);
    image(handles.axes2,imgoutput)
    colormap(handles.axes2,gray(256))%灰度图像要加背景才能显示正常
    handles.imgoutput=imgoutput;
    handles.imgdata=imgoutput;%新图片存为图片数据,可对新图片进行下一步操作
end

guidata(hObject,handles)


% --- 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)
% 按钮三的运行代码(使用伽马变换增加对比度函数)
    I = im2double(handles.imgdata);
    I = mat2gray(I); % 归一化
    imgoutput = imadjust(I,[0.2 0.8],[0.1 1]);
    image(handles.axes2,imgoutput)
    handles.imgoutput=imgoutput;
    handles.imgdata=imgoutput;
guidata(hObject,handles)


% --- 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)
%按钮四的运行代码(逆时针旋转)
imgoutput = imrotate(handles.imgdata,90,'crop');
image(handles.axes2,imgoutput)
handles.imgoutput=imgoutput;
handles.imgdata=imgoutput;
guidata(hObject,handles)



% --- 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)
%按钮五的运行代码(添加高斯噪声)
imgoutput=imnoise(handles.imgdata,'gaussian',0,0.02);
image(handles.axes2,imgoutput)
handles.imgoutput=imgoutput;
handles.imgdata=imgoutput;
guidata(hObject,handles)


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%按钮六的运行代码(添加椒盐噪声)
imgoutput=imnoise(handles.imgdata,'salt & pepper');
image(handles.axes2,imgoutput)
handles.imgoutput=imgoutput;
handles.imgdata=imgoutput;
guidata(hObject,handles)


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%按钮七的运行代码(消除噪声)
[a,b,c]=ddencmp('den','wv',handles.imgdata);
imgoutput=wdencmp('gbl',handles.imgdata,'sym4',2,a,b,c);
imgoutput=uint8(imgoutput);
image(handles.axes2,imgoutput)
handles.imgoutput=imgoutput;
handles.imgdata=imgoutput;
guidata(hObject,handles)


% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%按钮八的运行代码(直方图均衡化实现图像增强)
imgoutput=histeq(handles.imgdata);
image(handles.axes2,imgoutput)
handles.imgoutput=imgoutput;
handles.imgdata=imgoutput;
guidata(hObject,handles)


% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%按钮九的运行代码(以sobel算子为例提取边缘)
I=rgb2gray(handles.imgdata);
imgoutput = edge(I,'sobel');
imagesc(handles.axes2,imgoutput);
%或者用image(handles.axes2,im2uint8(double(imgoutput)));语句,logical二值图像颜色范围太小,没法用image直接显示,
colormap(handles.axes2,gray(256));
handles.imgoutput=imgoutput;
guidata(hObject,handles)


%以下单选框中五个选项可以选择边缘检测算子类型(点击即生成图片)
% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
I=rgb2gray(handles.imgdata);
imgoutput = edge(I,'sobel'); 
imagesc(handles.axes2,imgoutput)
colormap(handles.axes2,gray(256));
handles.imgoutput=imgoutput;
guidata(hObject,handles)
% Hint: get(hObject,'Value') returns toggle state of radiobutton3


% --- Executes on button press in radiobutton4.
function radiobutton4_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
I=rgb2gray(handles.imgdata);
imgoutput = edge(I,'prewitt'); 
imagesc(handles.axes2,imgoutput)
colormap(handles.axes2,gray(256));
handles.imgoutput=imgoutput;
guidata(hObject,handles)
% Hint: get(hObject,'Value') returns toggle state of radiobutton4


% --- Executes on button press in radiobutton5.
function radiobutton5_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
I=rgb2gray(handles.imgdata);
imgoutput = edge(I,'log'); 
imagesc(handles.axes2,imgoutput)
colormap(handles.axes2,gray(256));
handles.imgoutput=imgoutput;
guidata(hObject,handles)
% Hint: get(hObject,'Value') returns toggle state of radiobutton5


% --- Executes on button press in radiobutton9.
function radiobutton9_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
I=rgb2gray(handles.imgdata);
imgoutput = edge(I,'canny'); 
imagesc(handles.axes2,imgoutput)
colormap(handles.axes2,gray(256));
handles.imgoutput=imgoutput;
guidata(hObject,handles)
% Hint: get(hObject,'Value') returns toggle state of radiobutton9


% --- Executes on button press in radiobutton10.
function radiobutton10_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
I=rgb2gray(handles.imgdata);
imgoutput = edge(I,'roberts'); 
imagesc(handles.axes2,imgoutput)
colormap(handles.axes2,gray(256));
handles.imgoutput=imgoutput;
guidata(hObject,handles)
% Hint: get(hObject,'Value') returns toggle state of radiobutton10

GUI界面

数字图像处理:matlab编写简易GUI软件_第1张图片

运行示例

数字图像处理:matlab编写简易GUI软件_第2张图片
数字图像处理:matlab编写简易GUI软件_第3张图片
数字图像处理:matlab编写简易GUI软件_第4张图片


补充

实验中实现边缘检测功能时,发现使用image函数显示图像出现全蓝的现象,需要改用imagesc函数才能正常显示,或使用image(handles.axes2,im2uint8(double(imgoutput)))语句,原因是logical二值图像颜色范围太小,如果使用image将没法直接显示。

你可能感兴趣的:(matlab,图像处理,matlab)