文件
[filename,pathname]=uigetfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'打开');
if isequal(filename,0)|isequal(pathname,0)
errordlg('没有选中文件','出错');
return;
else
file=[pathname,filename];
global Z %用于初始化的全局变量Z
Z=file;
o=imread(file);
axes(handles.axes1);
imshow(o);
title('原始图像');
k=imread(file);
axes(handles.axes2);
imshow(k);
title('待处理图像');
handles.k=k;
guidata(hObject,handles);
end
文件2
[filename,pathname] =uigetfile({'*.jpg';'*.bmp';'*.tif'},'加载图片');
file=[pathname,filename];
global S; %设置一个全局变量S,
S=file;
x=imread(file); %选中图片
axes(handles.axes1);
imshow(x); %用axes1 显示原图
handles.img=x; %用handles(句柄)存储当前图像
guidata(hObject,handles); %保存handles 的内容
灰度变换
global T
axes(handles.axes2);
T=getimage;
k=rgb2gray(handles.k);
imshow(k);
title('灰度变换后的图像');
handles.k=k;
guidata(hObject,handles);
保存
[sfilename ,sfilepath]=uiputfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'保存图像文件','untitled.jpg');%选择图像文件保存的路径与格式
if ~isequal([sfilename,sfilepath],[0,0])
sfilefullname=[sfilepath ,sfilename]
axes(handles.axes2);
imwrite(handles.k,sfilefullname,'jpg');%实现对图像的保存
else
msgbox('你按了取消键','保存失败');
end
单独保存
imwrite(I2,'D:\我的文档\My?Pictures\压缩后xiao.jpg')
退出
clc;
close all;
clear;
撤销
axes(handles.axes2);%撤销
global T
imshow(T);
title('返回上一步操作效果');
handles.k=T;
guidata(hObject,handles);
直方均衡化
global T
axes(handles.axes2);
T=getimag;
k=rgb2gray(handles.k);
k=histeq(k);
imshow(k);
title('经过直方图均衡后的图像');
handles.k=k;
guidata(hObject,handles);
中值滤波
global T
axes(handles.axes2);
T=getimage;
k=rgb2gray(handles.k);
k=medfilt2(k);
imshow(k);
title('中值滤波后的图像');
handles.k=k;
guidata(hObject,handles);
低通滤波
axes(handles.axes2);
k1=handles.k;
f=double(k1);% 数据类型转换,matlab不支持图像的无符号整型的计算
g=fft2(f);% 傅里叶变换
g=fftshift(g);% 转换数据矩阵
[M,N]=size(g);
nn=2;%二阶巴特沃斯低通滤波器
d0=50;%截止频率50
m=fix(M/2); n=fix(N/2);
for i=1:M
for j=1:N
d=sqrt((i-m)^2+(j-n)^2);
h=1/(1+0.414*(d/d0)^(2*nn));% 计算低通滤波器传递函数
result(i,j)=h*g(i,j);
end
end
result=ifftshift(result);
k2=ifft2(result);
k=uint8(real(k2));
imshow(k);% 显示处理后的图像
title('经过低通滤波后的图像');
handles.k=k;
guidata(hObject,handles);
高通滤波
axes(handles.axes2);
x=(handles.k);
f=double(x);% 数据类型转换
k1=fft2(f);% 傅里叶变换
g=fftshift(k1);% 转换数据矩阵
[M,N]=size(g);
nn=2;
d0=25;%截止频率25
m=fix(M/2); n=fix(N/2);
for i=1:M
for j=1:N
d=sqrt((i-m)^2+(j-n)^2);% 计算高通滤波器传递函数
if d<=d0
h=0;
else h=1;
end
result(i,j)=h*g(i,j);
end
end
result=ifftshift(result);
k2=ifft2(result);
k=uint8(real(k2));
imshow(k);% 显示滤波处理后的图像
title('经过高通滤波后的图像');
handles.k=k;
guidata(hObject,handles);
图像初始化
global Z
axes(handles.axes2);
k=imread(Z);
imshow(k);
title('原始图像');
handles.k=k;
guidata(hObject,handles);
待定
val=get(handles.popupmenu4,'Value')
switch val
case 2
[filename,pathname]=uigetfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'打开');
if isequal(filename,0)|isequal(pathname,0)
errordlg('没有选中文件','出错');
return;
else
file=[pathname,filename];
global Z %用于初始化的全局变量Z
Z=file;
o=imread(file);
axes(handles.axes1);
imshow(o);
title('原始图像');
k=imread(file);
axes(handles.axes2);
imshow(k);
title('待处理图像');
handles.k=k;
guidata(hObject,handles);
end
case 3
[sfilename ,sfilepath]=uiputfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'保存图像文件','untitled.jpg');%选择图像文件保存的路径与格式
if ~isequal([sfilename,sfilepath],[0,0])
sfilefullname=[sfilepath ,sfilename]
axes(handles.axes2);
imwrite(handles.k,sfilefullname,'jpg');%实现对图像的保存
else
msgbox('你按了取消键','保存失败');
end
界面+旋转
angle=get(handles.edit1,'string');
handles.im1=imrotate(handles.im1,str2num(angle));
guidata(hObject,handles);?
uiresume(handles.figure1);
弹出输入框
prompt={'缩放倍数'};%提示信息
name='input';%对话框标题
numlines=1;%输入行数
defans={'1'};%默认数值
p=inputdlg(prompt,name,numlines,defans);
d0=str2num(p{1});
set(handles.parameter,'UserData',d0);
获取d0=get(handles.parameter,'UserData');
小波变换
axes(handles.axes2); %定义图像显示位置; 小波变换
rgb=handles.k; %选定操作图
mysize=size(rgb);
if numel(mysize)>2
k=rgb2gray(rgb);
else
k=rgb;
end
[c,s] = wavedec2(k,1,'sym5'); %利用小波sym5对图像k进行1层分解
a1 = wrcoef2('a',c,s,'sym5',1); %从小波分解结构[c,s]进行尺度为1时的低频重构
imshow(a1);
colormap(map);
title('尺度为1时的低频图像');
两个axe画图
plot(handles.axes1,t,y,'gd');
plot3(handles.axes2,t,x,y,'r*');
复原
I=checkerboard(8);
noise=0.1*randn(size(I));
PSF=fspecial('motion',21,11);
Blurred=imfilter(I,PSF,'circular');
BlurredNoisy=im2uint8(Blurred+noise);
NP=abs(fftn(noise)).^2;
NPOW=sum(NP(:)/prod(size(noise));
NCORR=fftshift(real(ifftn(NP)));
IP=abs(fftn(I)).^2;
IPOW=sum(IP(:)/prod(size(noise));
ICORR=fftshift(real(ifftn(IP)));
ICORR1=ICORR(:,ceil(size(I,1)/2));
NSR=NPOW/IPOW;
subplot(221);imshow(BlurredNoisy,[]);
title('模糊和噪声图像');
subplot(222);imshow(deconvwnr(BlurredNoisy,PSF,NSR),[]);
title('deconbwnr(A,PSF,NSR)');
subplot(223);imshow(deconvwnr(BlurredNoisy,PSF,NCORR,ICORR),[]);
title('deconbwnr(A,PSF,NCORR,ICORR)');
subplot(224);imshow(deconvwnr(BlurredNoisy,PSF,NPOW,ICORR1),[]);
title('deconbwnr(A,PSF,NPOW,ICORR_1_D)');
2.规则化滤波复原程序
I=checkerboard(8);
PSF=fspecial('gaussian',7,10);
V=.01;
BlurredNoisy=imnoise(imfilter(I,PSF),'gaussian',0,V);
NOISEPOWER=V*prod(size(I));
[J LAGRA]=deconvreg(BlurredNoisy,PSF,NOISEPOWER);
subplot(221);imshow(BlurredNoisy);
title('A=Blurred and Noisy');
subplot(222);imshow(J);
title('[J LAGRA]=deconvreg(A,PSF,NP)');
subplot(223);imshow(deconvreg(BlurredNoisy,PSF,[],LAGRA/10));
title('deconvreg(A,PSF,[],0.1*LAGRA)');
subplot(225);imshow(deconvreg(BlurredNoisy,PSF,[],LAGRA/10))
subplot(224);imshow(deconvreg(BlurredNoisy,PSF,[],LAGRA*10));
title('deconvreg(A,PSF,[],10*LAGRA');
3.Lucy-Richardson复原程序举例
%Lucy-Richardson复原程序
I=checkerboard(8);
PSF=fspecial('gaussian',7,10);
V=.0001;
BlurredNoisy=imnoise(imfilter(I,PSF),'gaussian',0,V);
WT=zeros(size(I));
WT(5:end-4,5:end-4)=1;
J1=deconvlucy(BlurredNoisy,PSF);
J2=deconvlucy(BlurredNoisy,PSF,20,sqrt(V));
J3=deconvlucy(BlurredNoisy,PSF,20,sqrt(V),[],WT);
subplot(221);imshow(BlurredNoisy);
title('A=Blurred and Noisy');
subplot(222);imshow(J1);
title('deconvlucy(A,PSF)');
subplot(223);imshow(J2);
title('deconvlucy(A,PSF,NI,DP)');
subplot(224);imshow(J3);
title('deconvlucy(A,PSF,NI,DP,[],WT)');
4.盲去卷积复原
%盲去卷积复原
I=checkerboard(8);
PSF=fspecial('gaussian',7,10);
V=.0001;
BlurredNoisy=imnoise(imfilter(I,PSF),'gaussian',0,V);
WT=zeros(size(I));
WT(5:end-4,5:end-4)=1;
INITPSF=ones(size(PSF));
FUN=inline('PSF+P1','PSF','P1');
[J P]=deconvblind(BlurredNoisy,INITPSF,20,10*sqrt(V),WT,FUN,0);
subplot(221);imshow(BlurredNoisy);
title('A=Blurred and Noisy');
subplot(222);imshow(PSF,[]);
title('True PSF');
subplot(223);imshow(J);
title('Deblured Image');
subplot(224);imshow(P,[]);
title('Recovered PSF');
运动模糊
global B;
axes(handles.axes2); %定义图像显示位置;
k=rgb2gray(handles.k); %图像灰度处理
answer=inputdlg({'LEN';'THETA'},'请输入') %输入参数(数组)
x=str2num(cell2mat(answer(1))) %提取参数LEN
y=str2num(cell2mat(answer(2))) %提取参数THETA
H=fspecial('motion',x,y); %点扩散函数
MotionBlur=imfilter(k,H,'