Click here to Skip to main content
Licence CPOL
First Posted 30 Mar 2006
Views 87,911
Downloads 1,176
Bookmarked 78 times

How to deskew an image

By | 25 Apr 2006 | Article
The article describes an algorithm to calculate the skew angle of an image.

Introduction

The following article describes an algorithm in VB.NET to deskew an image.

Background

Deskewing an image can help a lot, if you want to do 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 high, the barcode can not be detected. After deskewing, the barcode can be read.

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

The basic idea of the algorithm is:

  • Find reference lines in the image.
  • Calculate the angle of the lines.
  • Calculate the skew angle as an average of the angles.
  • Rotate the image.

The lines are detected with the Hough algorithm. Each point in the image can lie on an infinite number of lines. To find the reference lines, we let each point vote for all the lines that pass through the point. The lines with the highest number of points are our reference lines.

First, we need the parameterization of a line. A line can be parameterized as:

y = m*x+t

with slope m and offset t. We are interested in the angle and not the slope. The angle alpha of the line satisfies:

m=tan(alpha)=sin(alpha)/cos(alpha)

We get:

y=sin(alpha)/cos(alpha)*x+t

Which is equivalent to:

y*cos(alpha)-x*sin(alpha)=d

We can not search an infinite parameter space, so we have to define a discrete one. We search for all lines with:

-20<=alpha<=20

in 0.2 steps, and we round d to an integer.

The basic algorithm in pseudo code:

1. Create a two-dimensional matrix Hough and initialize the values with 0 
2. for y=0 to Height-1 
3.    for x=0 to Width-1 
4.      if Point(x,y) is black then 
5.        for alpha=-20 to 20 step 0.2 
6.          d= Trunc(y*cos(alpha)-x*sin(alpha)) 
7.          Hough(Trunc(alpha*5),d)+=1 
8.        next alpha 
9.      end if 
10.   next x 
11. next y 
12. Find the top 20 (alpha,d) pairs that have the highest count in the Hough matrix 
13. Calculate the skew angle as an average of the alphas
14. Rotate the image by – skew angle

The algorithm is computationally expensive. To save some time, the number of voting points is reduced. For each text line, you can draw many lines with different angles through the letters:

For deskewing, only the bottom line is important.

The points on the bottom line have a lower neighbour that is white. So, we only let points (x,y) vote that satisfy:

  • The point (x,y) is black.
  • The lower neighbour (x,y+1) is white.

References

The article was taken from GMSE Imaging.

History

  • 03-30-06: Original article.
  • 03-31-06: More explanations about the Hough algorithm.
  • 04-25-06: Added the References section.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

mackenb



Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionIt's really helpful! Pinmembershimizu_masato22:41 29 Mar '12  
GeneralMy vote of 5 Pinmembermanoj kumar choubey22:18 1 Mar '12  
GeneralMy vote of 5 PinmemberJohannes_Franke1:35 1 Feb '11  
GeneralRe: My vote of 5 [modified] Pinmembernguyenq114:57 12 Mar '11  
GeneralPort to C++ PinmemberSyd Logan23:18 10 Dec '09  
QuestionUpload souce code Deskew, Visual C++ or C#, Please ? Pinmemberlung_tung_chuong_IT10:28 25 Nov '09  
GeneralJava port of this deskew code now available! PinmemberRoland Quast3:33 24 Oct '09  
GeneralThis is excellent and it works in grayscale images at 300dpi Pinmemberdaelin5:42 24 Jul '09  
QuestionUpload a complete Paint App please? PinmemberEdy17:07 11 May '09  
QuestionJust so I understand PinmemberTheGuy7:06 20 Mar '09  
Excellent article! This is pretty amazing stuff.
 
I would really like to understand how the algorithm works a little better. To do so I am trying to figure out exactly what all of the variables in the source code are. So far I have determined the following (please correct me if any of these is incorrect):
 
- cAlphaStart - The starting angle of the hough lines we are interested in (the minimum considered angle)
- cAlphaStep - The angle accuracy of the algorithm
- cSteps - The number of steps of angle accuracy between the minimum considered angle and the maximum considered angle
- cSinA - sin pre-calculations for each angle so we don't have to do the calculation multiple times
- cCosA - cos pre-calculations for each angle so we don't have to do the calculation multiple times
- cHMatrix - An array containing a count of all of the hough lines in the image used to determine frequency
 
However, I am completely lost as to what the following variables represent:
 
- cDMin
- cDStep
- cDCount
 
They are labeled as "range of d", but I have no idea what "d" is, nor what the above variables are for. Any way you could explain what d is and what these variables are used for?
 
Thanks.
GeneralA note on full page scanning PinmembersmartyP4:59 11 Feb '09  
QuestionHow to run Pinmembernanni_n22:11 25 Feb '08  
GeneralPerformances Pinmemberlucapan5:58 11 Oct '07  
GeneralRe: Performances Pinmembernanni_n22:08 25 Feb '08  
GeneralRe: Performances Pinmemberdefwebserver12:59 20 Apr '08  
GeneralRe: Performances Pinmemberlucapan21:47 18 Oct '09  
Questiongr8 work but ... Pinmemberlooka722:21 30 Mar '07  
GeneralNice work Pinmemberkarulont3:56 28 Mar '07  
GeneralRe: Nice work PinmemberVimmi26113:26 6 Aug '08  
Questioncircular hough transfrom Pinmembershdelpiero12:36 23 Feb '07  
GeneralRenuka PinmemberMember #369869017:45 24 Jan '07  
QuestionDeskew Application PinmemberCholekarSagar0:27 9 Nov '06  
GeneralRegarding Deskew Application PinmemberCholekarSagar0:26 9 Nov '06  
QuestionApproach weakness ? PinmemberNinjaCross5:58 25 Apr '06  
AnswerRe: Approach weakness ? Pinmembermackenb3:48 27 Apr '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 25 Apr 2006
Article Copyright 2006 by mackenb
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid