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.
  • 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.



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

    return 0;



  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?

    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.

    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

  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() );

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

  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?

  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.

  6. Thanks for the info on perspective corrections.

  7. how to flat a curved image??

    1. How to do perspective transformation using python and opencv | sorting out error
      refer this tutorial u may get some idea.

  8. How to do perspective transformation using python and opencv | sorting out error

  9. Can you please give the code in python

  10. Can you please give the code in CSharp


  11. Thanks for this informative article, I hope you will get most positive response specially for this post. . . .

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

  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

  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

  15. I love this post.

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

  16. Very cool!

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

  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.

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

  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

  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

  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.

  22. Hey! I would like to share this great free online file converter that can come handy. Click here to learn more

  23. It is normal for students to be anxious about hiring an online professional Legitimate Custom Research Paper Services Provider because they can never be sure whether they can get high-quality affordable Custom Research Paper Services and the right Research Paper Assistance Services or not.

  24. Choose high quality and durable dennys driveshaft replacement parts for your Nissan. Replacement parts are available for your air intake system, body electrical, body mechanical and trim, body sheet metal, brakes, climate control, clutch, cooling system, diesel injection, drive belts, drive shafts and axle, engine electrical, engine parts, exhaust, fuel delivery, steering, suspension, tools and hardware, transmission. Replacement parts keep your Nissan running and looking great, these parts will surely make it more stylish, more fun to drive, more comfortable and convenient, and more high-tech. dennys driveshaft .