仿射变换、简单旋转和缩放或其他完全?

2022-01-16 00:00:00 opencv image-processing c rotation c++

场景是这样的:我有一张纸的图片,我想做一些 OCR.所以把下面的图片作为我的输入示例:

The scenario goes like this: I have a picture of a paper that I would like to do some OCR. So take the image below as my input example:

在成功检测到与纸张相对应的区域后,我留下了一个由 4 个坐标组成的 vector,用于定义其在图像中的位置.请注意,由于相机的距离和拍摄照片时的角度,这些坐标可能不会对应于完美的矩形.出于查看目的,我连接了子图像中的点,以便您了解我的意思:

After successfully detecting the area that corresponds to the paper I'm left with a vector<Point> of 4 coordinates that define its location inside the image. Note that these coordinates will probably not correspond to a perfect rectangle due to the distance of the camera and angle when the picture was taken. For viewing purposes I connected the points in the sub-image so you can see what I mean:

在这种情况下,点是: [1215, 43] , [52, 67] , [56, 869] 和 [1216, 884]

In this case, the points are: [1215, 43] , [52, 67] , [56, 869] and [1216, 884]

此时,我需要调整这些点,使它们水平对齐.我的意思是什么?如果您注意到上面子图像的区域,它有点旋转:图像右侧的点比另一侧的点位置稍高.

At this moment, I need to adjust these points so they become aligned horizontally. What do I mean by that? If you notice the area of the sub-image above, it is a little rotated: the points on right side of the image are positioned a little higher than points on the other side.

换句话说,我们有图像 A,它被故意夸大以看起来比现实更扭曲/旋转,然后是图像 B - 这就是我想作为这个过程的最终结果:

In other words, we have image A, which was exaggerated on purpose to look a little more distorted/rotated than reality, and then image B - which is what I would like as the final result of this procedure:

A) B)

我不确定哪些技术可以用来实现这种转变.该应用程序还需要自动检测需要进行多少旋转,因为我无法控制图像采集过程.

I'm not sure which techniques could be used to achieve this transformation. The application also needs to detect automatically how much rotation needs to be done, as I don't have control over the image acquisition procedure.

目的是有一个新的 Mat 与 规范化 子图像.我现在并不担心可能的图像失真,我只是在寻找一种方法来确定需要对子图像进行多少旋转以及如何应用它并获得更多 矩形区域.

The purpose is to have a new Mat with the normalized sub-image. I'm not worried about a possible image distortion right now, I'm just looking for a way to identify how much rotation needs to be done on the sub-image and how to apply it and get a more rectangular area.

推荐答案

我认为 http://felix.abecassis.me/2011/10/opencv-rotation-deskewing/ 和 http://felix.abecassis.me/2011/10/opencv-bounding-box-skew-angle/ 会派上用场.上述帖子不涉及透视变形(仅旋转).为了获得最佳结果,您必须使用 warpPerspective(可能与 getRotationMatrix2D 结合使用).使用线段之间的角度来找出你需要扭曲透视的程度.这里的假设是它们应该始终是 90 度,并且就视角而言,最接近 90 度的是最接近"的向量.

I think http://felix.abecassis.me/2011/10/opencv-rotation-deskewing/ and http://felix.abecassis.me/2011/10/opencv-bounding-box-skew-angle/ will come in handy. The aforementioned posts don't cover perspective warping (only rotation). To get the best results, you'll have to use warpPerspective (maybe in conjunction with getRotationMatrix2D). Use the angles between line segments to find out how much you need to warp the perspective. THe assumption here is that they should always be 90 degrees and that the closest one to 90 degrees is the "closest" vector as far as the perspective is concerned.

别忘了标准化你的向量!

Don't forget to normalize your vectors!

相关文章