问题描述
我正在开发一个处理银行支票的应用程序.但是,当银行的支票图像可以偏斜或略微旋转最大20度的角度时.在处理支票之前,我需要正确对齐此歪斜的图像.我被困在这里.
我最初的想法是,我将首先尝试在理想的检查图像"中使用霍夫线变换获得水平直线.一旦获得了直线的数量,我将使用相同的技术来检测倾斜图像中的直线.如果行数小于某个阈值,我将检测到图像歪斜.以下是我的尝试:
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,50)
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,1000,100)
if len(lines[0]) > 2:
#image is mostly properly aligned
else:
#rotate it by some amount to align it
但是,这使我无法找到它倾斜的角度.如果我可以找到角度,则可以执行以下操作:
#say it is off by +20 degrees
deg = 20
M = cv2.getRotationMatrix2D(center, -deg, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
然后,我想到了使用标量积获得旋转角度的方法.但是,然后使用哪两个元素的标量积呢?我无法通过理想"支票中的坐标从不良"检查中获取元素,因为其内容是倾斜的.那么,openCV中是否可以通过某种方式将不良"图像叠加在理想"图像上,并以某种方式计算其偏离的角度?
在您的情况下,我将使用与模板支票图像匹配的功能在图像中查找支票.然后,您只需找到从一个到另一个的转换并据此推导角度.
看看此OpenCV教程教您如何做到这一点.
实际上,如果您想要的是正确定向的银行支票,那么单应性就是实现此目的的正确工具.无需提取角度.只需将其应用到您的图像上(或根据您的计算方式将其取反),您将得到一张漂亮的支票,可以进行处理.
I am developing an application which processes cheques for banks. But when the bank's image of a cheque can be skewed or rotated slightly by an angle of maximum value 20 degrees. Before the cheque can be processed, I need to properly align this skewed image. I am stuck here.
My initial idea was that I will first try to get the straight horizontal lines using Hough Line Transform in an "ideal cheque image". Once i get the number of straight lines, I will use the same technique to detect straight lines in a skewed image. If the number of lines is less than some threshold, I will detect the image as skewed. Following is my attempt:
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,50)
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,1000,100)
if len(lines[0]) > 2:
#image is mostly properly aligned
else:
#rotate it by some amount to align it
However, this gets me nowhere in finding the angle by which it is skewed. If i can find the angle, I can just do the following:
#say it is off by +20 degrees
deg = 20
M = cv2.getRotationMatrix2D(center, -deg, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
I then thought of getting the angle of rotation using scalar product. But then, using the scalar product of which two elements? I cannot get elements from the "bad" cheque by their coordinates in the "ideal" cheque, because its contents are skewed. So, is there any way in openCV by which, I can, say, superimpose the "bad" image over the "ideal" one and somehow calculate the angle it is off by?
What I would do in your case is to find the check within the image using feature matching with your template check image. Then you only need to find the transformation from one to the other and deduce the angle from this.
Take a look at this OpenCV tutorial that teaches you how to do that.
EDIT:
In fact, if what you want is to have the bank check with the right orientation, the homography is the right tool for that. No need to extract an angle. Just apply it to your image (or its inverse depending on how you computed it) and you should get a beautiful check, ready for processing.
这篇关于计算另一个图像的旋转角度图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!