简体   繁体   中英

OpenCV Python align 4 corners of an element

I have the location of the 4 corners of the element but I want to straighten it My image looks like this representation:

From misaligned to aligned element

The said element is in an image so I'd like to apply the alignment to the whole image.

What you are searching for is a homography Transformation.

First you need the positions of your 4 corner points of the image and then your have to define the destination of those 4 points. In your case it would be the image boundaries. If you have your destination points, you can call the function H, mask = cv.findHomography(srcPoints, dstPoints) (see documentation in calib3d module ).

The matrix H describes the transformation from your source image to the destination image. Next you need to apply the Matrix H on your image which can be done with the function dst = cv.warpPerspective(srcImage, destImage,H) (see documentation of this function here ).

If you want to do more stuff with the homography matrix, see OpenCv Homography tutorial with example code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM