首先感谢提供给我参考的文章如下
(4条消息) 期末大作业之Matlab美图秀秀【GUI界面】_matlab gui界面美化_走运 ��的博客-CSDN博客
(4条消息) MATLAB图像处理简单程序(1)—实现几何、算数简单变换,滤镜处理以及图片变换效果展示_图像处理程序_火球2号的博客-CSDN博客
(4条消息) 基于MATLAB的数字图像处理系统设计_sunny_chenxi的博客-CSDN博客
(4条消息) 用matlab编程实现数字图像理想低通滤波、高斯低通滤波和巴特沃斯低通滤波去噪算法_matlab高斯低通滤波_紫色银杏树的博客-CSDN博客
还有b站的教程提供的基础思路
MATLAB中GUI图像处理附源码_哔哩哔哩_bilibili
本软件在以上参考基础上完成
目前效果只能适用于简单的图像
设计要求:
简易P图
设计并实现一款简单的P图软件,能够具备一些常用的P图功能。
要求完成以下工作:
1.1、图形界面
设计一个简单的图形界面,主要包含三个部分:上传图片部分,P图部分,保存结果部分。
1.2、上传图片/保存结果
通过控件控制,上传本地文件并在图形界面中显示;通过控件控制,将P好的图下载保存在本地。
1.3、P图功能
具备一些常用的P图功能,比如:彩色图像转黑白图像,明暗亮度变化,反色处理,磨皮,生成素描等。
源码:
%新建gui界面
function varargout = blackandwhite(varargin)
% BLACKANDWHITE MATLAB code for blackandwhite.fig
% BLACKANDWHITE, by itself, creates a new BLACKANDWHITE or raises the existing
% singleton*.
%
% H = BLACKANDWHITE returns the handle to a new BLACKANDWHITE or the handle to
% the existing singleton*.
%
% BLACKANDWHITE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BLACKANDWHITE.M with the given input arguments.
%
% BLACKANDWHITE('Property','Value',...) creates a new BLACKANDWHITE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before blackandwhite_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to blackandwhite_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 blackandwhite
% Last Modified by GUIDE v2.5 01-Jun-2023 16:41:51
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @blackandwhite_OpeningFcn, ...
'gui_OutputFcn', @blackandwhite_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 blackandwhite is made visible.初始化函数
function blackandwhite_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)
% 是一个以GUI中所有控件的Tag属性为字段的结构体,每个字段的取值就是对应控件的句柄.
%类似于C语言中指针,它是某个对象的唯一标识符,通过句柄就可以找到你需要的对象
% varargin command line arguments to blackandwhite (see VARARGIN)
% Choose default command line output for blackandwhite
handles.output = hObject;
set(handles.save,'Enable','off');
set(handles.exit,'Enable','off');
set(handles.reset,'Enable','off');
set(handles.slider1,'Enable','off');
set(handles.m1,'Enable','off');
set(handles.m2,'Enable','off');
set(handles.m3,'Enable','off');
set(handles.m4,'Enable','off');
set(handles.mopi,'Enable','off');
set(handles.sumiao,'Enable','off');
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes blackandwhite wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = blackandwhite_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 load.
function load_Callback(hObject, eventdata, handles)
% hObject handle to load (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file path]=uigetfile({'*.jpg';'*.bmp';'*.jpeg';'*.png'}, '打开文件');%uigetfile图像用户界面模块
image=[path file];
handles.file=image;
if (file==0)
warndlg('请选择一张图片...') ;
end
[fpath, fname, fext]=fileparts(file);
validex=({'.bmp','.jpg','.jpeg','.png'});
found=0;
for (x=1:length(validex))
if (strcmpi(fext,validex{x}))
found=1;
set(handles.save,'Enable','on');
set(handles.exit,'Enable','on');
set(handles.reset,'Enable','on');
set(handles.slider1,'Enable','on');
set(handles.m4,'Enable','on');
set(handles.m1,'Enable','on');
set(handles.m2,'Enable','on');
set(handles.m3,'Enable','on');
set(handles.mopi,'Enable','on');
set(handles.sumiao,'Enable','on');
handles.img=imread(image);
handles.i=imread(image);
h = waitbar(0,'等待...');
steps = 100;
for step = 1:steps
waitbar(step / steps)
end
close(h)
axes(handles.g1);
cla;
imshow(handles.img);
axes(handles.g2);
cla;
imshow(handles.img);
guidata(hObject,handles);
break;
end
end
if (found==0)
errordlg('文件扩展名不正确,请从可用扩展名[.jpg、.jpeg、.bmp、.png]中选择文件','Image Format Error');
end
%退出
% --- Executes on button press in exit.
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)
close all;
%保存
% --- Executes on button press in save.
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)
[file path]= uiputfile('*.jpg','Save Image as');
save=[path file]; imwrite(handles.img,save,'jpg');
%清除
% --- Executes on button press in reset.
function reset_Callback(hObject, eventdata, handles)
% hObject handle to reset (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.img=handles.i;
axes(handles.g2);
cla;
imshow(handles.img);
guidata(hObject,handles);
%磨皮
% --- Executes on button press in mopi.
function mopi_Callback(hObject, eventdata, handles)
% hObject handle to mopi (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
A=handles.i;%A:读取图像
r=2; %r:半径
T=10; %T:阈值
w=zeros(2*r+1,2*r+1); %模板矩阵的尺寸
%图像初始化处理
img=rgb2gray(A); %源图片转灰度图
img=double(img); %转为矩阵
R=double(A(:,:,1));
G=double(A(:,:,2));
B=double(A(:,:,3));
%解决边界值问题
[m,n]=size(R);
imgn=zeros(m+2*r,n+2*r); %创建一个长宽各增加[2r]的扩容矩阵
imgn(r+1:r+m,r+1:r+n)=R;
imgn(1:r,r+1:r+n)=R(1:r,1:n); %上边界填充
imgn(1:m+r,n+r+1:n+2*r)=imgn(1:m+r,n+1:n+r); %右边界填充
imgn(m+r+1:m+2*r,r+1:n+2*r)=imgn(m+1:m+r,r+1:n+2*r); %下边界填充
imgn(1:m+2*r,1:r)=imgn(1:m+2*r,r+1:2*r); %左边界填充
[m1,n1]=size(G);
imgn1=zeros(m1+2*r,n1+2*r); %创建一个长宽各增加[2r]的扩容矩阵
imgn1(r+1:r+m1,r+1:r+n1)=G;
imgn1(1:r,r+1:r+n1)=G(1:r,1:n1); %上边界填充
imgn1(1:m1+r,n1+r+1:n1+2*r)=imgn1(1:m1+r,n1+1:n1+r); %右边界填充
imgn1(m1+r+1:m1+2*r,r+1:n1+2*r)=imgn1(m1+1:m1+r,r+1:n1+2*r); %下边界填充
imgn1(1:m1+2*r,1:r)=imgn1(1:m1+2*r,r+1:2*r); %左边界填充
[m2,n2]=size(B);
imgn2=zeros(m2+2*r,n2+2*r); %创建一个长宽各增加[2r]的扩容矩阵
imgn2(r+1:r+m2,r+1:r+n2)=B;
imgn2(1:r,r+1:r+n2)=B(1:r,1:n2); %上边界填充
imgn2(1:m2+r,n2+r+1:n2+2*r)=imgn2(1:m2+r,n2+1:n+r); %右边界填充
imgn2(m2+r+1:m2+2*r,r+1:n2+2*r)=imgn2(m2+1:m2+r,r+1:n2+2*r); %下边界填充
imgn2(1:m2+2*r,1:r)=imgn2(1:m2+2*r,r+1:2*r); %左边界填充
%开始计算每个像素,共计算m*n次
for i=r+1:r+m
for j=r+1:r+n %遍历imgn 中部的源img部分
%计算式子的分母
w=1-abs(imgn(i-r:i+r,j-r:j+r)-imgn(i,j))/(2.5*T); %w是一个以img中的元素为核心,size=[2r+1][2r+1]的矩阵,这样的模板会计算m*n次
%灰度值溢出检查
for p=1:2*r+1
for q=1:2*r+1
if w(p,q) <=0
w(p,q)=0;
end
end
end
%计算式子的分子
s=w.*imgn(i-r:i+r,j-r:j+r);
%计算总式
imgn(i,j)=sum(sum(s))/sum(sum(w)); %一个sum()对一维数组求和,sum(sum())就是对二维矩阵求和
end
end
for i=r+1:r+m1
for j=r+1:r+n1 %遍历imgn 中部的源img部分
%计算式子的分母
w1=1-abs(imgn1(i-r:i+r,j-r:j+r)-imgn1(i,j))/(2.5*T); %w是一个以img中的元素为核心,size=[2r+1][2r+1]的矩阵,这样的模板会计算m*n次
%灰度值溢出检查
for p=1:2*r+1
for q=1:2*r+1
if w1(p,q) <=0
w1(p,q)=0;
end
end
end
%计算式子的分子
s1=w1.*imgn1(i-r:i+r,j-r:j+r);
%计算总式
imgn1(i,j)=sum(sum(s1))/sum(sum(w1)); %一个sum()对一维数组求和,sum(sum())就是对二维矩阵求和
end
end
for i=r+1:r+m2
for j=r+1:r+n2 %遍历imgn 中部的源img部分
%计算式子的分母
w2=1-abs(imgn2(i-r:i+r,j-r:j+r)-imgn2(i,j))/(2.5*T); %w是一个以img中的元素为核心,size=[2r+1][2r+1]的矩阵,这样的模板会计算m*n次
%灰度值溢出检查
for p=1:2*r+1
for q=1:2*r+1
if w2(p,q) <=0
w2(p,q)=0;
end
end
end
%计算式子的分子
s2=w2.*imgn2(i-r:i+r,j-r:j+r);
%计算总式
imgn2(i,j)=sum(sum(s2))/sum(sum(w2)); %一个sum()对一维数组求和,sum(sum())就是对二维矩阵求和
end
end
img=imgn(r+1:r+m,r+1:r+n); %从imgn截取出源img部分
img1=imgn1(r+1:r+m1,r+1:r+n1); %从imgn截取出源img部分
img2=imgn2(r+1:r+m2,r+1:r+n2); %从imgn截取出源img部分
res=cat(3,img,img1,img2);
handles.img=uint8(res);
cla;
imshow(handles.img);
guidata(hObject,handles);
%素描
% --- Executes on button press in sumiao.
function sumiao_Callback(hObject, eventdata, handles)
% hObject handle to sumiao (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.img=handles.i
info_size=size(handles.img);
height=info_size(1);
width=info_size(2);
N=zeros(height,width);
g=zeros(height,width);
imggray=rgb2gray(handles.img);
out=zeros(height,width);
spec=zeros(height,width,3);
for i=1:height
for j=1:width
N(i,j)=255-imggray(i,j);
end
end
for i=2:height-1
for j=2:width-1
sum=0;
sum=1*double(N(i-1,j-1))+2*double(N(i-1,j))+1*double(N(i-1,j+1));
sum=sum+2*double(N(i,j-1))+4*double(N(i,j))+2*double(N(i,j+1));
sum=sum+1*double(N(i+1,j-1))+2*double(N(i+1,j))+1*double(N(i+1,j+1));
sum=sum/16;
g(i,j)=sum;
end
end
for i=1:height
for j=1:width
b=double(g(i,j));
a=double(imggray(i,j));
temp=a+a*b/(256-b);
out(i,j)=uint8(min(temp,255));
end
end
handles.img=out/255;
cla;
imshow(handles.img);
guidata(hObject,handles);
%左右翻转
% --- Executes on button press in m1.
function m1_Callback(hObject, eventdata, handles)
% hObject handle to m1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.img=fliplr(handles.img);
axes(handles.g2);
cla;
imshow(handles.img);
guidata(hObject,handles);
%上下翻转
% --- Executes on button press in m2.
function m2_Callback(hObject, eventdata, handles)
% hObject handle to m2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.img=flipud(handles.img);
axes(handles.g2);
cla;
imshow(handles.img);
guidata(hObject,handles);
%黑白处理
% --- Executes on button press in m4.
function m4_Callback(hObject, eventdata, handles)
% hObject handle to m4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.img = rgb2gray(handles.img);
for i = 1:size(handles.img,1)
for j = 1:size(handles.img,2)
if(handles.img(i,j) < 230)
handles.img(i,j) = 0;
else
handles.img(i,j) = 255;
end
end
end
cla;
imshow(handles.img);
guidata(hObject,handles);
%反色处理
% --- Executes on button press in m3.
function m3_Callback(hObject, eventdata, handles)
% hObject handle to m4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
r=handles.img(:,:,1);
g=handles.img(:,:,2);
b=handles.img(:,:,3);
r1=255-r;
g1=255-g;
b1=255-b;
s=cat(3,r1,g1,b1);
axes(handles.g2); cla; imshow(s)
handles.img=s;
%亮度调节
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x=get(hObject,'Value');
img=handles.img;
img=img+x;
axes(handles.g2); cla; imshow(img)
handles.img=img;
% 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 slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (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,'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 slider2_CreateFcn(hObject, ~, 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
图形界面设计(简陋的设计):
二.功能测试
1 黑白处理
2反色处理
3亮度变化
4磨皮
5素描
6翻转