matlab练习程序(透视变换)

close all;
clc;
H=1;                        %索引pix中第一个元素,即高度
W=2;                        %索引pix中第二个元素,即宽度
left_right=0.3;               %抬起左边或右边时值为0-1之间,不抬起时为0
up_down=0;                %抬起上边或下边时值为0-1之间,不抬起时为0

img=imread('lena.jpg');       %这里v为原图像的高度,u为原图像的宽度
imshow(img);                    %这里y为变换后图像的高度,x为变换后图像的宽度
%img=flipud(img);           %注释掉为抬起下边,没注释掉为抬起上边
%img=fliplr(img);           %注释掉为抬起右边,没注释掉为抬起左边
[v u]=size(img);


a=1;b=up_down;c=0;
d=left_right;e=1;f=0;
g=up_down/v;h=left_right/u;i=1;
rot=[a b c;d e f;g h i];

pix1=[1 1 1]*rot./(g+h+i);                 %变换后图像左上点的坐标
pix2=[1 u 1]*rot./(g*v+h+i);               %变换后图像右上点的坐标
pix3=[v 1 1]*rot./(g+h*u+i);               %变换后图像左下点的坐标
pix4=[v u 1]*rot./(g*v+h*u+i);             %变换后图像右下点的坐标

height=round(max([abs(pix1(H)-pix3(H))+0.5 abs(pix2(H)-pix3(H))+0.5 ...
                  abs(pix1(H)-pix4(H))+0.5 abs(pix2(H)-pix4(H))+0.5]));     %变换后图像的高度

width=round(max([abs(pix1(W)-pix2(W))+0.5 abs(pix3(W)-pix2(W))+0.5 ...
                 abs(pix1(W)-pix4(W))+0.5 abs(pix3(W)-pix4(W))+0.5]));      %变换后图像的宽度
imgn=zeros(height,width);

delta_y=abs(min([pix1(H)-0.5 pix2(H)-0.5 pix3(H)-0.5 pix4(H)-0.5]));            %取得y方向的负轴超出的偏移量
delta_x=abs(min([pix1(W)-0.5 pix2(W)-0.5 pix3(W)-0.5 pix4(W)-0.5]));            %取得x方向的负轴超出的偏移量

for y=1-floor(delta_y):height-floor(delta_y)
    for x=1-floor(delta_x):width-floor(delta_x)
        pix=[y x 1]/rot*(g*y+h*x+i);                                %用变换后图像的点的坐标去寻找原图像点的坐标,                                         
                                                            %否则有些变换后的图像的像素点无法完全填充
        if pix(H)>=0.5 && pix(W)>=0.5 && pix(H)<=v && pix(W)<=u
            imgn(y+floor(delta_y),x+floor(delta_x))=img(round(pix(H)),round(pix(W)));
        end   
        
    end
end
figure,imshow(uint8(imgn));

%%二次变换,暂时只想起来这种笨方法,一次变换好像没法进行透视变换
img=imgn;
[v u]=size(img);
a=1;b=-b/2;c=0;
d=-d/2;e=1;f=0;
g=0;h=0;i=1;
rot=[a b c;d e f;g h i];

pix1=[1 1 1]*rot./(g+h+i);                 %变换后图像左上点的坐标
pix2=[1 u 1]*rot./(g*v+h+i);               %变换后图像右上点的坐标
pix3=[v 1 1]*rot./(g+h*u+i);               %变换后图像左下点的坐标
pix4=[v u 1]*rot./(g*v+h*u+i);             %变换后图像右下点的坐标

height=round(max([abs(pix1(H)-pix3(H))+0.5 abs(pix2(H)-pix3(H))+0.5 ...
                  abs(pix1(H)-pix4(H))+0.5 abs(pix2(H)-pix4(H))+0.5]));     %变换后图像的高度

width=round(max([abs(pix1(W)-pix2(W))+0.5 abs(pix3(W)-pix2(W))+0.5 ...
                 abs(pix1(W)-pix4(W))+0.5 abs(pix3(W)-pix4(W))+0.5]));      %变换后图像的宽度
imgn=zeros(height,width);

delta_y=abs(min([pix1(H)-0.5 pix2(H)-0.5 pix3(H)-0.5 pix4(H)-0.5]));            %取得y方向的负轴超出的偏移量
delta_x=abs(min([pix1(W)-0.5 pix2(W)-0.5 pix3(W)-0.5 pix4(W)-0.5]));            %取得x方向的负轴超出的偏移量

for y=1-floor(delta_y):height-floor(delta_y)
    for x=1-floor(delta_x):width-floor(delta_x)
        pix=[y x 1]/rot*(g*y+h*x+i);                                %用变换后图像的点的坐标去寻找原图像点的坐标,                                         
                                                            %否则有些变换后的图像的像素点无法完全填充
        if pix(H)>=0.5 && pix(W)>=0.5 && pix(H)<=v && pix(W)<=u
            imgn(y+floor(delta_y),x+floor(delta_x))=img(round(pix(H)),round(pix(W)));
        end   
        
    end
end
%imgn=flipud(imgn);             %注释掉为抬起下边,没注释掉为抬起上边
%imgn=fliplr(imgn);             %注释掉为抬起右边,没注释掉为抬起左边
figure,imshow(uint8(imgn));     

图片福利:

注:更好的实现在这里

参考网址:

1.http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/EPSRC_SSAZ/epsrc_ssaz.html

2.http://alumni.media.mit.edu/~cwren/interpolator/

3.http://www.robots.ox.ac.uk/~vgg/presentations/bmvc97/criminispaper/

posted @ 2012-04-11 22:10  Dsp Tian  阅读(16710)  评论(1编辑  收藏  举报