Matlab图像处理GUI程序设计(滤波,傅里叶变换,翻转,旋转,二值化,动态打开图像,保存图像)

目录

一、整体界面

 二、界面控件说明

三、注意事项说明

四、源码打包领取方式

五、源码展示


一、整体界面

 二、界面控件说明

用到的控件只有

坐标轴axes

按钮button

单选按钮

面板

按钮组

滚动条

具体分布如图

三、注意事项说明

只有一个文件,写了所有代码

需要注意的是,全局变量

global fpath

全文名称要一致,否则会读取错误

以及显示处理后的图片的Tag要记住,比如我的是show_proImg ,在代码中如果看到这个名字,记得替换成自己的

快速定位控件函数的方法是,左键选中控件,右键弹出的选项中,选择查看回调,(一般是选择callback,只有按钮组的函数是选择最后一个SelectChangFcn)

面板不用添加代码,只是起到装饰的作用

四、源码打包领取方式

点赞收藏之后私信我领取(白嫖方式)

也可以戳此链接领取

获取打包好的源码

五、源码展示

% --- Executes on button press in openImg.
function openImg_Callback(hObject, eventdata, handles)%菜单栏导入图片的控件
% hObject    handle to openImg (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% axes(handles.show_sourImg);%导入图片到框1
% image=imread('fruit.jpg');%导入flower图片
% imshow(image);
% title('yy')
%支持用户自己选定图片,任何格式的图片
%%选定axes show_sourImg
% [filename,pathname]=uigetfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'载入图像');
%    if isequal(filename,0)||isequal(pathname,0)
%       errordlg('没有选中文件','出错');
%    return;
%    else 
%    file=[pathname,filename];
%    end
% pic = imread(file);
% axes(handles.show_sourImg);
% imshow(pic);
% title('原始图像','color','b');
 [filename,pathname]=uigetfile(...
        {'*.bmp;*.jpg;*.png;*.jpeg','ImageFiles(*.bmp,*.jpg,*.png,*.jpeg)';...
            '*.*','AllFiles(*.*)'},...
            'Pickanimage');
    axes(handles.show_sourImg);
    global fpath;%这个全局变量很重要,事关程序时候能正常运行,后续的全局变量也要用这个名字
    fpath=[pathname filename];
    img_1=imread(fpath);
    imshow(img_1);
    title('原始图像');

% --- Executes on button press in button_huidu.
function button_huidu_Callback(hObject, eventdata, handles)%工具按钮 灰度变化
% hObject    handle to button_huidu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% global im
% axes(handles.show_proImg);
% y=rgb2gray(im);%彩色转灰色
% imshow(y);
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
imshow(img_2);
title('灰度图')

% --- Executes on button press in saveImg.
function saveImg_Callback(hObject, eventdata, handles)%菜单保存图片
% hObject    handle to saveImg (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% [filename,pathname] = uiputfile({'*.jpg','JPEG(*.jpg)';...
%                                                       '*.bmp','Bitmap(*.bmp)';...
%                                                       '*.gif','GIF(*.gif)';...
%                                                       '*.*',  'All Files (*.*)'},...
%                                                       'Save Picture','Untitled');
% if filename==0&&handles.show_proImg==null%如果没有文件名或者axes中没有内容,不保存
%     errordlg('未生成文件,无法保存','出错');
%     return;
% else
%     h=getframe(handles.show_proImg);
%     imwrite(h.cdata,[pathname,filename]);
% end;
% %方法二保存
% new_f_handle=figure('visible','off');
% new_axes=copyobj(handles.show_proImg,new_f_handle);
% set(new_axes,'units','default','position','default');
% [filename,pathname,fileindex]=uiputfile({'*.jpg';'*.bmp';'*.png';'*.tif'},'save picture as');%图片另存为
% if ~filename
%     return
% else
%     file=strucat(pathname,filename);
% switch fileindex
%     case 1
%         print(new_f_handle,'-djpeg',file);
%     case 2
%         print(new_f_handle,'-dbmp',file);
%     case 3
%         print(new_f_handle,'-dpng',file);
%     case 4
%         print(new_f_handle,'-dtif',file);
% end
% end
% delete(new_f_handle);
%方法三保存
    [filename,pathname]=uiputfile({'*.bmp','BMPfiles';'*.jpg;','JPGfiles'},'PickanImage');
        if isequal(filename,0)||isequal(pathname,0)
            return;
        else
            h=getframe(handles.show_proImg);  %保存处理后的图片
            imwrite(h.cdata,[pathname,filename]);  
        end

% --- Executes when figure1 is resized.
function figure1_SizeChangedFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in clearAll.
function clearAll_Callback(hObject, eventdata, handles)%菜单清空
% hObject    handle to clearAll (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    delete(allchild(handles.show_sourImg));%删除处理前导入的图像
    delete(allchild(handles.show_proImg));%删除处理后图像
end


% --- Executes on button press in button_mohu.
function button_mohu_Callback(hObject, eventdata, handles)%模糊图片
% hObject    handle to button_mohu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in button_jiaoyan.
function button_jiaoyan_Callback(hObject, eventdata, handles)%添加椒盐噪声
% hObject    handle to button_jiaoyan (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
img_2=imnoise(img_2,'salt & pepper',0.06);
imshow(img_2);
title('加入椒盐噪声后')


% --- Executes on button press in button_yan.
function button_yan_Callback(hObject, eventdata, handles)%废弃
% hObject    handle to button_yan (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in button_xuanzhuan.
function button_xuanzhuan_Callback(hObject, eventdata, handles)
% hObject    handle to button_xuanzhuan (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
img_2=imrotate(img_2,90,'nearest');
imshow(img_2);
 title('旋转90度')



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 butoon_bosong.
function butoon_bosong_Callback(hObject, eventdata, handles)%泊松噪声
% hObject    handle to butoon_bosong (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.show_proImg); 
global fpath;
img_2=imread(fpath);
img_2 = imnoise(img_2,'poisson');
cla;
imshow(img_2);
guidata(hObject,handles);
title('添加泊松噪声');


% --- Executes on button press in button_JunHeng.
function button_JunHeng_Callback(hObject, eventdata, handles)%直方图均衡
% hObject    handle to button_JunHeng (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
K=16;
img_2=histeq(img_2,K);
imshow(img_2);
title('直方图均衡')


% --- Executes on button press in button_TongJi.
function button_TongJi_Callback(hObject, eventdata, handles)%直方图统计
% x=x(::);
% horz=::;
% bar(horz,x);
% %axis([]);
% set(handlesaxes,'xtick',::);
% %set(handlesaxes,'ytick',::);
% else
%    msgbox('这是灰度图像','旋转失败');
% end
% functionjunheng_Callback(hObject,eventdata,JFJHJFJFGJHFJFJGJDHJ
% handlesnoise_img=y;
% guidata(hObject,handles);
global fpath;
axes(handles.show_proImg);
x=imhist(fpath);
x1=x(1:10:256);
horz=1:10:256;
bar(horz,x1);
axis([0,255 0 15000]);
set(handles.show_proImg,'xtick',0:50:255);
set(handles.show_proImg,'ytick',0:2000:15000);




% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)%滚动条的代码
a=get(handles.slider1,'value');
x1=imadjust(im,[0.3 0.7],[0 1],a);
axes(handles.show_proImg);
imshow(x1);

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in JieTu.
function JieTu_Callback(hObject, eventdata, handles)%菜单截图
% hObject    handle to JieTu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% global fpath;
% axes(handles.show_proImg);%处理后图片的axes
% fpath=getimage;%获取图片
% x=imcrop(handles.show_proImg);%截图
% imshow(x);%显示
% handles.axces=x;
% guidata(hObject,handles);
%需要截图的数量
cant=ml_GetPrivateProfileString('COUNT','count','D:\Desktop\MATlab\MY\程序设计\jmageProcess');


% --- Executes on button press in pushbutton21.
function pushbutton21_Callback(hObject, eventdata, handles)%加入高斯噪声
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
img_2=imnoise(img_2,'gaussian');
imshow(img_2);
title('加入高斯噪声')


% --- Executes on button press in pushbutton22.
function pushbutton22_Callback(hObject, eventdata, handles)%乘性噪声
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
handles.img = imnoise(img_2,'speckle',0.04);
cla; imshow(handles.img);
guidata(hObject,handles);
title('乘性噪声');


% --- Executes on button press in pushbutton37.
function pushbutton37_Callback(hObject, eventdata, handles)%傅里叶变换(频谱图)
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
img_2=im2double(img_2);
img_2=fft2(img_2);
img_2=fftshift(img_2);
img_2=abs(img_2);
img_2=log(img_2+1);
imshow(img_2,[]);
title('傅里叶变换(频谱图)')


% --- Executes on button press in pushbutton38.
function pushbutton38_Callback(hObject, eventdata, handles)%加入噪声后的傅里叶变换(频谱图)
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
img_2=imnoise(img_2,'salt & pepper',0.06);
img_2=im2double(img_2);
img_2=fft2(img_2);
img_2=fftshift(img_2);
img_2=abs(img_2);
img_2=log(img_2+1);
imshow(img_2,[]);
title('加入噪声后的傅里叶变换(频谱图)')


% --- Executes on button press in pushbutton33.
function pushbutton33_Callback(hObject, eventdata, handles)% 图像处理 旋转
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
img_2=imrotate(img_2,90,'nearest');
imshow(img_2);
title('旋转90度')


% --- Executes on button press in pushbutton34.
function pushbutton34_Callback(hObject, eventdata, handles)%图像处理 灰度变化
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
imshow(img_2);
title('灰度图')


% --- Executes on button press in pushbutton35.
function pushbutton35_Callback(hObject, eventdata, handles)%图像处理 二值化
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=im2bw(img_2,0.5);%尽管im2bw报红 还是用这个
imshow(img_2);
title('二值化图像')



% --- Executes on button press in pushbutton36.
function pushbutton36_Callback(hObject, eventdata, handles)%图像处理 去噪
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
img_2= imnoise(img_2,'salt & pepper',0.1); %加10%的椒盐
axes(handles.show_proImg);
imshow(img_2);
img_2=medfilt2(img_2);
axes(handles.show_proImg);
imshow(img_2);
title('去噪');


% --- Executes on button press in pushbutton29.
function pushbutton29_Callback(hObject, eventdata, handles)%二值图细化
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
I = imread(img_2);
J = bwmorph(I,'thin',Inf);%细化
imshow(J);

% --- Executes on button press in pushbutton31.
function pushbutton31_Callback(hObject, eventdata, handles)%开运算
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
SE= strel('square',20);               % 结构元素
result = imopen(img_2,SE);         % 开运算
imshow(result);
title('开运算后的图像');


% --- Executes on button press in pushbutton32.
function pushbutton32_Callback(hObject, eventdata, handles)%闭运算
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
SE= strel('square',20);               % 结构元素
result = imclose(img_2,SE);         % 开运算
imshow(result);
title('闭运算后的图像');


% --- Executes on button press in pushbutton25.
function pushbutton25_Callback(hObject, eventdata, handles)%均值滤波
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
h=fspecial('average');
handles.img=imfilter(img_2,h,'replicate');
cla; imshow(handles.img)
guidata(hObject,handles);
title('均值滤波');

% --- Executes on button press in pushbutton26.
function pushbutton26_Callback(hObject, eventdata, handles)%高斯滤波
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
hsize=[8 8]; 
sigma=1.7;
h=fspecial('gaussian',hsize,sigma);
handles.img=imfilter(img_2,h,'replicate');
cla; 
imshow(img_2);
guidata(hObject,handles);
title('高斯滤波')


% --- Executes on button press in pushbutton27.
function pushbutton27_Callback(hObject, eventdata, handles)%中值滤波
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
r=medfilt2(img_2(:,:,1));
g=medfilt2(img_2(:,:,2));
b=medfilt2(img_2(:,:,3)); 
handles.img=cat(3,r,g,b);
cla; 
imshow(handles.img);
guidata(hObject,handles);
title('中值滤波')


% --- Executes on button press in radio_canny.
function radio_canny_Callback(hObject, eventdata, handles)%canny边缘检测
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
img_2=edge(img_2,'canny');
imshow(img_2,[]);
title('canny边缘检测')


% --- Executes on button press in radio_sobel.
function radio_sobel_Callback(hObject, eventdata, handles)%Sobel边缘检测
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
img_2=edge(img_2,'sobel');
imshow(img_2,[]);
title('Sobel边缘检测')


% --- Executes on button press in radio_roberts.
function radio_roberts_Callback(hObject, eventdata, handles)%Roberts边缘检测
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
img_2=edge(img_2,'roberts');
imshow(img_2,[]);
title('Roberts边缘检测')


% --- Executes on button press in radio_log.
function radio_log_Callback(hObject, eventdata, handles)%Log边缘检测
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
img_2=rgb2gray(img_2);
img_2=edge(img_2,'log');
imshow(img_2,[]);
title('Log边缘检测')


% --- Executes on button press in pushbutton41.
function pushbutton41_Callback(hObject, eventdata, handles)%退出
clc
clear
close(gcf)%关闭界面


% --- Executes on button press in pushbutton42.
function pushbutton42_Callback(hObject, eventdata, handles)%图像反色
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
x=img_2;
r=x(:,:,1); r=256-r;
g=x(:,:,2); g=256-g;
b=x(:,:,3); b=256-b;
handles.img=cat(3,r,g,b);
cla; 
imshow(handles.img);
guidata(hObject,handles);


% --- Executes on button press in pushbutton43.
function pushbutton43_Callback(hObject, eventdata, handles)%上下翻转
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
handles.img=flipud(img_2);%上下翻转
cla;
imshow(handles.img);
guidata(hObject,handles);


% --- Executes on button press in pushbutton44.
function pushbutton44_Callback(hObject, eventdata, handles)%左右翻转
axes(handles.show_proImg);
global fpath;
img_2=imread(fpath);
handles.img=fliplr(img_2);%左右翻转
cla;
imshow(handles.img);
guidata(hObject,handles);


% --- Executes on button press in pushbutton45.
function pushbutton45_Callback(hObject, eventdata, handles)%截图
axes(handles.show_proImg);
global fpath;
fpath=getimage;
x=imcrop(handles.show_proImg);%截图
imshow(x);
guidata(hObject,handles);

% --- Executes when selected object is changed in uibuttongroup2.
function uibuttongroup2_SelectionChangedFcn(hObject, eventdata, handles)%放大缩小 按钮组
% hObject    handle to the selected object in uibuttongroup2 
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fpath;
str=get(hObject,'string');
axes(handles.show_proImg);
switch str
    case'最邻近插值'
        fpath=getimage;
        prompt={'输入参数'};
        defans={'2'};
        p=inputdlg('prompt','input',1,defans);
        p1=str2num(p{1});
        f=imresize(im,p1,'nearest');
        imshow(f);
        fpath=f;
        guidata(hObject,handles);
    case '双线性插值'
        fapath=getimage;
        prompt={'输入参数:'};
        defans={'1'};
        p=inputdlg('prompt','input',1,defans);
        p1=str2num(p{1});
        f=imresize(fpath,p1,'bilinear');
        imshow(f);
        fpath=f;
        guidata(hObject,handles);
end

评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值