Perspective Transform

Mat getPerspectiveTransform(InputArray src, InputArray dst)
- Calculates a perspective transform from four pairs of the corresponding points.

void warpPerspective(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
- Applies a perspective transformation to an image.
Parameters:
  • src – input image.
  • dst – output image that has the size dsize and the same type as src .
  • M3\times 3 transformation matrix.
  • dsize – size of the output image.
  • flags – combination of interpolation methods (INTER_LINEAR or INTER_NEAREST) and the optional flag WARP_INVERSE_MAP, that sets M as the inverse transformation ( \texttt{dst}\rightarrow\texttt{src} ).
  • borderMode – pixel extrapolation method (BORDER_CONSTANT or BORDER_REPLICATE).
  • borderValue – value used in case of a constant border; by default, it equals 0.

Example:

------------
#include<opencv2/opencv.hpp>

using namespace cv;

int main( )
{
    // Input Quadilateral or Image plane coordinates
    Point2f inputQuad[4]; 
    // Output Quadilateral or World plane coordinates
    Point2f outputQuad[4];
        
    // Lambda Matrix
    Mat lambda( 2, 4, CV_32FC1 );
    //Input and Output Image;
    Mat input, output;
    
    //Load the image
    input = imread( "lena.jpg", 1 );
    // Set the lambda matrix the same type and size as input
    lambda = Mat::zeros( input.rows, input.cols, input.type() );

    // The 4 points that select quadilateral on the input , from top-left in clockwise order
    // These four pts are the sides of the rect box used as input 
    inputQuad[0] = Point2f( -30,-60 );
    inputQuad[1] = Point2f( input.cols+50,-50);
    inputQuad[2] = Point2f( input.cols+100,input.rows+50);
    inputQuad[3] = Point2f( -50,input.rows+50  );  
    // The 4 points where the mapping is to be done , from top-left in clockwise order
    outputQuad[0] = Point2f( 0,0 );
    outputQuad[1] = Point2f( input.cols-1,0);
    outputQuad[2] = Point2f( input.cols-1,input.rows-1);
    outputQuad[3] = Point2f( 0,input.rows-1  );

    // Get the Perspective Transform Matrix i.e. lambda 
    lambda = getPerspectiveTransform( inputQuad, outputQuad );
    // Apply the Perspective Transform just found to the src image
    warpPerspective(input,output,lambda,output.size() );

    //Display input and output
    imshow("Input",input);
    imshow("Output",output);

    waitKey(0);
    return 0;
}
------------

Result:


33 comments:

  1. How about if I mapped just a point in an image (eg. I'm tracking an object) and want to get the new transformed point instead of the whole image? Any tip to a starter?
    Regards
    Edson

    ReplyDelete
    Replies
    1. You can track the individual points of the object and you may try with some prediction filters to predict the next position of the respective points. You may use Kalman filter for the purpose.

      Delete
    2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru JavaScript Online Training in India. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

      Delete
  2. Thank you very much. It is very helpful.
    Shouldn't the following be for "output" image instead of lambda (which is a 3X3 matrix)?

    // Set the lambda matrix the same type and size as input
    lambda = Mat::zeros( input.rows, input.cols, input.type() );

    ReplyDelete
  3. Thanks for your code, I used it for my project

    ReplyDelete
  4. i want to get an image aligned based on 6 co-ordinates (center of eyes,tip of the nose,mouth end points and the center of the mouth).how should i approach it?

    ReplyDelete
  5. That worked like a charm. Just one comment: You can remove any parameters for lambda and just write

    Mat lambda = getPerspective...

    It works the same.

    ReplyDelete
  6. Thanks for the info on perspective corrections.

    ReplyDelete
  7. Replies
    1. How to do perspective transformation using python and opencv | sorting out error
      refer this tutorial u may get some idea.
      https://youtu.be/VOv7el_NVU8

      Delete
  8. How to do perspective transformation using python and opencv | sorting out error
    https://youtu.be/VOv7el_NVU8

    ReplyDelete
  9. Can you please give the code in python

    ReplyDelete
  10. Can you please give the code in CSharp

    ReplyDelete

  11. Thanks for this informative article, I hope you will get most positive response specially for this post. . . .
    โกเด้นสล็อต

    ReplyDelete
  12. افضل شركة رش مبيدات بالرياض
    الصفرات لرش المبيدات
    شركة الصفرات لرش المبيدات بالرياض

    ReplyDelete
  13. Currently Python is the most popular Language in IT. Python adopted as a language of choice for almost all the domain in IT including Web Development, Cloud Computing (AWS, OpenStack, VMware, Google Cloud, etc.. ),Read More

    ReplyDelete
  14. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.
    Data Science Training in Chennai
    Data Science training in kalyan nagar
    Data science training in Bangalore
    Data Science training in marathahalli
    Data Science interview questions and answers
    Data science training in bangalore

    ReplyDelete
  15. I love this post.


    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร Gclub กับทางทีมงานของเราเพียงเท่านั้น
    ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
    สมัครสล็อตออนไลน์ >>> goldenslot
    สนใจร่วมลงทุนกับเรา สมัครเอเย่น Gclub คลิ๊กได้เลย

    ReplyDelete
  16. Very cool!

    เว็บไซต์คาสิโนออนไลน์ที่ได้คุณภาพอับดับ 1 ของประเทศ
    เป็นเว็บไซต์การพนันออนไลน์ที่มีคนมา สมัคร Gclub Royal1688
    และยังมีหวยให้คุณได้เล่น สมัครหวยออนไลน์ ได้เลย
    สมัครสมาชิกที่นี่ >>> Gclub Royal1688
    ร่วมลงทุนสมัครเอเย่นคาสิโนกับทีมงานของเราได้เลย

    ReplyDelete
  17. Our Pay for nursing papers for sale services makes your college life bearable and fulfilling by enabling you to pass your exams and also in other research and essay writing tasks.

    ReplyDelete
  18. Free Xbox gift cardsI enjoyed over read your blog post. Your blog have nice information,
    I got good ideas from this amazing blog.

    ReplyDelete
  19. Your blog was absolutely fantastic!
    Great deal of great information & this can be useful some or maybe the other way.
    Keep updating your blog,anticipating to get more detailed contents.

    บาคาร่าออนไลน์
    บาคาร่า
    baccarat online
    free online slot

    ReplyDelete
  20. Thanks for providing such a nice information. this post is really helpful. It takes strengths to build for the future.. it was really nice that you decided to share this information. LIC Merchant Info

    ReplyDelete
  21. My partner and I stumbled over here by a different page and thought I may as well check How to find routing number without check .I like what I see so i am just following you. Look forward to looking over your web page repeatedly.

    ReplyDelete