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:


69 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. 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. افضل شركة رش مبيدات بالرياض
    الصفرات لرش المبيدات
    شركة الصفرات لرش المبيدات بالرياض

    ReplyDelete
  12. 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
  13. Free Xbox gift cardsI enjoyed over read your blog post. Your blog have nice information,
    I got good ideas from this amazing blog.

    ReplyDelete
  14. 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
  15. 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
  16. Your post has really helped me a lot. I live in a different country than you, but I believe it will help a lot in my country. 토토사이트 If possible, please visit my website as well. Thank you.

    ReplyDelete
  17. Yes, I am entirely agreed with this article, and I just want say that this article is very helpful and enlightening. I also have some precious piece of concerned info !!!!!!Thanks. 먹튀검증

    ReplyDelete
  18. I’m still learning from you, while I’m improving myself. I certainly enjoy reading everything that is written on your website.Keep the posts coming. I loved it!
    먹튀검증

    ReplyDelete
  19. Your site is excellent. I was really touched by this short article. I'm so delighted to discover the write-up I have actually been searching for a long period of time. I covet the number of individuals sympathize with your writing. You discover a great deal of expertise from my writing.실시간 바카라사이트

    ReplyDelete
  20. Keep working like that!. back scratcher shoe horn I'll come often. It's nice to see a lot of good comments here. Thank you. 토토추천

    ReplyDelete
  21. After reading your article I was amazed. I know that you explain 안전공원추천 blog is really good and impressive you made it mice

    ReplyDelete
  22. 먹튀검증 very internet smart so I’m not 100% sure. Any tips or advice would be greatly appreciated.

    ReplyDelete
  23. I was able to think a lot while looking at this post. I will pass this blog to many people. Definitely a good blog is very good. Thank you for sharing. Feel free to visit my website; 야설

    ReplyDelete
  24. I want to we appreciate you this passion you cash in on throughout establishing the next few paragraphs. I am trustworthy identical best work from you when you need it at the same time. Feel free to visit my website; 일본야동

    ReplyDelete
  25. Very good written information. It will be valuable to anybody who employess it, as well as yours truly :). Keep up the good work – for sure i will check out more posts. Feel free to visit my website; 국산야동

    ReplyDelete
  26. I'm glad that i found this type of site, after so many hours of searching of a marvelous site like yours so. Thanks a lot. Feel free to visit my website;
    일본야동

    ReplyDelete
  27. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article :D 먹튀검증


    ReplyDelete
  28. Hey! I know this is kinda off topic however I'd figured I'd
    ask. Would you be interested in trading links or maybe guest
    writing a blog article or vice-versa? My site goes over a lot of the same subjects as yours and I believe we could
    greatly benefit from each other. If you're interested feel free to send me an email.
    I look forward to hearing from you! Wonderful blog by the way!
    토토사이트

    ReplyDelete
  29. I am regular reader, how are you everybody? This paragraph posted at this site is actually
    good. 경마사이트

    ReplyDelete
  30. Papers get to operate through Bible helpers and also plagiarism checkers to be certain that the author did not find idle at any stage in the process. Language inconsistencies won't be a thing with your newspapers as the specialists of our school paper writing support are passionate about writing and are always excited to strike you with something fresh. By: 안전놀이터


    ReplyDelete
  31. I'm so happy to finally find a post with what I want. casino You have inspired me a lot. If you are satisfied, please visit my website and leave your feedback.


    ReplyDelete
  32. How can you think of this? I thought about this, but I couldn't solve it as well as you.안전놀이터I am so amazing and cool you are. I think you will help me. I hope you can help me.


    ReplyDelete
  33. I have read your article; it is very informative and helpful for me. I admire the valuable information you offer in yo ur articles. Thanks for posting it. Feel free to visit my website; 야설


    ReplyDelete
  34. Thanks for the blog filled with so many information. Stopping by your blog helped me to get what I was looking for. Now my task has become as easy as ABC. 안전놀이터


    ReplyDelete
  35. Don't go past my writing! Please read my article only once. Come here and read it once"크레이지슬롯


    ReplyDelete
  36. Hello, I am one of the most impressed people in your article. sòng bạc I'm very curious about how you write such a good article. Are you an expert on this subject? I think so. Thank you again for allowing me to read these posts, and have a nice day today. Thank you.


    ReplyDelete
  37. Hello, I am one of the most impressed people in your article. 슬롯사이트 If possible, please visit my website as well. Thank you.


    ReplyDelete
  38. When I read your article on this topic, the first thought seems profound and difficult. There is also a bulletin board for discussion of articles and photos similar to this topic on my site, but I would like to visit once when I have time to discuss this topic. 바카라사이트I think it would be nice if you come to if you can solve my problem.


    ReplyDelete
  39. 온라인카지노사이트 I really fascinated by this excellent blog, thanks for such wonderful blog! Good job and more power!

    ReplyDelete
  40. 카지노사이트 I’ve been watching out for your content. If ever you have some more updates please do notify me.

    ReplyDelete
  41. 토토 Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I’ll be sure to check back again real soon.

    ReplyDelete
  42. These are really useful and good information. If you want more information,
    come to my link and get better information..먹튀폴리스

    ReplyDelete
  43. Nice post! This is a very nice blog that I will definitively come back to more times this year! 스포츠중계
    Thanks for informative post

    ReplyDelete
  44. Thank you for some other informative blog. Where else could I get that type of information written in such an ideal means? I have a mission that I’m just now working on, and I have been at the look out for such information. 먹튀검증 It helped me a lot. If you have time, I hope you come to my site and share your opinions. Have a nice day.


    ReplyDelete
  45. 메이저사이트April 20, 2022 at 3:48 PM

    It has a good meaning. If you always live positively, someday good things will happen. 메이저사이트Let's believe in the power of positivity. Have a nice day.


    ReplyDelete
  46. 메이저놀이터April 25, 2022 at 2:40 PM

    I figure this article can be enhanced a tad. There are a couple of things that are dangerous here, and if you somehow managed to change these things, this article could wind up a standout amongst your best ones. I have a few thoughts with respect to how you can change these things. 메이저놀이터


    ReplyDelete
  47. Thank you so much for such a well-written article. It’s full of insightful information. Your point of view is the best among many without fail.For certain, It is one of the best blogs in my opinion. 먹튀검증


    ReplyDelete
  48. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article :D 먹튀검증


    ReplyDelete
  49. Which is some inspirational stuff. Never knew that opinions might be this varied. Thank you for all the enthusiasm to provide such helpful information here. 온라인바카라
    dfxhh

    ReplyDelete
  50. That's exactly what I've been looking for
    검증카지노

    Your information was very useful to me.
    온라인카지노

    ReplyDelete
  51. pg slot asia สล็อต เป็นเว็บไซต์พนันสล็อตออนไลน์ ลำดับหนึ่ง ที่คนอีกหลายๆคนนั้นต้องเคยได้ฟังชื่อนี่กันมาบ้างแล้วเพราะเว็บไซต์ของพวกเรานั้น PG SLOT เปิดให้บริการโกรธยะหนึ่ง

    ReplyDelete
  52. PG JOKER มีโปรโมชั่นมากมาย ให้คุณได้เลือก สิทธิพิเศษ สำหรับสมาชิกสุดพิเศษของพวกเรา เมื่อลงทะเบียนสมัครสมาชิกใหม่ รับโบนัสโดยทันที ของยอดฝาก เกมสนุกแถมได้เงินไม่อั้นพีจีสล็อตเว็บเดียวเท่านั้น

    ReplyDelete
  53. 도박꾼 카지노 커뮤니티 Best Casino Community Gamblers 카지노커뮤니티

    ReplyDelete
  54. Your post was very helpful and easy to follow. Combined with 123 hp com setup, I finally managed to get my printer up and running without any issues."

    ReplyDelete