1

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?

Tejash Desai
  • 466
  • 4
  • 11

1 Answers1

1

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.

Sunreef
  • 4,452
  • 21
  • 33
  • "Then you only need to find the transformation from one to the other and deduce the angle from this" - can you please elaborate? – Tejash Desai Jun 17 '16 at 10:04
  • @Tejash Desai You probably don't even need the angle if you use this method. Find the homography between your ideal check and your image and apply its inverse to your image to get the rectified check. – Sunreef Jun 17 '16 at 11:46