Matlab实用GUI完成图像处理

源码地址
https://gitee.com/niusongcun/matlab-gui.git

实现效果

在这里插入图片描述
在这里插入图片描述
GUI界面所实现的功能如下。
在这里插入图片描述

源码

function varargout = gui(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_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

function gui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);

function varargout = gui_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;

% --------------------------------------------------------------------
%文件
function open_Callback(hObject, eventdata, handles)%打开图片
global im   %定义一个全局变量im
global im2
[filename,pathname]=...
    uigetfile({'*.*';'*.bmp';'*.tif';'*.png'},'select picture');  %选择图片路径
str=[pathname filename];  %合成路径+文件名
im=imread(str);   %读取图片
im2=im;
axes(handles.axes1);  %使用第一个axes
imshow(im);  %显示图片

function save_Callback(hObject, eventdata, handles)%保存图片
global BW 
set(handles.axes2,'HandleVisibility','ON');
axes(handles.axes2);
[filename,pathname]=uiputfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'save image as');
file=strcat(pathname,filename);
BW=getimage(gca);
imwrite(BW,file);
set(handles.axes2,'HandleVisibility','Off');
function quit_Callback(hObject, ~, handles)%退出操作
close(gcf)  %关闭当前Figure窗口句柄

% --------------------------------------------------------------------

%开启摄像头
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
global V;
winvideoinfo = imaqhwinfo('winvideo');
V = videoinput('winvideo', 1);
V_Res = get(V, 'VideoResolution');
V_Bands = get(V, 'NumberOfBands');
axes(handles.axes1);
%h = image(zeros(V_Res(2), V_Res(1), V_Bands));
h = imshow(zeros(V_Res(2), V_Res(1), V_Bands));
preview(V, h);

%捕获图片
function pushbutton2_Callback(hObject, eventdata, handles)
global V;
global im;
im = getsnapshot(V) ;
axes(handles.axes1);
imshow(im);
% --------------------------------------------------------------------

%菜单栏的调回函数,实际不使用
function t1_Callback(hObject, eventdata, handles)

function t2_Callback(hObject, eventdata, handles)

function t3_Callback(hObject, eventdata, handles)

function t4_Callback(hObject, eventdata, handles)

function t5_Callback(hObject, eventdata, handles)

function t6_Callback(hObject, eventdata, handles)

function t7_Callback(hObject, eventdata, handles)

function t8_Callback(hObject, eventdata, handles)

function t9_Callback(hObject, eventdata, handles)


% --------------------------------------------------------------------
% 图像类型变换
function rgb2gray_Callback(hObject, eventdata, handles)%原图-灰度
global im
global BW  %定义全局变量  
axes(handles.axes2); 
BW=rgb2gray(im);
im=BW;
imshow(BW);

function im2bw_Callback(hObject, eventdata, handles)%原图-二值
global im
global BW  %定义全局变量  
axes(handles.axes2); 
BW=im2bw(im);
im=BW;
imshow(BW);

function dither_Callback(hObject, eventdata, handles)%灰度-二值
global im
global BW  %定义全局变量  
axes(handles.axes2); 
BW=dither(im);
im=BW;
imshow(BW);

% --------------------------------------------------------------------
% 边缘检测
function roberts_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
BW=edge(im,'roberts',0.04);
imshow(BW);

function sobel_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
BW=edge(im,'sobel',0.04);
imshow(BW);

function prewitt_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
BW=edge(im,'prewitt',0.04);
imshow(BW);

function log_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
BW=edge(im,'log',0.003);
imshow(BW);

% --------------------------------------------------------------------
%图像变换
function DFT_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2); 
I1=double(im);
I2=fft2(I1);
I3=fftshift(I2);
I3=log(abs(I3));
BW=I3;
imshow(BW,[]);

function DCT_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2); 
I1=double(im);
I2=dct2(I1);
I3=log(abs(I2));
BW=I3;
imshow(BW);

% --------------------------------------------------------------------
% 图像旋转
function rotate_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量 
% A=getimage(handles.axes1);
A=im;
axes(handles.axes2); 
prompt={'度数:'};
def={'90'};
answer=inputdlg(prompt,'请输入:',1,def);
if ~isempty(answer)
a = str2num(answer{1});
J=imrotate(A,360-a);
BW=J;
imshow(BW);
end

function Initial_Callback(hObject, eventdata, handles)%初始化
global im
global im2
global BW  %定义全局变量 
BW=im2;
im=im2;
axes(handles.axes2); 
imshow(BW);

% --------------------------------------------------------------------
% 图像噪声添加
function gaussian_Callback(hObject, eventdata, handles)

global im
global BW  %定义全局变量 
%获取输入框中的值
gui_handles = guihandles(gcf);
gsize = get(gui_handles.gaus_edit1,'String');
gsigma = get(gui_handles.gaus_edit2,'String');
I=im2double(im);
f = fspecial('gaussian',str2num(gsize),str2num(gsigma));
J = imfilter(im,f);
BW=J;
axes(handles.axes2); 
imshow(BW);

function salt_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量 
%获取输入框中的值
gui_handles = guihandles(gcf);
density = get(gui_handles.jiaoyan_edit,'String');
I=im2double(im);
J=imnoise(I,'salt & pepper',str2num(density));
BW=J;
axes(handles.axes2); 
imshow(BW);

function speckle_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量 
%获取输入框中的值
gui_handles = guihandles(gcf);
sigma = get(gui_handles.spe_edit,'String');
I=im2double(im);
J=imnoise(I,'speckle',str2num(sigma));
BW=J;
axes(handles.axes2); 
imshow(BW);

function poisson_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量 
I=im2double(im);
J=imnoise(I,'poisson');
BW=J;
axes(handles.axes2); 
imshow(BW);

% --------------------------------------------------------------------
% 图像滤波
function medilt_Callback(hObject, eventdata, handles)%中值滤波
global BW  %定义全局变量 
J=medfilt2(BW, [3,3]);
BW=J;
axes(handles.axes2); 
imshow(BW);

function wiener_Callback(hObject, eventdata, handles)%自适应滤波
global BW  %定义全局变量 
J=wiener2(BW,[3,3]);
BW=J;
axes(handles.axes2); 
imshow(BW);

function filter2_Callback(hObject, eventdata, handles)%均值滤波
global BW  %定义全局变量 
M1=ones(3);
M1=M1/9;
J=filter2(M1,BW);
BW=J;
axes(handles.axes2); 
imshow(BW);

% --------------------------------------------------------------------
%图像灰度变化
function plotchange_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
A=im2double(im);
    a=0.3;%0.3 0.7 0.5 0.9
    b=0.7;
    c=0.1;
    d=0.9;
    %0.3 0.7 0.1 0.9
    B=A;
    [m,n]=size(B);
    Mg=max(max(B));
    Mf=max(max(A));
    for (i=1:m)
      for (j=1:n)
        if(A(i,j)>=0&&A(i,j)<=a)
             B(i,j)=(c/a)*A(i,j);
        end
        if(A(i,j)>=a&&A(i,j)<=b)
            B(i,j)=(((d-c)/(b-a))*(A(i,j)-a))+c;
        end
        if(A(i,j)>=b&&A(i,j)<=1)
             B(i,j)=(((Mg-d)/(Mf-b))*(A(i,j)-b))+d;
        end
      end
    end
   BW=B;
   imshow(BW);

function imhist_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
 BW=im;
 imhist(BW);
 
function histeq_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
BW=histeq(im);
imhist(BW);

% --------------------------------------------------------------------
function histeqafter_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
imshow(BW);

% --------------------------------------------------------------------
%以下为文本与参数
% --------------------------------------------------------------------
%高斯滤波参数
function gaus_edit1_Callback(hObject, eventdata, handles)
% hObject    handle to gaus_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 gaus_edit1 as text
%        str2double(get(hObject,'String')) returns contents of gaus_edit1 as a double


% --- Executes during object creation, after setting all properties.
function gaus_edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gaus_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');
end



function gaus_edit2_Callback(hObject, eventdata, handles)
% hObject    handle to gaus_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 gaus_edit2 as text
%        str2double(get(hObject,'String')) returns contents of gaus_edit2 as a double


% --- Executes during object creation, after setting all properties.
function gaus_edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gaus_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
% --------------------------------------------------------------------
%椒盐噪声参数
function jiaoyan_edit_Callback(hObject, eventdata, handles)
% hObject    handle to jiaoyan_edit (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 jiaoyan_edit as text
%        str2double(get(hObject,'String')) returns contents of jiaoyan_edit as a double


% --- Executes during object creation, after setting all properties.
function jiaoyan_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to jiaoyan_edit (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



function spe_edit_Callback(hObject, eventdata, handles)
% hObject    handle to spe_edit (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 spe_edit as text
%        str2double(get(hObject,'String')) returns contents of spe_edit as a double


% --- Executes during object creation, after setting all properties.
function spe_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to spe_edit (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

  • 8
    点赞
  • 118
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韭菜盖饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值