资源描述
%系统自动生成的创建对话框的代码
function varargout = myproject(varargin)
% MYPROJECT M-file for myproject.fig
% MYPROJECT, by itself, creates a new MYPROJECT or raises the existing
% singleton*.
%
% H = MYPROJECT returns the handle to a new MYPROJECT or the handle to
% the existing singleton*.
%
% MYPROJECT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MYPROJECT.M with the given input arguments.
%
% MYPROJECT('Property','Value',...) creates a new MYPROJECT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before myproject_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to myproject_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 myproject
% Last Modified by GUIDE v2.5 07-Jun-2008 11:33:02
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @myproject_OpeningFcn, ...
'gui_OutputFcn', @myproject_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 myproject is made visible.
function myproject_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 myproject (see VARARGIN)
% Choose default command line output for myproject
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes myproject wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = myproject_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 lingcunwei.
function lingcunwei_Callback(hObject, eventdata, handles) %另存为按钮的回调函数
global ImagenUmbral %定义全局变量
if isempty(ImagenUmbral)==1,msgbox('Doesn''t exist an image');return,
end %如果ImagenUmbral不包含图像,则弹出对话框并显示'Doesn''t exist an image'
[filename,pathname]=uiputfile({'*.jpg';,'*.tif';,'*.gif';,'*.bmp';,'*.png';, ...
'*.hdf';,'*.pcx';,'*.xwd';,'*.ico';,'*.cur';,'*.ras';, ...
'*.pdm';,'*.pgm';,'*.ppm'},'Save file name'); %显示保存文件的对话框
if isequal(filename,0) | isequal(pathname,0)
errordlg('Saving canceled','Threshold GUI'); error('Saving canceled')
else %如果不存在该文件,或者不存在保存路径,则显示错误信息
try
imwrite(ImagenUmbral,[ pathname,filename]); %保存文件
end %try
end %if
% hObject handle to lingcunwei (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 dayinshuchu.
function dayinshuchu_Callback(hObject, eventdata, handles) %打印按钮的回调函数
printdlg %显示打印对话框
% hObject handle to dayinshuchu (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 fuzhidaojianqieban.
function fuzhidaojianqieban_Callback(hObject, eventdata, handles)%复制到剪切板按钮的回调函数
global ImagenUmbral %处理后的图像
global J %处理前的图像
J=ImagenUmbral %将处理后的图像赋予处理前的图像
% hObject handle to fuzhidaojianqieban (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 jihebianhuan.
function jihebianhuan_Callback(hObject, eventdata, handles)
global ImagenUmbral %定义一个全局变量ImagenUmbral
global J %使用全局变量J
selection = questdlg('请选择几何变换','选择几何变换','水平镜像','垂直镜像','对角镜像','default')
if strcmp(selection,'水平镜像') %如果选择“水平镜像”
[M,N]=size(J) %测量图像尺寸参数
I=J %将J赋给I
for i=1:M %从第一行到最后一行
for j=1:N/2 %对每一行的第一个像素到中间的一个像素
t=I(i,j);I(i,j)=I(i,N-j+1);I(i,N-j+1)=t; %交换这一行第一个像素和最后一个像素的灰度值,交换第二个和倒数第二个灰度值,以此类推,直到中间的像素。
end %end for
end %end for
subplot(224); %分割绘图窗口为两行两列,将句柄移到第四个位置
imshow(I) %显示图像
ImagenUmbral=I; %将图像赋予全局变量ImagenUmbral
else if strcmp(selection,'垂直镜像') %如果选择“垂直镜像”
[M,N]=size(J) %测量图像尺寸参数
I=J %将J赋给I
for j=1:N %从第一列到最后一列
for i=1:M/2 %对每一列的第一个像素到中间的一个像素
t=I(i,j);I(i,j)=I(M-i+1,j);I(M-i+1,j)=t; %交换这一列第一个像素和最后一个像素的灰度值,交换第二个和倒数第二个灰度值,以此类推,直到中间的像素。
end %end for
end %end for
subplot(224); %分割绘图窗口为两行两列,将句柄移到第四个位置
imshow(I) %显示图像
ImagenUmbral=I; %将图像赋予全局变量ImagenUmbral
else if strcmp(selection,'对角镜像') %如果选择“垂直镜像”
I=J %将J赋给I
[M,N]=size(I) %测量图像尺寸参数
for i=1:M %从第一行到最后一行
for j=1:N/2 %对每一行的第一个像素到中间的一个像素
t=I(i,j);I(i,j)=I(M-i+1,N-j+1);I(M-i+1,N-j+1)=t;%交换第i行的第一个像素和M-i+1行的最后一个像素的灰度值,以此类推,直到中间的像素。
end %end for
end %end for
subplot(224); %分割绘图窗口为两行两列,将句柄移到第四个位置
imshow(I) %显示图像
ImagenUmbral=I; %将图像赋予全局变量ImagenUmbral
end %end of else if strcmp(selection,'对角镜像')
end %end of else if strcmp(selection,'垂直镜像')
end %end of if strcmp(selection,'水平镜像')
% hObject handle to jihebianhuan (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%灰度反转按钮的回调函数,灰度反转采用公式T=L-1-S,其中S为存储原始图像的灰度值的矩阵,T为存储反转后图像灰度值的矩阵。
% --- Executes on button press in huidufanzhuan.
function huidufanzhuan_Callback(hObject, eventdata, handles)%灰度反转按钮的回调函数
global ImagenUmbral %定义一个全局变量ImagenUmbral,将灰度反转后的图像存入ImagenUmbral中
global J %使用全局变量J
A=double(J) %将图像J的各点像素值存入矩阵A中
A=255-A %用255减去A的各点像素值,再重新存入A中
A=uint8(A) %将A的每个元素转换成整数
subplot(224) %分割绘图窗口为两行两列,将句柄移到第四个位置
imshow(A) %显示图像A
ImagenUmbral=A %将A赋给全局变量ImagenUmbral
% hObject handle to huidufanzhuan (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 zhifangtujunhenghua.
function zhifangtujunhenghua_Callback(hObject, eventdata, handles)%直方图均衡化按钮的回调函数
global ImagenUmbral %定义一个全局变量ImagenUmbral,将直方图均衡化后的图像存入ImagenUmbral中
global J %使用全局变量J
subplot(2,2,4); %分割绘图窗口为两行两列,将当前句柄移到第四个位置
%W = histeq(J); %Matlab自带直方图均衡化函数
PS=J %令PS为待处理的图像
[m,n]=size(PS); %测量图像尺寸参数
GP=zeros(1,256); %预创建存放灰度出现概率的向量
for k=0:255 %对每一个像素值
GP(k+1)=length(find(PS==k))/(m*n);%计算每级灰度出现的概率,将其存入GP中相应位置
end %end for
S1=zeros(1,256); %分配一个256维数组
for i=1:256 %对每一个像素值
for j=1:i %对从1到这个像素值的所有像素的
S1(i)=GP(j)+S1(i); %计算Sk,Sk为一个映射,将对从1到这个像素值的所有像素的概率相加
end %end for
end %end for
S2=round((S1*256)+0.5); %将Sk归到相近级的灰度
PA=PS; %定义一个与PS一样大小的矩阵
for i=0:255 %对每一个像素值
PA(find(PS==i))=S2(i+1); %将各个像素归一化后的灰度值赋给这个像素
end %end for
imshow(PA) %显示均衡化后的图像
ImagenUmbral=PA; %将均衡化图像赋给ImagenUmbral
% hObject handle to zhifangtujunhenghua (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 zhifangtutongji.
function zhifangtutongji_Callback(hObject, eventdata, handles)%直方图统计按钮的回调函数
global ImagenUmbral %定义一个全局变量ImagenUmbral
global J %使用全局变量J
figure %弹出绘图窗口
subplot(211) %分割绘图窗口为两行一列,将当前句柄移到第一个区域
[m,n]=size(J); %测量图像尺寸参数
GP=zeros(1,256); %预创建存放灰度出现概率的向量
for k=0:255 %对每一个像素
GP(k+1)=length(find(J==k))/(m*n); %计算每级灰度出现的概率,将其存入GP中相应位置
end %end for
bar(0:255,GP,'b') %绘制直方图
title('原图像直方图') %标题为'原图像直方图'
subplot(212) %分割绘图窗口为两行一列,将当前句柄移到第二个区域
K=ImagenUmbral %将全局变量ImagenUmbral赋予K
[m,n]=size(K); %测量图像尺寸参数
GP=zeros(1,256); %预创建存放灰度出现概率的向量
for k=0:255 %对每一个像素
GP(k+1)=length(find(K==k))/(m*n);%计算每级灰度出现的概率,将其存入GP中相应位置
end %end for
bar(0:255,GP,'b') %绘制直方图
title('处理后图像直方图') %标题为'处理后图像直方图'
% hObject handle to zhifangtutongji (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 tuxiangfuyuan.
function tuxiangfuyuan_Callback(hObject, eventdata, handles)%图像复原按钮的回调函数
global ImagenUmbral %定义一个全局变量ImagenUmbral
global J %使用全局变量J
selection = questdlg('请选择图像复原方法','图像复原方法','中值滤波','均值滤波','default')
if strcmp(selection,'中值滤波') %如果选择'中值滤波'
A=J %将J赋给A
[m,n] = size(A); %测量图像尺寸参数
B=A; %将A赋给B
pixel_block = zeros(1,9); %开辟一个一行九列的数组,均赋于初值0
for i = 2:m-1 %对A中的各个像素点(不包括边界)
for j = 2:n-2
pixel_block = reshape(A(i-1:i+1,j-1:j+1),9,1); %采用3*3的窗口,将一个像素点及其周围的8各点,依次存入新开辟的数组pixel_block中
sorted_block = sort(pixel_block); %将数组pixel_block排序
block_median = sorted_block(5); %将数组的中值赋予block_median
B(i,j) = block_median; %将block_median赋予B的相应位置的像素值
end; %end of for j = 2:n-2
end; %end of for i = 2:m-1
B = uint8(B); %将B中所有元素转换成0~255之间的整数
subplot(224) %分割绘图窗口为两行两列,将句柄移到第四个位置
imshow(B) %显示图像B
ImagenUmbral=B %将B赋给全局变量ImagenUmbral
else if strcmp(selection,'均值滤波')%如果选择'均值滤波'
A=J %将J赋给A
[m,n] = size(A); %m为A的行数,n为A的列数
B=A; %将A赋给B
pixel_block = zeros(1,9); %开辟一个一行九列的数组,均赋于初值0
for i = 2:m-1 %对A中的各个像素点(不包括边界)
for j = 2:n-1
pixel_block = reshape(A(i-1:i+1,j-1:j+1),3^2,1);%采用3*3的窗口,将一个像素点及其周围的8各点,一次存入新开辟的数组pixel_block中
mean_block = mean(pixel_block); %求出数组中所有元素的平均值
B(i,j) = mean_block; %将mean_block赋予B的相应位置的像素值
end; %end of for j = 2:n-2
end; %end of for i = 2:m-1
B = uint8(B); %将B中所有元素转换成0~255之间的整数
subplot(224) %分割绘图窗口为两行两列,将句柄移到第四个位置
imshow(B) %显示图像B
ImagenUmbral=B %将B赋给全局变量ImagenUmbral
end %end of else if strcmp(selection,'均值滤波')
end %end of if strcmp(selection,'中值滤波')
% hObject handle to tuxiangfuyuan (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
展开阅读全文