我在通过 OCR 处理发票的程序中发现了一个有趣的问题。
在一步中,我对图像进行了歪斜校正,现在我有了一个特定的用例。
原始文件(解密):
旋转文件(解密):
我本以为旋转大约是 -15 度,但程序告诉我是 -2.2 度。
使用的代码:
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;
}
如您所见,代码似乎工作正确,但谁是正确的?我如何找出图像的实际旋转?到底是怎么回事?
最佳答案
看看这一行:
它的尺寸是:472 * 20。对其应用 arctan 函数:
atan(20/472) = 0.042347549 radians
将其转换为度数,您有:
2.42633583 degrees
因此,您的程序运行正常,您对 15 度的估计完全不对。
关于Java 图像旋转 : Is the computed angle correct?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20904110/