Click here to Skip to main content
Click here to Skip to main content

Tagged as

Detect image skew angle and deskew image

, , 13 Feb 2013
Rate this:
Please Sign up or sign in to vote.
Deskew an image by converting it to grayscale + edges and rotating by the detected skew angle.



This article discusses about some very basic and generalized techniques for detecting image skew. Definitely there are much advanced algorithms to detect skew, but those will not be covered in this article.

So! What is image skew?


Using the above drawn figure as reference, I can say - the theta pointed out by that bluish arrow sign is the image skew Smile | :) .

To put it in proper words, the angle by which the image seems to be deviated from its perceived steady position is the image skew.

Some pre-requisites for detecting the skew

Before detecting the skew, the first step is to differentiate between a text image and a regular image. By text image, I mean images which are scanned documents or screenshot of a text document, or in other words, images that contains letters and texts.

And a regular image in my terms is a picture, or photo of a scenery, or something drawn by Mr. Van Gogh and Co.

In one of my articles, I have covered the topic of a basic algorithm to differentiate between regular image and text images.

However, although the skew detection of a text image is relatively easier to implement, for a regular image, it's quite difficult and next to impossible in some cases. Suppose I have a picture of only the head part of my favourite celebrity Ms. Julia Roberts, ... there is no way to know from the image whether she really has her head slanted towards one direction, or the camera man who took the beautiful picture had a problem with the camera angle.

Therefore, for regular images, we have to make some sort of assumptions. The case we are going to discuss here will assume, the pictures to be deskewed will be framed. Which means, the pictures that we are dealing with are pictures taken from another picture having frames/border around them. You can refer to the application screenshot at the very top as an example. The screenshot you see is of a camera captured image of another picture having a thick white-ish border around it.

The basic concept of detecting the skew

The main idea to detect skew for a regular image and a text image is the same. We have to first turn the pictures to Gray Scale.

Then, for regular (non text) images, we have to find the edges or feature lines of the image. There are many algorithms to detect the edges of an image, and in our case, we used the canny edge detection algorithm. We can wish to skip this part for text images.

The next step and the most important step is to cast rays from one side of the picture to the other, and by using the intersection info of the rays with the various parts of the images, we come up with a good skew angle.


As you can see from the above image, we have first converted the image (the one you see in the application screenshot) to its very basic feature lines/edges and have casted horizontal rays from one side to the other. The rays which we have casted (I mean the horizontal lines) intersect with the border/frame of the image. If I put the intersection points of the rays and the left edge of the picture in an X, Y plot (where X is horizontal and Y is vertical), I will end up with some sort of straight line curve that has a slope angle close to 2 degrees with respect to the Y axis. So, there you have it, your skew angle for the image in the application screenshot is close to 2 degrees or 0.035 radians.

This is more or less the basic concept used for detecting the skew of a regular framed image. You have to keep in mind this idea is applicable only to regular images and not text images.

By the way, the uploaded source code's skew detection algorithm does a little more than what I have explained above. What it does is - cast rays from left to right to intersect with the left edge, then top to bottom, then right to left, and then bottom to top. The algorithm considers the one with the most intersection points, and creating consistently occurring slopes as the edge to consider as the reference edge. For example, if the bottom to top rays intersects most with the bottom edge of the image and more frequently occurring slopes are observed, then that edge has the priority and the points of the bottom to top rays would be considered to be put in the X,Y plot. All these extra effort is just for the fact that, in some images, the border edges might be broken and not as beautifully framed fully around the image, like you are seeing in the application screenshot image. So, we'll have a set of rays with different slopes for such images, because the rays will go beyond the border edge wherever there is a broken edge line, and will create a different angle with another intersected point that lies somewhere inside the image bounded by the frame. So, the most occurring slopes (if two slopes have less than 0.0349 radians / 2.0 degrees difference between them - we'd consider the slopes as same valued) will be taken into consideration to detect the skew.

In order to detect the skew of text images, we don't have to worry too much about the picture having border edges or not. As mentioned before, we cast rays from left to right and find how many blackish pixels the rays intersect with. Then, we rotate the angle of the rays by a small amount and cast the rays again, and do the same process again and again.

The angle at which most white space is encountered (in other words, most black pixels are shot down, i.e., intersected), we consider that to be the skew angle. Most likely, we would start from 0 degree and go down to 90 degrees, and then again, from 0 degree go up to 90 degrees with the angle of the rays. Whenever the white ratio reaches the maximum peak and then starts to come down again, we stop immediately, and we know we have found our skew angle. This is the basic idea of detecting skew for text images.

The deskewing of an image is less of a trouble. All you need to do is rotate the image by the same amount of the skew, only to the reverse direction.

The code for the rotation is available in the ImageFunctions.h header file of the uploaded source.

However, if you want a real nice quality rotated image, you can use the wonderful aarot class by Mark Gordon ( Since the rotation code inside this class performs antialiasing, the performance is a little slow, but the output image quality is very good.

Using the code

In the uploaded source code, mainly two classes are used for detecting the skew. One class, the main class, is named SkewManager, which has code to detect the skew, and the other class is ImageData, which is just a wrapper for the bitmap bits and also keeps the width and the height of the image to be used by the SkewManager to apply its algorithms to the bitmap bits.

The SkewManager expects the bitmap that will be passed to be gray scaled. The blurring of the image to its edges (if required) is done inside the ImageData class.

Following is the example of the usage of the two classes (the m_ prefixed variables as well as the skewMan variable are member variables of the CScannedDocTestDoc class).

BOOL CScannedDocTestDoc::OnOpenDocument(LPCTSTR lpszPathName)
    HRESULT hr = m_Image.Load(lpszPathName);

        int nPitch = m_Image.GetPitch();
        int nWidth = m_Image.GetWidth();
        int nHeight = m_Image.GetHeight();

        int nBytesPerPixel = m_Image.GetBPP() / 8;
            byte* pGrayScaleBits = NULL;
            // Get a gray scaled bitmap and bitmap bits from the original image
            m_hBmpGrayScale = ImageFunctions::GetGrayScaleImage((HBITMAP)m_Image, 
            nWidth, nHeight, nBytesPerPixel, nPitch, &pGrayScaleBits);
            if(pGrayScaleBits != NULL)
                // Find out if the image is a regular image or text image
                m_bTextImage = ImageFunctions::IsTextImage(pGrayScaleBits, nWidth, nHeight);
                m_imgData.Load(pGrayScaleBits, m_Image.GetWidth(), m_Image.GetHeight(), 1);

                // get the skew angle
                double angle = 0.0;
                skewMan.GetSkewAngle(m_bTextImage, m_imgData, angle);
                m_dSkewAngle = angle;
        return TRUE;
    return FALSE;

Points of interest

In order to achieve skew detection and deskewing of images, I had to go through a lot of trouble to perform basic image operations such as creating a gray scaled image, rotating an image etc., and I have put all those functionalities inside one header, ImageFunctions.h. Most of it was improvisation on already available materials in the Internet and MSDN. The canny edge detection algorithm can be found in the the header canny_edge.h.


This is a cut down version of the deskewimage code built to compile into a DLL.  A small C# demo application is included in the solution showing how to call the code contained in within the DLL,  pass in a file name to be analysed and the skew angle in radians is returned and displayed on the screen. 


  • Kathey Marsden, Professor Richard J. Fateman
  • for the OCRchie project - the foundation upon which, the text image skew detection algorithms of this article's codes are based upon.

  • Also, thanks to Archie Russell, James Hopkin, and Cynthia Tian, who contributed significantly to the original design.
  • Heath, M., Sarkar, S., Sanocki, T., and Bowyer, K. Comparison of edge detectors: a methodology and initial study, Computer Vision and Image Understanding 69 (1), 38-54, January 1998.
  • Heath, M., Sarkar, S., Sanocki, T. and Bowyer, K.W. A Robust Visual, Method for Assessing the Relative Performance of Edge Detection Algorithms, IEEE Transactions on Pattern Analysis and Machine Intelligence 19 (12), 1338-1359, December 1997.
  • for the excellent canny edge detection algorithm.

  • Shahadatul Hakim
  • for providing with reading materials related to deskewing text and framed images.


  • Article uploaded: 24 August, 2010.
  • Article Updated 29 May 2012 CsharpClient Added
  • Article Updated 14 February 2013 1.1 Added
    Updated to compile in Visual Studio 2008 (Professional Edition)
    Combined into a single VS Solution containing two projects (for easier debugging)
    Minor Bug Fixes
    Angle of 999 is now returned if the document could not be analysed (not a 8bpp image)
    Added a Text box to show the name of the file analysed


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


About the Authors

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
You don't learn patterns, you just code it.
Follow on   Twitter

Network Administrator
Australia Australia
No Biography provided

Comments and Discussions

Questionthanks for the code but.. PinmemberChristian Lin7-Jan-13 15:43 
AnswerRe: thanks for the code but.. PinmemberMukit, Ataul7-Feb-13 5:21 
Questionexception HRESULT 0x8007000B; which image format(s)? Pinmembercinderbdt10-Sep-12 8:36 
AnswerRe: exception HRESULT 0x8007000B; which image format(s)? PinmemberMukit, Ataul10-Sep-12 18:42 
GeneralRe: exception HRESULT 0x8007000B; which image format(s)? Pinmembergupta_san417-Nov-12 19:11 
GeneralRe: exception HRESULT 0x8007000B; which image format(s)? [modified] PinmemberMukit, Ataul7-Nov-12 19:20 
QuestionI need this project as dll to reference it to app Pinmembersseerraajj27-May-12 17:03 
AnswerRe: I need this project as dll to reference it to app PinmemberMukit, Ataul27-May-12 18:47 
You can easily create an MFC dll with visual studio, and export some c style functions.... to call from the dll.
A sample dll would look something like this:
#include "stdafx.h"
#include "NGHConnPoint.h"

#ifdef _MANAGED
#pragma managed(push, off)
DWORD ul_reason_for_call,
LPVOID lpReserved
switch (ul_reason_for_call)
return TRUE;
#ifdef _MANAGED
#pragma managed(pop)
#define NGHCONNPOINT_API __declspec(dllexport)
#define NGHCONNPOINT_API __declspec(dllimport)

extern "C" NGHCONNPOINT_API void OnAppRun(int argc, wchar_t* argv[])
if(argc > 0)
MessageBox(0,L"OnAppRun", argv[0], MB_OK);
MessageBox(0, L"OnAppRun", L"CDocViewWrapper", MB_OK);
Here the OnAppRun is an exported function from the DLL which can be called from C# or or from anywhere.
Just create an MFC dll with visual studio and follow the above example to create an exported function.
GeneralRe: I need this project as dll to reference it to app Pinmembersseerraajj28-May-12 23:02 
GeneralRe: I need this project as dll to reference it to app [modified] PinmemberMukit, Ataul29-May-12 0:17 
GeneralRe: I need this project as dll to reference it to app PinmemberMukit, Ataul30-May-12 0:00 
GeneralRe: I need this project as dll to reference it to app Pinmembertlhintoq5-Jun-12 11:48 
GeneralRe: I need this project as dll to reference it to app PinmemberMukit, Ataul5-Jun-12 20:22 
GeneralRe: I need this project as dll to reference it to app Pinmembertlhintoq6-Jun-12 3:00 
GeneralRe: I need this project as dll to reference it to app Pinmembersseerraajj7-Jun-12 5:42 
GeneralRe: I need this project as dll to reference it to app PinmemberMukit, Ataul7-Jun-12 9:33 
GeneralRe: I need this project as dll to reference it to app Pinmembersseerraajj9-Jun-12 0:03 
GeneralMy vote of 5 Pinmembermanoj kumar choubey26-Feb-12 20:06 
GeneralRe: My vote of 5 PinmemberMukit, Ataul26-Feb-12 20:14 
QuestionHow to use aarot to rotate the image? PinmemberMember 19710844-Aug-11 14:31 
AnswerRe: How to use aarot to rotate the image? PinmemberMember 19710844-Aug-11 16:51 
GeneralRe: How to use aarot to rotate the image? PinmemberMukit, Ataul6-Aug-11 20:47 
GeneralMy vote of 5 PinmemberMember 191619813-Oct-10 18:30 
GeneralRe: My vote of 5 PinmemberMukit, Ataul13-Oct-10 18:57 
GeneralCool Stuff PinmemberHarrison H6-Oct-10 10:52 

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 | Mobile
Web03 | 2.8.140814.1 | Last Updated 14 Feb 2013
Article Copyright 2010 by Mukit, Ataul, David_Pollard
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid