Click here to Skip to main content
15,885,278 members
Articles / Multimedia / GDI+

How to deskew an image

Rate me:
Please Sign up or sign in to vote.
4.65/5 (40 votes)
25 Apr 2006Apache2 min read 237.9K   6.6K   104   39
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:

Image 1

After deskewing:

Image 2

Using the code

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

VB
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:

Image 3

For deskewing, only the bottom line is important.

Image 4

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 Apache License, Version 2.0


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
dental implant23-Jul-23 6:24
professionaldental implant23-Jul-23 6:24 
QuestionFantastic! Pin
Magrat19-Dec-18 11:02
Magrat19-Dec-18 11:02 
QuestionRetrieve the Lines? Pin
Darren Schroeder2-Apr-18 8:13
Darren Schroeder2-Apr-18 8:13 
GeneralUpvoting Pin
anotherCPuser27-Mar-17 17:01
anotherCPuser27-Mar-17 17:01 
Generalthank u very much :) Pin
Member 1121113415-Apr-15 21:44
Member 1121113415-Apr-15 21:44 
QuestionTranslated to c# but images with less black lines go skew Pin
Jdarmanovich10-Feb-15 19:52
Jdarmanovich10-Feb-15 19:52 
AnswerRe: Translated to c# but images with less black lines go skew Pin
Member 1290940810-Sep-18 20:02
Member 1290940810-Sep-18 20:02 
GeneralI am so appreciate it! Pin
marshell_w3-Feb-14 8:48
marshell_w3-Feb-14 8:48 
QuestionCode in windows service error Pin
Member 983823212-Mar-13 7:44
Member 983823212-Mar-13 7:44 
QuestionIt's really helpful! Pin
shimizu_masato29-Mar-12 22:41
shimizu_masato29-Mar-12 22:41 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey1-Mar-12 22:18
professionalManoj Kumar Choubey1-Mar-12 22:18 
GeneralMy vote of 5 Pin
Johannes_Franke1-Feb-11 1:35
Johannes_Franke1-Feb-11 1:35 
GeneralRe: My vote of 5 [modified] Pin
nguyenq112-Mar-11 14:57
nguyenq112-Mar-11 14:57 
GeneralPort to C++ Pin
Syd Logan10-Dec-09 23:18
Syd Logan10-Dec-09 23:18 
QuestionUpload souce code Deskew, Visual C++ or C#, Please ? Pin
lung_tung_chuong_IT25-Nov-09 10:28
lung_tung_chuong_IT25-Nov-09 10:28 
GeneralJava port of this deskew code now available! Pin
Roland Quast24-Oct-09 3:33
Roland Quast24-Oct-09 3:33 
GeneralThis is excellent and it works in grayscale images at 300dpi Pin
daelin24-Jul-09 5:42
daelin24-Jul-09 5:42 
QuestionUpload a complete Paint App please? Pin
Edy11-May-09 17:07
Edy11-May-09 17:07 
QuestionJust so I understand Pin
TheGuy20-Mar-09 7:06
TheGuy20-Mar-09 7:06 
AnswerRe: Just so I understand Pin
IronHarry2-Oct-17 20:07
IronHarry2-Oct-17 20:07 
GeneralA note on full page scanning Pin
smartyP11-Feb-09 4:59
smartyP11-Feb-09 4:59 
QuestionHow to run Pin
nanni_n25-Feb-08 22:11
nanni_n25-Feb-08 22:11 
GeneralPerformances Pin
lucapan11-Oct-07 5:58
lucapan11-Oct-07 5:58 
GeneralRe: Performances Pin
nanni_n25-Feb-08 22:08
nanni_n25-Feb-08 22:08 
GeneralRe: Performances Pin
defwebserver20-Apr-08 12:59
defwebserver20-Apr-08 12:59 
The Fastbitmap class does speed things up but you have to replace a few more lines to make it work. Here is the code with the replacements:

Imports System.Drawing
Imports System.Drawing.Imaging
Imports FastBitmap

Public Class gmseDeskew
' Representation of a line in the image.
Public Class HougLine
' Count of points in the line.
Public Count As Integer
' Index in Matrix.
Public Index As Integer
' The line is represented as all x,y that solve y*cos(alpha)-x*sin(alpha)=d
Public Alpha As Double
Public d As Double
End Class

' The Bitmap
Dim cBmp As FastBitmap
' The range of angles to search for lines
Dim cAlphaStart As Double = -20
Dim cAlphaStep As Double = 0.2
Dim cSteps As Integer = 40 * 5
' Precalculation of sin and cos.
Dim cSinA() As Double
Dim cCosA() As Double
' Range of d
Dim cDMin As Double
Dim cDStep As Double = 1
Dim cDCount As Integer
' Count of points that fit in a line.
Dim cHMatrix() As Integer

' Calculate the skew angle of the image cBmp.
Public Function GetSkewAngle() As Double
Dim hl() As gmseDeskew.HougLine
Dim i As Integer
Dim sum As Double
Dim count As Integer

' Hough Transformation
Calc()
' Top 20 of the detected lines in the image.
hl = GetTop(20)
' Average angle of the lines
For i = 0 To 19
sum += hl(i).Alpha
count += 1
Next
Return sum / count
End Function

' Calculate the Count lines in the image with most points.
Private Function GetTop(ByVal Count As Integer) As HougLine()
Dim hl() As HougLine
Dim i As Integer
Dim j As Integer
Dim tmp As HougLine
Dim AlphaIndex As Integer
Dim dIndex As Integer

ReDim hl(Count)
For i = 0 To Count - 1
hl(i) = New HougLine
Next
For i = 0 To cHMatrix.Length - 1
If cHMatrix(i) > hl(Count - 1).Count Then
hl(Count - 1).Count = cHMatrix(i)
hl(Count - 1).Index = i
j = Count - 1
While j > 0 AndAlso hl(j).Count > hl(j - 1).Count
tmp = hl(j)
hl(j) = hl(j - 1)
hl(j - 1) = tmp
j -= 1
End While
End If
Next
For i = 0 To Count - 1
dIndex = hl(i).Index \ cSteps
AlphaIndex = hl(i).Index - dIndex * cSteps
hl(i).Alpha = GetAlpha(AlphaIndex)
hl(i).d = dIndex + cDMin
Next
Return hl
End Function
Public Sub New(ByVal bmp As Bitmap)
cBmp = New FastBitmap(bmp)
End Sub
' Hough Transforamtion:
Private Sub Calc()
Dim x As Integer
Dim y As Integer
Dim hMin As Integer = cBmp.Bitmap.Height / 4
Dim hMax As Integer = cBmp.Bitmap.Height * 3 / 4

Init()
For y = hMin To hMax
For x = 1 To cBmp.Bitmap.Width - 2
' Only lower edges are considered.
If IsBlack(x, y) Then
If Not IsBlack(x, y + 1) Then
Calc(x, y)
End If
End If
Next
Next
End Sub
' Calculate all lines through the point (x,y).
Private Sub Calc(ByVal x As Integer, ByVal y As Integer)
Dim alpha As Integer
Dim d As Double
Dim dIndex As Integer
Dim Index As Integer

For alpha = 0 To cSteps - 1
d = y * cCosA(alpha) - x * cSinA(alpha)
dIndex = CalcDIndex(d)
Index = dIndex * cSteps + alpha
Try
cHMatrix(Index) += 1
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
Next
End Sub
Private Function CalcDIndex(ByVal d As Double) As Double
Return Convert.ToInt32(d - cDMin)
End Function
Private Function IsBlack(ByVal x As Integer, ByVal y As Integer) As Boolean
Dim c As Color
Dim luminance As Double

c = cBmp.GetPixel(x, y)
luminance = (c.R * 0.299) + (c.G * 0.587) + (c.B * 0.114)
Return luminance < 140
End Function
Private Sub Init()
Dim i As Integer
Dim angle As Double

' Precalculation of sin and cos.
ReDim cSinA(cSteps - 1)
ReDim cCosA(cSteps - 1)
For i = 0 To cSteps - 1
angle = GetAlpha(i) * Math.PI / 180.0#
cSinA(i) = Math.Sin(angle)
cCosA(i) = Math.Cos(angle)
Next
' Range of d:
cDMin = -cBmp.Bitmap.Width
cDCount = 2 * (cBmp.Bitmap.Width + cBmp.Bitmap.Height) / cDStep
ReDim cHMatrix(cDCount * cSteps)
End Sub

Public Function GetAlpha(ByVal Index As Integer) As Double
Return cAlphaStart + Index * cAlphaStep
End Function
End Class

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

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