Algorithms - How to deskew an image

Deskewing an image can help a lot, if you want to perform OCR, OMR, barcode detection or just improve the readability of scanned images. For example think of a camera that automatically takes photos of goods with a barcode. If the skew angle is too big, the barcode can not be detected. After deskewing the barcode can be read. The full source code can be downloded here.

Before deskewing After deskewing

Using the code

The following code determines the skew angle of the image bmpIn:

Dim sk As New gmseDeskew(bmpIn)
Dim skewangle As Double = sk.GetSkewAngle
Dim bmpOut As Bitmap = RotateImage(bmpIn, -skewangle)

Points of interest

First all lines in the image are calculated with the Hough Transformation (see Wikipedia definition). The lines are represented as all (x, y) that satisfy y*cos(alpha)-x*sin(alpha)=d. For each pair (alpha,d) in the search space the count of points that are on the line are calculated. We are only interested in the lower bounding line of the text. Otherwise we will get a lot of lines that have nothing to do with the skew angle:

Detected lines
All Points
Points on Lower Bounds

For that reason only points are considered, that have a white point below. The skew angle is determined as the average angle of the top 20 lines. To speed up the calculation sin(alpha) and cos(alpha) are precalculated for the search range and saved in an array.

Algorithms Overview