What Is This?

A C++ port of VB code at http://imaging.gmse.net/download/gmseDeskew_vb.html for deskewing an image. Seems to work well for images that are skewed some small angle between -20 and +20 degrees.

The buf argument to Deskew::Init() is a pointer to an array of characters holding pixel data in the range 0, 255 (grayscale). The width and height arguments are self explanatory. You need to provide your own image rotation code, the angle returned by GetSkewAngle() is in degrees.

See also http://imaging.gmse.net/articledeskew.html.
Wikipedia article is at http://en.wikipedia.org/wiki/Hough_transform

deskew.h

/* 
Copyright (c) 2009, Syd Logan All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The names of its contributors may not be used to
endorse or promote products derived from this software without specific prior
written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

if !defined(__DESKEW_H__)
#define __DESKEW_H__

// http://imaging.gmse.net/download/gmseDeskew_vb.html

class HoughLine {
public:
   HoughLine();
   void SetCount(int count) {m_count = count;};
   int GetCount() {return m_count;};
   void SetIndex(int index) {m_index = index;};
   int GetIndex() {return m_index;};
   void SetAlpha(double alpha) {m_alpha = alpha;};
   double GetAlpha() {return m_alpha;};
   void SetD(double d) {m_d = d;};
   double GetD() {return m_d;};
private:
   int m_count;
   int m_index;
   double m_alpha;
   double m_d;
};

class Deskew {
public:
   Deskew();
   ~Deskew();
   void Init(char *buf, const int width, const int height);
   double GetSkewAngle();
   HoughLine **GetTop(const int count);
   void Calc();
   void Calc(const int x, const int y);
   bool IsBlack(const int x, const int y);
   double GetAlpha(const int index);
   int CalcDIndex(const double d);
private:
   int m_width;
   int m_height;
   char *m_buf;
   int m_cDMin;
   double m_cAlphaStart;
   double m_cAlphaStep;
   int m_cSteps;
   double m_cDStep;
   int *m_cHMatrix;
   int m_cHMatrixSize;
};

#endif

deskew.cpp

Note: I don't precompute the sin/cos tables. That is left as an exercise if you want to do it.
/* 
Copyright (c) 2009, Syd Logan All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The names of its contributors may not be used to
endorse or promote products derived from this software without specific prior
written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "deskew.h"
#include <math.h>

HoughLine::HoughLine() :
   m_count(0),
   m_index(0),
   m_alpha(0.0),
   m_d(0.0)
{
}

Deskew::Deskew() : 
   m_cAlphaStart(-20.0),
   m_cAlphaStep(0.2),
   m_cSteps(40 * 5),
   m_cDStep(1.0),
   m_cHMatrix(NULL)
{
}

Deskew::~Deskew()
{
   if (m_cHMatrix) {
      free(m_cHMatrix);
   }
}

void 
Deskew::Init(char *buf, const int width, const int height)
{
   m_buf = buf;
   m_width = width;
   m_height = height;
   m_cDMin = -width;
   int cDCount = 2 * (width + height) / m_cDStep;
   m_cHMatrixSize = cDCount * m_cSteps;
   m_cHMatrix = (int *) malloc(sizeof(int) * m_cHMatrixSize);
}

double 
Deskew::GetSkewAngle()
{
   int count = 20;
   double sum = 0.0;
   HoughLine **top;

   // Calculate Hough transform

   Calc();

   // Get the best 20 results

   top = GetTop(count);

   if (!top) {
      return 0.0;
   }

   // Return the average of the best

   for (int i = 0; i < count; i++) {
      sum += top[i]->GetAlpha();
      printf("Best %d is %f\n", i, top[i]->GetAlpha());
      delete top[i];
   }
  
   printf("Sum is %f\n", sum); 
   free(top);
   double ret = (sum / count);
   printf("ret is %f\n", ret);
   return ret;
}

HoughLine **
Deskew::GetTop(const int count)
{
   HoughLine **hl;
   HoughLine *tmp;
   int dIndex;
   int alphaIndex;

   hl = (HoughLine **) malloc(count * sizeof(HoughLine *));

   for (int i = 0; i < count; i++) {
       hl[i] = new HoughLine;
   }

   for (int i = 0; i < m_cHMatrixSize; i++) {
      if (m_cHMatrix[i] > hl[count - 1]->GetCount()) {
         hl[count - 1]->SetCount(m_cHMatrix[i]);
         hl[count - 1]->SetIndex(i);
         int j = count - 1;
         while (j > 0 && hl[j]->GetCount() > hl[j - 1]->GetCount()) {
            tmp = hl[j];
            hl[j] = hl[j - 1];
            hl[j - 1] = tmp;
            j -= 1;
         }
      }
   }

   for (int i = 0; i < count; i++) {
      dIndex = hl[i]->GetIndex() / m_cSteps;
      alphaIndex = hl[i]->GetIndex() - dIndex * m_cSteps;
      hl[i]->SetAlpha(GetAlpha(alphaIndex));
      hl[i]->SetD(dIndex + m_cDMin);
   }
   return hl;
}

void 
Deskew::Calc()
{
   int hMin = m_height / 4;
   int hMax = m_height * 3 / 4;

   for (int y = hMin; y <= hMax; y++) {
      for (int x = 1; x < m_width - 1; x++) {
         if (IsBlack(x, y)) {
            if (!IsBlack(x, y + 1)) {
               Calc(x, y);
            }
         }
      }
   }
}

void 
Deskew::Calc(const int x, const int y)
{
   double d;
   int dIndex;
   int index;

   for (int alpha = 0; alpha < m_cSteps; alpha++) {
      double rads = GetAlpha(alpha) * 3.14159265354 / 180.0;
      d = y * cos(rads) - x * sin(rads);
      dIndex = CalcDIndex(d);
      index = dIndex * m_cSteps + alpha;
      m_cHMatrix[index]++; 
   }
}

bool 
Deskew::IsBlack(const int x, const int y)
{
   //luminance = (c.R * 0.299) + (c.G * 0.587) + (c.B * 0.114)
   return ((unsigned char) *(m_buf + y * m_width + x) < 140 ? true : false);
}

double 
Deskew::GetAlpha(const int index)
{
   return m_cAlphaStart + index * m_cAlphaStep;
}

int 
Deskew::CalcDIndex(const double d)
{
   return (int) (d - m_cDMin);
}