Perspective transform

  • Follow


Hi!
Im trying to program a method for perspective transformation using this piece of theory: http://alumni.media.mit.edu/~cwren/interpolator/

The problem is I get strange indexes when computing the mapping. Im doing a inverse of the " non-vector form" stated in the article..
Any help is appreciated! Maybe im trying to reinvent something that already exists?
Anders

function squareIm = perspectiveTrans(im,points)
% X,Y: samples from old corners
X = points(:,1);
Y = points(:,2);

% x,y: New Corner coordinates
x = [1 size(im,1) size(im,1) 1]';
y = [1 1 size(im,2) size(im,2)]';


% A matrix
A = [x(1) y(1) 1 0 0 0 (-X(1)*x(1)) (-X(1)*y(1));
     0 0 0 x(1) y(1) 1 (-Y(1)*x(1)) (-Y(1)*y(1));
     
     x(2) y(2) 1 0 0 0 (-X(2)*x(2)) (-X(2)*y(2));
     0 0 0 x(2) y(2) 1 (-Y(2)*x(2)) (-Y(2)*y(2));
     
     x(3) y(3) 1 0 0 0 (-X(3)*x(3)) (-X(3)*y(3));
     0 0 0 x(3) y(3) 1 (-Y(3)*x(3)) (-Y(3)*y(3));
     
     x(4) y(4) 1 0 0 0 (-X(4)*x(4)) (-X(4)*y(4));
     0 0 0 x(4) y(4) 1 (-Y(4)*x(4)) (-Y(4)*y(4))];

% B matrix
for i=1:4
    B(i*2-1,1) = X(i);
    B(i*2,1) = Y(i);
end

 
% get parameters
unknowns = A\B;
a = unknowns(1); b = unknowns(2); c = unknowns(3); d = unknowns(4);
e = unknowns(5); f = unknowns(6); g = unknowns(7); h = unknowns(8);


% For each pixel in the new image, find what pixel in old it maps to.
for row=1:size(im,1)
    for col=1:size(im,2)
        oldR = ( (e-f*h)* row - (b-c*h)* col + (b*f-e*c) ) / ( (a*e - b*d) - (e*g - d*h)* row - (a*h - g*b)* col ); %"inverse of the projection formula";
        oldC = ( -(d-f*g)* row + (a-c*g)* col - (a*f-c*d) ) / ( (a*e - b*d) - (e*g - d*h)* row - (a*h - g*b)* col ); %"inverse of the projection formula";
        squareIm(row,col) = im(round(oldR),round(oldC));
    end
end
0
Reply Anders 10/16/2010 6:10:05 PM

These are my opinions: The central point of the content available in the link that you sent was:

If I give you n points in one plane and give their mapped views in another plane, is there a transform matrix (which the author calls the perspective projection because of the geometrical approach he takes). The key question is: how many such points do you need to find the 9 values of the matrix. Also, the fact that these n points lying on a plane gives you additional equations (nice!). It is the classic question of solving m equations in n variables. If the number of independent equations be less than the number being solved for, you are lost. The pseudo inverse method that is being used is the solution of the minimization of the cost function of the squares of the errors. 

You should simply use the formula he has used in the article under the section for "The Solution". Get the matrix dimension right. Some ideas:

http://www.mathworks.com/help/techdoc/ref/pinv.html

The more interesting problem would have been if the planes were curved surfaces.
0
Reply Saurabh 10/21/2010 7:59:04 PM


"Anders " <lm06aj6@student.lth.se> wrote in message <i9cppt$im1$1@fred.mathworks.com>...
> Hi!
> Im trying to program a method for perspective transformation using this piece of theory: http://alumni.media.mit.edu/~cwren/interpolator/

Anders,

You might find additional resources if you search under the more standard term "projective transformation." Perspective typically involves mapping 3D to 2D, but you are mapping 2D to 2D.

Also, the Image Processing Toolbox function cp2tform solves this very problem. (From your variable names, it looks as if your application might involve imagery.) Here's one place to get started:

http://www.mathworks.com/help/toolbox/images/f12-26140.html

Hope this helps,

Rob Comer
Mapping Toolbox Development
MathWorks
0
Reply Rob 10/22/2010 12:16:03 AM

freelance writer
0
Reply EUGENIAODOM26 5/13/2011 9:22:12 PM

freelance writer
0
Reply ClaudineSchwartz29 5/13/2011 9:44:44 PM

freelance writer
0
Reply Miranda22Ollie 5/14/2011 4:04:12 AM

8 Replies
824 Views

(page loaded in 0.476 seconds)

Similiar Articles:













7/23/2012 11:29:25 AM


Reply: