资源描述:
《matlab图像双线性插值,最近邻插值与几何变换》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验二图像几何变换与插值一、实验目的巩固图像处理编程的步骤格式,理解数据插值及图像几何变换的原理,掌握图像几何变换的实现方法。二、实验内容1、理解反向变换的实现思路2、图像缩放及插值Matlab取整命令:floor,ceil,round分别实验最近邻插值和双线性插值f=imread('lena.bmp');beishu=0.5;[row,col]=size(f);r1=round(row*beishu);c1=round(col*beishu);b=zeros(r1,c1);fori=1:r1forj=1:c1i1=round(i/beishu);j1=rou
2、nd(j/beishu);ifi1<1i1=1;endifj1<1j1=1;endb(i,j)=f(i1,j1);endendb=uint8(b);figure;imshow(f);figureimshow(b);3、图像旋转及插值以图像中心为轴实现任意角度(逆时针为正)的图像旋转,分别实验两种插值算法f=imread('lena.bmp');B=imrotate(f,45,'nearest','crop');C=imrotate(f,45,'bilinear','crop');figure;subplot(121);imshow(f);title('原图像'
3、);subplot(122);imshow(B);title('最近邻插值');figure;subplot(121);imshow(f);title('原图像');subplot(122);imshow(C);title('双线性插值');%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%matlab练习程序(图像放大/缩小,最邻近插值)ccccl;w=0.6;%放大或缩小的宽度h=1.4;%放大或缩小的高度img=imread('Corner.png');imshow(img);[mn
4、]=size(img);imgn=zeros(h*m,w*n);rot=[h00;0w0;001];%变换矩阵x=h*u,y=w*vinv_rot=inv(rot);forx=1:h*mfory=1:w*npix=[xy1]*inv_rot;imgn(x,y)=img(round(pix(1)),round(pix(2)));endendfigure,imshow(uint8(imgn))%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%matlab练习程序(图像放大/缩小,双线性插值)c
5、ccm=1.8;%放大或缩小的高度n=2.3;%放大或缩小的宽度img=imread('lena.jpg');imshow(img);[hw]=size(img);imgn=zeros(h*m,w*n);rot=[m00;0n0;001];%变换矩阵fori=1:h*mforj=1:w*npix=[ij1]/rot;float_Y=pix(1)-floor(pix(1));float_X=pix(2)-floor(pix(2));ifpix(1)<1 %边界处理pix(1)=1;endifpix(1)>hpix(1)=h;endifpix(2)<
6、1pix(2)=1;endifpix(2)>wpix(2)=w;endpix_up_left=[floor(pix(1))floor(pix(2))]; %四个相邻的点pix_up_right=[floor(pix(1))ceil(pix(2))];pix_down_left=[ceil(pix(1))floor(pix(2))];pix_down_right=[ceil(pix(1))ceil(pix(2))];value_up_left=(1-float_X)*(1-float_Y); %计算临近四个点的权重value_up_right=f
7、loat_X*(1-float_Y);value_down_left=(1-float_X)*float_Y;value_down_right=float_X*float_Y; %按权重进行双线性插值imgn(i,j)=value_up_left*img(pix_up_left(1),pix_up_left(2))+...value_up_right*img(pix_up_right(1),pix_up_right(2))+...value_down_left*img(pix_down_le
8、ft(1),pix_down_left