简体   繁体   English

Java Image Rotation:计算的角度是否正确?

[英]Java Image Rotation: Is the computed angle correct?

I've found an interesting issue in the program that I'm working on to process invoices via an OCR. 我正在通过OCR处理发票的程序中发现了一个有趣的问题。
In one step I deskew the image, now I have a particular use case. 第一步,我对图像进行了校正,现在有了一个特定的用例。

Original file (declassified): 原始文件(已解密):

原版的

Rotated file (declassified): 旋转文件(已解密):

旋转的

I was thinking by heart that the rotation would be around -15 degrees, however the program tells me that it is -2.2 degrees. 我一直心里在想旋转大约是-15度,但是程序告诉我旋转是-2.2度。

The code used: 使用的代码:

private BufferedImage rotateImage(BufferedImage image, float angleDegrees, int imageType) {
    long start = System.currentTimeMillis();
    angleDegrees %= 360;
    BufferedImage returnImage = new BufferedImage(image.getWidth(), image.getHeight(), imageType);
    Graphics2D g2d = returnImage.createGraphics();
    g2d.rotate(Math.toRadians(angleDegrees), returnImage.getWidth() / 2, returnImage.getHeight() / 2);
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();
    long end = System.currentTimeMillis();
    float duration = 1.0f * (end - start) / 1000;
    //System.out.println("Duration: " + duration + " seconds");
    return returnImage;
}

As you can see the code seems be working correctly , but who is correct? 正如你所看到的代码似乎工作正常 ,但谁是正确的? How would I figure out the actual rotation of the image? 如何计算图像的实际旋转度? What exactly is going on? 到底是怎么回事?

Take a look at this line: 看一下这一行:

在此处输入图片说明

It's dimensions are: 472 * 20. Apply the arctan function to it: 它的尺寸为:472 *20。对其应用arctan函数:

atan(20/472) = 0.042347549 radians

Convert this to degrees and you have: 将此转换为度,您将:

2.42633583 degrees

So, your program works correctly, and your estimation of 15 degrees was simply off. 因此,您的程序可以正常运行,并且您对15度的估计完全不正确。

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

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