例6.5彩色图像锐化
I=imread('zhx2.jpg');
imshow(I);
lapMatrix=[1 1 1;1 -8 1;1 1 1];
I_tmp=imfilter(I,lapMatrix,'replicate'); I_sharped=imsubtract(I,I_tmp);
imshow(I_sharped);
例6.6空域滤波图像复原
I=imread('zhx2.jpg2'); subplot(221);
imshow(I);title('RGB原图'); m=fspecial('motion',20,45); I2=imfilter(I,m,'circular');
noise=imnoise(zeros(size(I)),'gaussian',0,0.005); subplot(222);
imshow(noise);title('noise'); I3=double(I2)+noise; I3=uint8(I3); subplot(223);
imshow(I3);title('运动模糊并加入噪声后图像'); I3_recovered=deconvwnr(I3,m); subplot(224);
imshow(I3_recovered);title('维纳滤波复原后图像'); clc
例6.7彩色图像的边缘检测
I=imread('zhx2.jpg'); subplot(121);imshow(I); s0pt=fspecial('sobel');
%计算R,G,B 对x,y的偏导数
Rx=imfilter(double(I(:,:,1)),s0pt,'replicate'); Ry=imfilter(double(I(:,:,1)),s0pt','replicate'); Gx=imfilter(double(I(:,:,2)),s0pt,'replicate'); Gy=imfilter(double(I(:,:,2)),s0pt','replicate'); Bx=imfilter(double(I(:,:,3)),s0pt,'replicate'); By=imfilter(double(I(:,:,3)),s0pt','replicate'); %计算点积gxx,gyy,gxy
gxx=Rx.^2+Gx.^2+Bx.^2; gyy=Ry.^2+Gy.^2+By.^2;
gxy=Rx.*Ry+Gx.*Gy+Bx.*By; %计算变化率最大的角度
theta=0.5*(atan(2*gxy./(gxx-gyy+eps)));
G1=0.5*((gxx+gyy)+(gxx-gyy).*cos(2*theta)+2*gxy.*sin(2*theta)); %由于tan函数的周期性,现旋转90度再次计算 theta=theta+pi/2;
G2=0.5*((gxx+gyy)+(gxx-gyy).*cos(2*theta)+2*gxy.*sin(2*theta)); G1=G1.^0.5 G2=G2.^0.5;
gradiant=mat2gray(max(G1,G2)); subplot(122); imshow(gradiant);
例6.8密度分层
I=imread('peppers2.bmp'); imshow(I);
G2C=grayslice(I,8); figure
imshow(G2C,hot(8));