f=zeros(40,40);% f(10:30,10:30)=1; subplot(1,3,1);% imshow(f);% F=fft2(f)
subplot(1,3,2);% imshow(F);%
D=log(1+abs(F));% subplot(1,3,3); imshow(D); 1——2 clc
load imdemos saturn2 subplot(121) imshow(saturn2)
i=fftshift(fft2(saturn2)); subplot(122)
imshow(log(abs(i)),[]),colormap(jet(64)),colorbar; 1——3
w=imread('text.png'); a=w(33:45,88:98); figure;
imshow(w); figure; imshow(a);
C=real(ifft2(fft2(w).*fft2(rot90(a,2),256,256)));
figure;
imshow(C,[]); max(C(:)); thresh=60; figure;
imshow(C>thresh); 1——4
RGB=imread('autumn.tif'); figure(1),imshow(RGB); I=rgb2gray(RGB); figure(2),imshow(I); J=dct2(I);
figure(3),imshow(log(abs(J)),[]),colormap(jet(64)),colorbar; J(abs(J)<10)=0; K=idct2(J)/255;
figure(4),imshow(K); 1——5——2 cr=0.125;
sig=imread('cameraman.tif'); sig=double(sig)/255; figure(1),imshow(sig); [m_sig,n_sig]=size(sig); sizi=8; snum=64;
t=hadamard(sizi);
hdcoe=blkproc(sig,[sizi sizi],'P1*x*P2',t,t'); coe=im2col(hdcoe,[sizi sizi],'distinct'); coe_temp=coe; [Y Ind]=sort(coe); [m,n]=size(coe);
snum=snum-snum*cr; for i=1:n
coe_temp(Ind(1:snum),i)=0; end
re_hdcoe=col2im(coe_temp,[sizi sizi],[m_sig n_sig],'distinct');
re_sig=blkproc(re_hdcoe,[sizi sizi],'P1*x*P2',t,t');
re_sig=double(re_sig)/64; figure(2);
imshow(re_sig);
error=sig.^2-re_sig.^2;
MSE=sum(error(:))/prod(size(re_sig));
2——1
i=imread('pout.tif'); imshow(i); figure;
subplot(1,2,1); imhist(i); j=histeq(i,64); subplot(1,2,2) imhist(j) 2——2
f=imread('autumn.tif'); gl=imadjust(f,[0 1],[1,0]); subplot(121); imshow(f); subplot(122); imshow(gl);
2——3
f=imread('autumn.tif');
gl=imadjust(f,[0.5 0.75],[1,0]); subplot(121); imshow(f); subplot(122); imshow(gl); 2——4
f=imread('autumn.tif'); gl=imadjust(f,[ ],[ ],2); subplot(121); imshow(f); subplot(122); imshow(gl); test 1
I=imread('pout.tif'); [row,col] = size(I); imshow(I); K=histeq(I,64)
subplot(121) imhist(K);
title('直方图均衡化') J = zeros(row,col);
for i =1:row
for j = 1: col
if (I(i,j) <10 && I(i,j) >0) J(i,j) = 0.5*I(i,j); elseif (I(i,j) < 20)
J(i,j) = 2*I(i,j)-15; elseif (I(i,j) < 30)
J(i,j) = 0.5*I(i,j)+15; else
J(i,j) = 0.5*I(i,j); end end end
J = uint8(J); subplot(122); imhist(J)
title('线形变化')
test2
I=imread('cameraman.tif') figure
subplot(121)
f=imcomplement(I); imshow(f)
title('求反图像') I1=I;
axis([50,250,50,200]);
grid on; %显示网格线 axis on; %显示坐标系 J=double(I1); J=40*(log(J+1)); H=uint8(J);
subplot(1,2,2),imshow(H); title('对数变换处理')
3-1
I=imread('eight.tif');
J=imnoise(I,'salt & pepper',0.02); subplot(1,2,1); imshow(I);
title('原始图像'); subplot(1,2,2); imshow(J);
title('噪声图像');
K1=filter2(fspecial('average',3),J)/255; K2=filter2(fspecial('average',5),J)/255; K3=filter2(fspecial('average',7),J)/255; figure,imshow(K1);
title('3*3模板均值滤波'); figure,imshow(K2);
title('5*5模板均值滤波'); figure,imshow(K3);
title('7*7模板均值滤波'); title('7*7模板均值滤波'); 3-2
I=imread('eight.tif');
J=imnoise(I,'salt & pepper',0.02); subplot(2,2,1); imshow(J);
title('噪声图像'); K1=medfilt2(J,[3 3]); K2=medfilt2(J,[5 5]); K3=medfilt2(J,[7 7]); subplot(2,2,2); imshow(K1);
title('3*3模板中值滤波'); subplot(2,2,3); imshow(K2);
title('5*5模板中值滤波'); subplot(2,2,4); imshow(K3);
title('7*7模板中值滤波'); 3-3
f=imread('moon.tif');
w4=fspecial('laplacian',0); w8=[1 1 1;1 -8 1;1 1 1]; f=im2double(f);
g4=imfilter(f,w4,'replicate'); g8=imfilter(f,w8,'replicate'); G4=f-g4; G8=f-g8; imshow(f);
figure,imshow(G4); figure,imshow(G8); 3-4 clear;
I1=imread('moon.tif'); subplot(2,2,1); imshow(I1); title('原始图像');
I2=imnoise(I1,'salt & pepper'); subplot(2,2,2); imshow(I2);
title('噪声图像')'; f=double(I2); g=fft2(f); g=fftshift(g); [N1,N2]=size(g); n=2; do=50;
n1=fix(N1/2); n2=fix(N2/2); for i=1:N1 for j=2:N2
d=sqrt((i-n1)^2+(j-n2)^2); h=1/(1+0.414*(d/do)^(2*n)); result1(i,j)=h*g(i,j); if(g(i,j)>50)
result2(i,j)=0; else
result2(i,j)=g(i,j); end end end
result1=ifftshift(result1); result2=ifftshift(result2); X2=ifft2(result1); X3=uint8(real(X2)); subplot(2,2,3); imshow(X3);
title('Butterworth 滤波图像'); X4=ifft2(result2); X5=uint8(real(X4)); subplot(2,2,4); imshow(X5);
title('理想低通滤波器'); test1
I=imread('m83.tif');
J=imnoise(I,'salt & pepper',0.02); subplot(2,2,1); imshow(J);
title('噪声图像'); K1=medfilt2(J,[3 3]); K2=medfilt2(J,[5 5]); K3=medfilt2(J,[7 7]); subplot(2,2,2); imshow(K1);
title('3*3模板中值滤波'); subplot(2,2,3); imshow(K2);
title('5*5模板中值滤波'); subplot(2,2,4); imshow(K3);
title('7*7模板中值滤波'); f=imread('board.tif'); w4=fspecial('sobel'); w8=[1 2 1;0 0 0;-1 -2 -1]; f=im2double(f);
g4=imfilter(f,w4,'replicate'); g8=imfilter(f,w8,'replicate'); G4=f-g4; G8=f-g8; imshow(f);
figure,imshow(G4); figure,imshow(G8); test2
f=imread('board.tif'); w4=fspecial('sobel'); w8=[1 2 1;0 0 0;-1 -2 -1]; f=im2double(f);
g4=imfilter(f,w4,'replicate'); g8=imfilter(f,w8,'replicate'); G4=f-g4; G8=f-g8; imshow(f);
figure,imshow(G4); figure,imshow(G8);
4-1 clear; d=15
h=zeros(2*d+1,2*d+1); h(d+1,1:2*d+1)=1/(2*d); f=imread('cameraman.tif'); [m,n]=size(f);
fe=zeros(m+2*d,n+2*d); fe(1:m,1:n)=f;
he=zeros(m+2*d,n+2*d); he(1:2*d+1,1:2*d+1)=h; F=fft2(fe); H=fft2(he);
ns=5*rand(m+2*d,n+2*d); g=ifft2(F.*H)+ns; G=fft2(g); K=0;
F_est=((H.^2)./(H.^2+K)).*G./H; f_est=real(ifft2(F_est)); imshow(f); figure;
imshow(g(d+1:m+d,d+1:n+d),[min(g(:)) max(g(:))]); figure;
imshow(f_est(1:m,1:n),[min(f_est(:)) max(f_est(:))]); 4-2
F=checkerboard(8); figure(1); imshow(F,[]);
PSF=fspecial('motion',7,45); MF=imfilter(F,PSF,'circular'); noise=imnoise(zeros(size(F)),'gaussian',0,0.01); MFN=MF+noise; figure(2);
imshow(MFN,[]);
NSR=sum(noise(:).^2)/sum(MFN(:).^2); figure(3);
imshow(deconvwnr(MFN,PSF),[]); figure(4);
imshow(deconvwnr(MFN,PSF,NSR),[]); Sn=abs(fft2(noise)).^2; Sf=abs(fft2(F)).^2;
NCORR=fftshift(real(ifft2(Sn))); ICORR=fftshift(real(ifft2(Sf))); figure(5);
imshow(deconvwnr(MFN,PSF,NCORR,ICORR),[]); test1
F=checkerboard(8); figure(1); imshow(F,[]);
PSF=fspecial('motion',7,45); MF=imfilter(F,PSF,'circular');
noise=imnoise(zeros(size(F)),'gaussian',0,0.01);
MFN=MF+noise; figure(2);
imshow(MFN,[]);
NSR=sum(noise(:).^2)/sum(MFN(:).^2); figure(3);
imshow(deconvwnr(MFN,PSF),[]); figure(4);
imshow(deconvwnr(MFN,PSF,NSR),[]);
Sn=abs(fft2(noise)).^2; Sf=abs(fft2(F)).^2;
NCORR=fftshift(real(ifft2(Sn))); ICORR=fftshift(real(ifft2(Sf))); figure(5);
imshow(deconvwnr(MFN,PSF,NCORR,ICORR),[]);
5-1
I = imread('circuit.tif'); BW1 = edge(I,'prewitt'); BW2 = edge(I,'canny'); BW3 = edge(I,'sobel'); BW4 = edge(I,'roberts'); BW5 = edge(I,'log');
BW6 = edge(I,'zerocross'); figure
subplot(131) imshow(BW1); title('prewitt') subplot(132) imshow(BW2); title('canny') subplot(133) imshow(BW3); title('sobel') figure
subplot(131) imshow(BW4); title('roberts') subplot(132) imshow(BW5);
title('prewitt') subplot(133) imshow(BW6); title('zerocross') 5-2 clc
clear all
I=imread('cameraman.bmp'); subplot(1,2,1),imshow(I); title('原始图像')
axis([50,250,50,200]);
grid on; %显示网格线 axis on; %显示坐标系 level=graythresh(I); %确定灰度阈值 BW=im2bw(I,level);
subplot(1,2,2),imshow(BW); title('Otsu 法阈值分割图像') axis([50,250,50,200]);
grid on; %显示网格线 axis on; %显示坐标系 I=imread('xian.bmp'); %载入图像 I1=rgb2gray(I); subplot(1,2,1); imshow(I1);
title('灰度图像') axis([50,250,50,200]);
grid on; %显示网格线 axis on; %显示坐标系 se=strel('disk',1); %生成圆形结构元素
I2=imdilate(I1,se); %用生成的结构元素对图像进行膨胀 subplot(1,2,2); imshow(I2);
title(' 膨胀后图像'); axis([50,250,50,200]);
grid on; %显示网格线 axis on; %显示坐标系 test1
I1=imread('cameraman.bmp'); %=rgb2gray(I); figure;
subplot(2,2,1); imshow(I1);
title(' 灰度图像') axis([50,250,50,200]);
grid on; %显示网格线 axis on; %显示坐标系 [m,n]=size(I1); %测量图像尺寸参数
GP=zeros(1,256); %预创建存放灰度出现概率的向量 for k=0:255
GP(k+1)=length(find(I1==k))/(m*n); %计算每级灰度出现的概率,将其存入GP中相应位置 end
subplot(2,2,2),bar(0:255,GP,'g') %绘制直方图 title('灰度直方图') xlabel('灰度值') ylabel(' 出现概率')
I2=im2bw(I,150/255); subplot(2,2,3),imshow(I2); title('阈值150的分割图像') axis([50,250,50,200]);
grid on; %显示网格线 axis on; %显示坐标系 I3=im2bw(I,200/255); % subplot(2,2,4),imshow(I3); title('阈值200的分割图像') axis([50,250,50,200]);
grid on; %显示网格线 axis on; %显示坐标系 test2
I=imread('cameraman.bmp');
%I=rgb2gray(I);%如果是rgb图像就加,如果不是就不要这行
figure,imshow(I),title('原始图像') I=double(I); [M,N]=size(I);
[y,x]=getpts; %获得区域生长起始点 x1=round(x); %横坐标取整 y1=round(y); %纵坐标取整
seed=I(x1,y1); %将生长起始点灰度值存入seed中 Y=zeros(M,N); %作一个全零与原图像等大的图像矩阵Y,作为输出图像矩阵
Y(x1,y1)=1; %将Y中与所取点相对应位置的点设置为白场
sum=seed; %储存符合区域生长条件的点的灰度值的和
suit=1; %储存符合区域生长条件的点的个数
count=1; %记录每次判断一点周围八点符合条件的新点的数目 threshold=15; %域值 while count>0
s=0; %记录判断一点周围八点时,符合条件的新点的灰度值之和 count=0; for i=1:M for j=1:N if Y(i,j)==1
if (i-1)>0 && (i+1)<(M+1) && (j-1)>0 && (j+1)<(N+1) %判断此点是否为图像边界上的点
for u= -1:1 %判断点周围八点是否符合域值条件
for v= -1:1 %u,v为偏移量 if Y(i+u,j+v)==0 & abs(I(i+u,j+v)-seed)<=threshold&
1/(1+1/15*abs(I(i+u,j+v)-seed))>0.8%判断是否未存在于输出矩阵Y,并且为符合域值条件的点
Y(i+u,j+v)=1; %符合以上两条件即将其在Y中与之位置对应的点设置为白场 count=count+1;
s=s+I(i+u,j+v); %此点的灰度之加入s中 end end end end end end end
suit=suit+count; %将n加入符合点数计数器中
sum=sum+s; %将s加入符合点的灰度值总合中
seed=sum/suit; %计算新的灰度平均值 end
figure,imshow(Y),title('分割后图像')
因篇幅问题不能全部显示,请点此查看更多更全内容