实验四、图像的几何变换与彩色处理一、实验目的1理解和掌握图像的平移、垂直镜像变换、水平镜像变换、缩放和旋转的原理和应用;2熟悉图像几何变换的MATLAB操作和基本功能3 掌握彩色图像处理的基本技术二、实验步骤1 启动MATLAB程序,读入图像并对图像文件分别进行平移、垂直镜像变换、水平镜像变换、缩放和旋转操作%%%%%%平移>> flowerImg=imread('flower.jpg');>> se=translate(strel(1),[100 100]);>> img2=imdilate(flowerImg,se);>> subplot(1,2,1);>> imshow(flowerImg);>> subplot(1,2,2);>> imshow(img2);I1=imread('flower.jpg');I1=double(I1);H=size(I1);I2(1:H(1),1:H(2),1:H(3))=I1(H(1):-1:1,1:H(2),1:H(3)); I3(1:H(1),1:H(2),1:H(3))=I1(1:H(1),H(2):-1:1,1:H(3)); Subplot(2,2,1);Imshow(uint8(I1));Title('原图');Subplot(2,2,2);Imshow(uint8(I3));Title('水平镜像');Subplot(2,2,3);Imshow(uint8(I2));Title('垂直镜像');img1=imread('flower.jpg');figure,imshow(img1);%%%%%%缩放img2=imresize(img1,0.25);figure,imshow(img2);imwrite(img2,'a2.jpg');%%%%%%旋转img3=imrotate(img1,90); figure,imshow(img3); imwrite(img3,'a3.jpg');2 实验如下操作:(1)改变图像缩放比例f= imread('flower.jpg');T=[ 0.5 0 0; 0 0.5 0; 0 0 1];tform=maketform('affine',T);[g1,xdata1,ydata1]=imtransform(f,tform,'FillValue',255);T=[ 1 0 0; 0 1 0; 0 0 1];tform=maketform('affine',T);[g2,xdata2,ydata2]=imtransform(f,tform,'FillValue',255);T=[ 1.5 0 0; 0 1.5 0; 0 0 1];tform=maketform('affine',T);[g3,xdata3,ydata3]=imtransform(f,tform,'FillValue',255);hold onimshow(g3,'XData', xdata3, 'YData', ydata3)hold onimshow(g2,'XData', xdata2, 'YData', ydata2)hold onimshow(g1,'XData', xdata1, 'YData', ydata1)axis autoaxis on(2)改变图像的旋转角度,f= imread('flower.jpg');theta=3*pi/4;T=[cos(theta) sin(theta) 0; -sin(theta) cos(theta) 0; 0 0 1]; tform=maketform('affine',T);[g3,xdata3,ydata3]=imtransform(f,tform,'FillValue',255); theta=pi;T=[cos(theta) sin(theta) 0; -sin(theta) cos(theta) 0; 0 0 1]; tform=maketform('affine',T);[g4,xdata4,ydata4]=imtransform(f,tform,'FillValue',255); imshow(f);hold onimshow(g3,'XData', xdata3, 'YData', ydata3)hold onimshow(g4,'XData', xdata4, 'YData', ydata4)axis autoaxis on观察变换结果,要求把经过不同类型几何变换的图像和原图像在同一坐标系内显示输出(请参考课件或教材上的代码)3 读入一幅彩色图像,进行如下图像处理:(1)在RGB彩色空间中对图像进行模糊和锐化处理rgb= imread('flower.jpg');figure; imshow(rgb); title('原图');%平滑滤波r=rgb(:,:,1);g=rgb(:,:,2);b=rgb(:,:,3);m=fspecial('average',[8,8]);r_filtered=imfilter(r,m);g_filtered=imfilter(g,m);b_filtered=imfilter(b,m);rgb_filtered=cat(3,r_filtered,g_filtered,b_filtered);figure; imshow(rgb_filtered); title('模糊后');imwrite(rgb_filtered, 'RGB彩色空间模糊后.jpg');%拉普拉斯lapMatrix=[1 1 1;1 -8 1;1 1 1];i_tmp=imfilter(rgb,lapMatrix,'replicate');i_sharped=imsubtract(rgb,i_tmp);figure; imshow(i_sharped); title('锐化后'); imwrite(i_sharped, 'RGB彩色空间锐化后.jpg');(2)在HSI彩色空间中,对H分量图像进行模糊和锐化处理,转换回RGB格式并观察效果(3)在HSI彩色空间中,对S分量图像进行模糊和锐化处理,转换回RGB格式并观察效果(4)在HSI彩色空间中,对I分量图像进行模糊和锐化处理,转换回RGB格式并观察效果fc = imread('flower.jpg');h = rgb2hsi(fc);H = h (:,:,1);S = h (:,:,2);I = h (:,:,3);subplot(3,3,1);imshow(fc); title('原图');%平滑滤波m=fspecial('average',[8,8]);h_filtered=imfilter(H,m);img_h_filtered = cat(3,h_filtered,S,I);rgb_h_filtered = hsi2rgb(img_h_filtered);subplot(3,3,2);imshow(rgb_h_filtered); title('H分量模糊后');imwrite(rgb_h_filtered, 'H分量模糊后.jpg');%拉普拉斯lapMatrix=[1 1 1;1 -8 1;1 1 1];i_tmp=imfilter(H,lapMatrix,'replicate');H_sharped=imsubtract(H,i_tmp);img_h_sharped = cat(3,H_sharped,S,I);rgb_h_sharped = hsi2rgb(img_h_sharped);subplot(3,3,3); imshow(rgb_h_sharped); title('H分量锐化后'); imwrite(rgb_h_sharped, 'H分量锐化后.jpg');subplot(3,3,4);imshow(fc); title('原图');%平滑滤波m=fspecial('average',[8,8]);s_filtered=imfilter(S,m);img_s_filtered = cat(3,H,s_filtered,I);rgb_s_filtered = hsi2rgb(img_s_filtered);subplot(3,3,5);imshow(rgb_s_filtered); title('S分量模糊后'); imwrite(rgb_s_filtered, 'S分量模糊后.jpg');%拉普拉斯lapMatrix=[1 1 1;1 -8 1;1 1 1];i_tmp=imfilter(S,lapMatrix,'replicate');s_sharped=imsubtract(S,i_tmp);img_s_sharped = cat(3,H,s_sharped,I);rgb_s_sharped = hsi2rgb(img_s_sharped);subplot(3,3,6); imshow(rgb_s_sharped); title('S分量锐化后'); imwrite(rgb_s_sharped, 'S分量锐化后.jpg');subplot(3,3,7);imshow(fc); title('原图');%平滑滤波m=fspecial('average',[8,8]);i_filtered=imfilter(I,m);img_i_filtered = cat(3,H,S,i_filtered);rgb_i_filtered = hsi2rgb(img_i_filtered);subplot(3,3,8);imshow(rgb_i_filtered); title('I分量模糊后'); imwrite(rgb_i_filtered, 'I分量模糊后.jpg');%拉普拉斯lapMatrix=[1 1 1;1 -8 1;1 1 1];i_tmp=imfilter(I,lapMatrix,'replicate');i_sharped=imsubtract(I,i_tmp);img_i_sharped = cat(3,H,S,i_sharped);rgb_i_sharped = hsi2rgb(img_i_sharped);subplot(3,3,9); imshow(rgb_i_sharped); title('I分量锐化后'); imwrite(rgb_i_sharped, 'I分量锐化后.jpg');由图看出I分量图像进行模糊和锐化处理的效果最好。