当前位置:文档之家› matlab图像处理的几个实例

matlab图像处理的几个实例

Matlab图像处理的几个实例(初学者用)1.图像的基本信息及其加减乘除clear,clc;P=imread('yjx.jpg');whos PQ=imread('dt.jpg');P=im2double(P);Q=im2double(Q);gg1=im2bw(P,0.3);gg2=im2bw(P,0.5);gg3=im2bw(P,0.8);K=imadd(gg1,gg2);L=imsubtract(gg2,gg3);cf=immultiply(P,Q);sf=imdivide(Q,P);subplot(421),imshow(P),title('郁金香原图');subplot(422),imshow(gg1),title('0.3');subplot(423),imshow(gg2),title('0.5');subplot(424),imshow(gg3),title('0.8');subplot(425),imshow(K),title('0.3+0.5');subplot(426),imshow(L),title('0.5-0.3');subplot(427),imshow(cf),title('P*Q');subplot(428),imshow(sf),title('P/Q');2.图像缩放clear,clc;I=imread('dt.jpg');A=imresize(I,0.1,'nearest');B=imresize(I,0.4,'bilinear');C=imresize(I,0.7,'bicubic');D=imresize(I,[100,200]);F=imresize(I,[400,100]);figuresubplot(321),imshow(I),title('原图');subplot(322),imshow(A),title('最邻近插值');subplot(323),imshow(B),title('双线性插值');subplot(324),imshow(C),title('二次立方插值');subplot(325),imshow(D),title('水平缩放与垂直缩放比例为2:1'); subplot(326),imshow(F),title('水平缩放与垂直缩放比例为1:4');灰度变换、直方图变换clear,clc;fg=imread('fg.jpg');zl=imread('zl.jpg');hfg=rgb2gray(fg);fg1=double(hfg);out1=255*(fg1/255).^0.7;out1(find(out1>255))=255;fg1=uint8(fg1);out1=uint8(out1);img=rgb2gray(zl);[harm,x]=imhist(img);J=histeq(hfg,harm);figuresubplot(421),imshow(fg1),title('复古灰度图');subplot(422),imhist(fg1),title('复古灰度图的直方图');subplot(423),imshow(out1),title('对复古灰度图像进行幂次变换'); subplot(424),imhist(out1),title('幂次变换图像的直方图');subplot(425),imshow(img),title('朱莉');subplot(426),imhist(img),title('朱莉图像对应的直方图');subplot(427),imshow(J),title('直方图变换后的复古图');subplot(428),imhist(J),title('直方图变换后的复古图对应的直方图');傅里叶变换、频域滤波1.傅里叶变换clear,clc;rgb=imread('zl.jpg');rgb=imresize(rgb,0.7,'bilinear');rgb=im2double(rgb);fR=rgb(:,:,1);fG=rgb(:,:,2);fB=rgb(:,:,3);flyfR=fft2(fR);flyfG=fft2(fG);flyfB=fft2(fB);Frgb(:,:,1)=flyfR;Frgb(:,:,2)=flyfG;Frgb(:,:,3)=flyfB;tzR=fftshift(flyfR);tzG=fftshift(flyfG);tzB=fftshift(flyfB);tzF(:,:,1)=tzR;tzF(:,:,2)=tzG;tzF(:,:,3)=tzB;iflyfR=ifft2(flyfR);iflyfG=ifft2(flyfG);iflyfB=ifft2(flyfB);out(:,:,1)=iflyfR;out(:,:,2)=iflyfG;out(:,:,3)=iflyfB;figuresubplot(221),imshow(rgb),title('原图');subplot(222),imshow(Frgb),title('图像频谱');subplot(223),imshow(tzF),title('调整中心后的图像频谱'); subplot(224),imshow(out),title('逆变换得到的原图');2.频域滤波clear,clc;I=rgb2gray(imread('ml.jpg'));J=imnoise(I,'gaussian',0.1);Jzz1=medfilt2(J,[3 3]);Jzz2=medfilt2(J,[10 10]);XJ=imnoise(I,'salt & pepper');f=im2double(XJ);g=fft2(f);g=fftshift(g);[M,N]=size(g);nn=2;d0=50;m=fix(M/2);n=fix(M/2);for i=1:Mfor j=1:Nd=sqrt((i-m)^2+(j-n)^2);h1=1/(1+0.414*(d/d0)^(2*nn));result1(i,j)=h1*g(i,j);endendresult1=ifftshift(result1);J2=ifft2(result1);J3=im2uint8(real(J2));figuresubplot(231),imshow(I),title('原图的灰度图像');subplot(232),imshow(J),title('加高斯噪声');subplot(233),imshow(Jzz1),title('模板3*3中值滤波后的图像'); subplot(234),imshow(Jzz2),title('模板10*10中值滤波后的图像'); subplot(235),imshow(J3),title('低通滤波图');彩色图像处理clear,clc;rgb=imread('yjx.jpg');fR=rgb(:,:,1);fG=rgb(:,:,2);fB=rgb(:,:,3);R=rgb;R(:,:,[2 3])=0;G=rgb;G(:,:,[1 3])=0;B=rgb;B(:,:,[1 2])=0;yiq=rgb2ntsc(rgb);fY=yiq(:,:,1);fI=yiq(:,:,2);fQ=yiq(:,:,3);fR=histeq(fR,256);fG=histeq(fG,256);fB=histeq(fB,256);RGB=cat(3,fR,fG,fB);figuresubplot(341),imshow(rgb),title('原图');subplot(342),imshow(R),title('图像的红色分量');subplot(343),imshow(G),title('图像的绿色分量');subplot(344),imshow(B),title('图像的蓝色分量');subplot(345),imshow(yiq),title('NTSC彩色空间');subplot(346),imshow(fY),title('亮度');subplot(347),imshow(fI),title('色调');subplot(348),imshow(fQ),title('饱和度');subplot(349),imshow(RGB),title('rgb均衡化后的彩色图像');原图最邻近插值双线性插值二次立方插值水平缩放与垂直缩放比例为2:1水平缩放与垂直缩放比例为1:4原图图像的红色分量图像的绿色分量图像的蓝色分量NTSC 彩色空间亮度色调饱和度rgb 均衡化后的彩色图像郁金香原图0.30.50.80.3+0.50.5-0.3P*QP/Q原图图像频谱调整中心后的图像频谱逆变换得到的原图原图的灰度图像加高斯噪声模板3*3中值滤波后的图像模板10*10中值滤波后的图像低通滤波图。

相关主题