简体   繁体   English

在给定两个角点的情况下计算图像的旋转度?

[英]Calculate degree of rotation of an image given two corner points?

I'm trying to write a C# console application that takes an image with text in it and rotate it so the text is as close to horizontal as possible so that OCR is more accurate. 我正在尝试编写一个C#控制台应用程序,该应用程序将在其中包含文本的图像并将其旋转,以使文本尽可能接近水平,以使OCR更准确。

Using AWS Rekognition I can get a bounding box with corner points which provide a guide as to the rotation needed however I don't have the maths ability to work out what sort of algorithm would work best. 使用AWS Rekognition,我可以获得带有角点的边界框,该边界框提供了所需旋转的指南,但是我没有数学能力来确定哪种算法最合适。

As an example, AWS will provide these relative coordinates: 例如,AWS将提供以下相对坐标:

"X": 0.6669167280197144, "Y": 0.6940382719039917

"X": 0.759939968585968, "Y": 0.681664764881134

"X": 0.7622751593589783, "Y": 0.7211361527442932

"X": 0.6692519187927246, "Y": 0.7335096001625061

So far, I've tried using either the top or bottom "Y" coordinates to create a ratio and then multiplying that by either 6 or -6. 到目前为止,我已经尝试使用顶部或底部的“ Y”坐标来创建比率,然后将其乘以6或-6。

This is the code I've tried. 这是我尝试过的代码。

var rotationAngle = (int)Math.Round(coordRatio * 6 * rotationDirection);

OR 要么

var rotationAngle = Math.Sin(Math.Cos(coordRatio * Math.PI)) * 10 * rotationDirection;

This seems like it should be reasonably simple trig. 这似乎应该是相当简单的触发。

Given the x and y coordinates of a point we can get the angle of that point in radians clockwise from the 'up' vector ( {x:0, y:1} ) using the Math.Atan2 method: 给定一个点的xy坐标,我们可以使用Math.Atan2方法从“向上”向量( {x:0, y:1} )顺时针以弧度为单位获取该点的角度:

double x = 1;
double y = 1;
double radians = Math.Atan2(x, y);

radians is set to 0.7853981... (or 45 degrees). radians设置为0.7853981... (或45度)。

We can use this to find the angle between two points by finding the different between the points and calculate the angle as above: 我们可以使用它来找到两点之间的角度,方法是找出两点之间的差异并按上述方法计算角度:

Point p1 = new Point(0.6669167280197144d, 0.6940382719039917d);
Point p2 = new Point(0.759939968585968d, 0.681664764881134);
Point difference = p2 - p1;
double radians = Math.Atan2(difference.X, difference.Y);

(Using a very simple Point class I just threw together.) (使用一个非常简单的Point类,我将它们放在一起。)

This results in 1.70303529... radians ( 97.5767... degrees). 结果为1.70303529...弧度( 97.5767...度)。 Walking the list of points gives us... some strange results: 走点列表给我们...一些奇怪的结果:

          Radians     Degrees
-------------------------------
p1 - p0   1.7030353   97.576734
p2 - p1   0.0590928   3.3857640
p3 - p2  -1.4385580  -82.423302
p0 - p3  -3.0824998  -176.61423

Sadly it doesn't look like this is a simple problem after all. 遗憾的是,看起来这毕竟不是一个简单的问题。 We can figure out the angles between any two points, but it doesn't look like we've got a proper rectangle defined here. 我们可以算出任意两点之间的角度,但是看起来我们在这里没有定义一个合适的矩形。 It looks like it might be a parallelogram. 看起来可能是平行四边形。 We have two pairs of roughly parallel lines, but those pairs are not at right angles. 我们有两对大致平行的线,但是这两对线不成直角。 Internal angles are approximately 94.2 and 85.8 degrees. 内角约为94.2度和85.8度。

In order to fix this image you'll probably want to start by rotating it so that the longest sides ( [p0,p1] and [p3,p0] ) are horizontal. 为了修复此图像,您可能需要先旋转图像,以使最长的边( [p0,p1][p3,p0] )是水平的。 This would be a rotation of ( 90 - 97.576734 = -7.576734 degrees, or 1.5707963 - 1.7030353 = -0.1322383 radians), preferably around the center of the points ( {X: 0.7145959, Y: 0.7075872} ). 这将是( 90 - 97.576734 = -7.576734度,或1.5707963 - 1.7030353 = -0.1322383弧度)的1.5707963 - 1.7030353 = -0.1322383 ,最好是围绕点的中心旋转( {X: 0.7145959, Y: 0.7075872} )。 Then you can work out the skew value and fix the parallelogram back into a rectangle. 然后,您可以计算出偏斜值并将平行四边形固定回矩形。

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

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