简体   繁体   English

OpenCV Python对齐元素的4个角

[英]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: 我具有该元素4个角的位置,但我想将其弄直。我的图像看起来像这样:

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. 首先,您需要图像的4个角点的位置,然后必须定义这4个点的目的地。 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 ). 如果您有目的地,则可以调用函数H, mask = cv.findHomography(srcPoints, dstPoints) (请参阅calib3d模块中的文档)。

The matrix H describes the transformation from your source image to the destination image. 矩阵H描述了从源图像到目标图像的转换。 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 ). 接下来,您需要应用矩阵H图像可与功能上做dst = cv.warpPerspective(srcImage, destImage,H)看到这个函数的文档在这里 )。

If you want to do more stuff with the homography matrix, see OpenCv Homography tutorial with example code 如果您想使用单应性矩阵做更多的事情,请参阅带有示例代码的OpenCv Homography教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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