数字图像的几何运算

数字图像的几何运算

实验任务

  • 图像的平移
  • 图像的旋转
  • 图像的缩放

源代码

f = imread('./test.TIF');
...imshow(f);

...图像的缩放
f_15 = imresize(f,1.5,'nearest');
f_15 = imresize(f,1.5,'bilinear');
f_15 = imresize(f,1.5,'bicubic');

f_5 = imresize(f,0.5,'nearest');
f_5 = imresize(f,0.5,'bilinear');
f_5 = imresize(f,0.5,'bicubic');

...图像的旋转
f_t = imrotate(f,30,'nearest','crop');
f_t = imrotate(f,30,'bilinear','crop');
f_t = imrotate(f,30,'bicubic','crop');

...图像的平移
x = 100;
y = 100;

f_g=rgb2gray(f);             
[nrows,ncols]=size(f_g);             
width=nrows;                            
height=ncols;
NewImage=uint8(zeros(width,height));       

T=[1 0 0;0 1 0;x y 1];                    
tform=maketform('affine',T);      

tx=zeros(width,height);
ty=zeros(width,height);
for i=1:width
    for j=1:height
        tx(i,j)=i;
    end
end
for i=1:width
    for j=1:height
        ty(i,j)=j;
    end
end
[w, z]=tforminv(tform,tx,ty);          

for i=1:width
    for j=1:height
    source_x=w(i,j);
    source_y=z(i,j);
    if (source_x>=width-1||source_y>=height-1||double(uint16(source_x))<=0||double(uint16(source_y))<=0)     %&&??? ||???
        NewIamge(i,j)=0;   
    else
        if(source_x/double(uint16(source_x))==1.0)&&(source_y/double(uint16(source_y))==1.0)

            NewImage(i,j)=f_g(int16(source_x),int16(source_y));
        else
            a=double(uint16(source_x));
            b=double(uint16(source_y));
            x11=double(f_g(a,b));
            x12=double(f_g(a,b+1));
            x21=double(f_g(a+1,b));
            x22=double(f_g(a+1,b+1));
            NewImage(i,j)=uint8((b+1-source_y)*((source_x-a)*x21+(a+1-source_x)*x11)+(source_y-b)*((source_x-a)*x22+(a+1-source_x)*x12));

        end
    end
    end
end
I=NewImage;
imshow(I)      

猜你喜欢

转载自blog.csdn.net/guoxuan_chn/article/details/70342199