IDL图像旋转间接法双线性内插法 | 您所在的位置:网站首页 › cosa的图像 › IDL图像旋转间接法双线性内插法 |
;间接法图像旋转代码 pro test ;***1. 输入旋转文件,可以修改 file = 'C:\Users\ASUS\Desktop\ch06-2作业图像旋转idl程序\jz512-rgb.tif' ;***1. 输入旋转角度,可以修改 angle = 20 ;***2. 读取 tiff 格式图像 tm_data = READ_TIFF(file) dims = size(tm_data, /DIMENSIONS) ;图像数据大小,行,列 print,dims ;***3. 显示图像 ;定义显示窗口,和数据同样大的窗口 window, 0, xsize = dims[1], ysize = dims[2], $ TITLE = 'Landsat TM data of Jiaozuo' ;显示图像,注意加入order关键字 ,图像变正 TV, tm_data,TRUE=1,/order pi=3.14159265 ;***4. 定义旋转矩阵, ;还记得旋转矩阵的使用吗??? ;[x',y']=|cosA, sinA| * [x,y] ; |-sinA,cosA| Rot=[[cos(angle*pi/180),sin(angle*pi/180)], $ [-1*sin(angle*pi/180),cos(angle*pi/180)]] ;***5. 定义四个角点坐标,[0,0]为原点 BT1=[0,0] ;左上角点坐标 BT2=[0,dims[1]-1] ;右上角点坐标 BT3=[dims[2]-1,0] ;左下角点坐标 BT4=[dims[2]-1,dims[1]-1] ;右下角点坐标 BT1_rot=Rot ;旋转左上角点坐标 BT2_rot=Rot#BT2 ;旋转右上角点坐标 BT3_rot=Rot#BT3 ;旋转左下角点坐标 BT4_rot=Rot#BT4 ;旋转右下角点坐标 x_min=min([BT1_rot[0],BT2_rot[0],BT3_rot[0],BT4_rot[0]]) ;旋转后图像最小x值 y_min=min([BT1_rot[1],BT2_rot[1],BT3_rot[1],BT4_rot[1]]) ;旋转后图像最小y值 x_max=max([BT1_rot[0],BT2_rot[0],BT3_rot[0],BT4_rot[0]]) ;旋转后图像最大x值 y_max=max([BT1_rot[1],BT2_rot[1],BT3_rot[1],BT4_rot[1]]) ;旋转后图像最大y值 width=x_max-x_min+1 ;旋转后图像宽度 height=y_max-y_min+1 ;旋转后图像高度 ;***6. 新建MxNx3的空矩阵,用于存储旋转后彩色图像 img_rot=intarr(3,height,width) ;***7. *********采用双线性内插法旋转图像******** for i = 0, width-1 do begin for j = 0, height-1 do begin x_rot=i+x_min y_rot=j+y_min ; 坐标变换 x = (x_rot*cos(angle*pi/180))+(y_rot*sin(angle*pi/180)) y = (y_rot*cos(angle*pi/180)) - (x_rot*sin(angle*pi/180)) ; 确定四个领域像素的坐标和权重 x1=floor(x) x2=x1+1 y1=floor(y) y2=y1+1 wx2=x-x1 wy2=y-y1 wx1=1-wx2 wy1=1-wy2 ; 边界情况的处理 if x1 lt 0 then x1=0 else if x1 ge dims[1]-1 then x1=dims[1]-1 if x2 lt 1 then x2=1 else if x2 ge dims[1] then x2=dims[1]-1 if y1 lt 0 then y1=0 else if y1 ge dims[2]-1 then y1=dims[2]-1 if y2 lt 1 then y2=1 else if y2 ge dims[2] then y2=dims[2]-1 ; 四个领域像素取值 p11=tm_data[*,x1,y1] p12=tm_data[*,x1,y2] p21=tm_data[*,x2,y1] p22=tm_data[*,x2,y2] ; 双线性内插计算 for k=0,2 do begin img_rot[k,j,i]=wx1*wy1*p11[k]+wx1*wy2*p12[k]+wx2*wy1*p21[k]+wx2*wy2*p22[k] endfor endfor endfor ;***8. 显示旋转后图像 dimsrot= size(img_rot, /DIMENSIONS) window, 1, xsize = dimsrot[1], ysize = dimsrot[2], $ TITLE = 'Rotation of Landsat TM data Jiaozuo' TV, img_rot, TRUE=1 ;***9.保存旋转图像为tiff格式 tif_rot_file = 'C:\Users\ASUS\Desktop\ch06-2作业图像旋转idl程序\jz512-rgb-rotation-bilinear.tif' WRITE_TIFF, tif_rot_file, img_rot print,'end pro' End |
CopyRight 2018-2019 实验室设备网 版权所有 |