Click here to Skip to main content
11,799,282 members (68,276 online)
Click here to Skip to main content

How to deskew an image

, 25 Apr 2006 CPOL 129K 3.8K 90
Rate this:
Please Sign up or sign in to vote.
The article describes an algorithm to calculate the skew angle of an image.


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


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:


We get:


Which is equivalent to:


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


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.


The article was taken from GMSE Imaging.


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


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


About the Author

Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

Generalthank u very much :) Pin
Member 1121113415-Apr-15 21:44
memberMember 1121113415-Apr-15 21:44 
QuestionTranslated to c# but images with less black lines go skew Pin
Jdarmanovich10-Feb-15 19:52
memberJdarmanovich10-Feb-15 19:52 
GeneralI am so appreciate it! Pin
marshell_w3-Feb-14 8:48
membermarshell_w3-Feb-14 8:48 
QuestionCode in windows service error Pin
Member 983823212-Mar-13 7:44
memberMember 983823212-Mar-13 7:44 
QuestionIt's really helpful! Pin
shimizu_masato29-Mar-12 22:41
membershimizu_masato29-Mar-12 22:41 
GeneralMy vote of 5 Pin
manoj kumar choubey1-Mar-12 22:18
membermanoj kumar choubey1-Mar-12 22:18 
GeneralMy vote of 5 Pin
Johannes_Franke1-Feb-11 1:35
memberJohannes_Franke1-Feb-11 1:35 
GeneralRe: My vote of 5 [modified] Pin
nguyenq112-Mar-11 14:57
membernguyenq112-Mar-11 14:57 
GeneralPort to C++ Pin
Syd Logan10-Dec-09 23:18
memberSyd Logan10-Dec-09 23:18 
QuestionUpload souce code Deskew, Visual C++ or C#, Please ? Pin
lung_tung_chuong_IT25-Nov-09 10:28
memberlung_tung_chuong_IT25-Nov-09 10:28 
GeneralJava port of this deskew code now available! Pin
Roland Quast24-Oct-09 3:33
memberRoland Quast24-Oct-09 3:33 
GeneralThis is excellent and it works in grayscale images at 300dpi Pin
daelin24-Jul-09 5:42
memberdaelin24-Jul-09 5:42 
QuestionUpload a complete Paint App please? Pin
Edy11-May-09 17:07
memberEdy11-May-09 17:07 
QuestionJust so I understand Pin
TheGuy20-Mar-09 7:06
memberTheGuy20-Mar-09 7:06 
GeneralA note on full page scanning Pin
smartyP11-Feb-09 4:59
membersmartyP11-Feb-09 4:59 
QuestionHow to run Pin
nanni_n25-Feb-08 22:11
membernanni_n25-Feb-08 22:11 
GeneralPerformances Pin
lucapan11-Oct-07 5:58
memberlucapan11-Oct-07 5:58 
GeneralRe: Performances Pin
nanni_n25-Feb-08 22:08
membernanni_n25-Feb-08 22:08 
GeneralRe: Performances Pin
defwebserver20-Apr-08 12:59
memberdefwebserver20-Apr-08 12:59 
GeneralRe: Performances Pin
lucapan18-Oct-09 21:47
memberlucapan18-Oct-09 21:47 
Questiongr8 work but ... Pin
looka730-Mar-07 22:21
memberlooka730-Mar-07 22:21 
GeneralNice work Pin
karulont28-Mar-07 3:56
memberkarulont28-Mar-07 3:56 
GeneralRe: Nice work Pin
Vimmi2616-Aug-08 13:26
memberVimmi2616-Aug-08 13:26 
Questioncircular hough transfrom Pin
shdelpiero23-Feb-07 12:36
membershdelpiero23-Feb-07 12:36 
GeneralRenuka Pin
Member #369869024-Jan-07 17:45
memberMember #369869024-Jan-07 17:45 
QuestionDeskew Application Pin
CholekarSagar9-Nov-06 0:27
memberCholekarSagar9-Nov-06 0:27 
GeneralRegarding Deskew Application Pin
CholekarSagar9-Nov-06 0:26
memberCholekarSagar9-Nov-06 0:26 
QuestionApproach weakness ? Pin
NinjaCross25-Apr-06 5:58
memberNinjaCross25-Apr-06 5:58 
AnswerRe: Approach weakness ? Pin
mackenb27-Apr-06 3:48
membermackenb27-Apr-06 3:48 
GeneralGreat article Pin
zcaccau4-Apr-06 1:10
memberzcaccau4-Apr-06 1:10 
GeneralRe: Easier method Pin
zcaccau4-Apr-06 1:08
memberzcaccau4-Apr-06 1:08 
AnswerRe: Easier method Pin
Graham Toal21-Mar-11 9:22
memberGraham Toal21-Mar-11 9:22 
GeneralRe: more explanation ? Pin
mackenb31-Mar-06 0:49
membermackenb31-Mar-06 0:49 

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.

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