Click here to Skip to main content
15,896,118 members
Articles / Desktop Programming / MFC

Detect image skew angle and deskew image

,
Rate me:
Please Sign up or sign in to vote.
4.95/5 (41 votes)
13 Feb 2013CPOL8 min read 233.1K   17K   137  
Deskew an image by converting it to grayscale + edges and rotating by the detected skew angle.
// SkewManager.h: interface for the SkewManager class.
//
//////////////////////////////////////////////////////////////////////

#ifndef __SKEWMANAGER_H__
#define __SKEWMANAGER_H__

#include "ImageData.h"

#define M_PI 3.14159265358979323846


class SkewManager
{
public:
	SkewManager();
	virtual ~SkewManager();
	
	bool GetSkewAngle(bool isTextImage, ImageData& image, double& outAngle, int canny_slices = 0);

private:
	
	enum EdgeType
	{
		ET_LEFT = 0,
		ET_TOP,
		ET_RIGHT,
		ET_BOTTOM
	};
	struct SlopeInfo
	{
		double x;
		double y;
		double slope;
		double rounded_slope;
		int frequency;

		SlopeInfo()
		{
			x = INFINITE;
			y = INFINITE;
			slope = INFINITE;
			rounded_slope = INFINITE;
			frequency = INFINITE;
		}
	};
	
	bool IsText(ImageData& image);
	double GetTextSkewAngle(ImageData& image, int sample_skip = 10);
	double GetImageSkewAngle(ImageData& image, int slices, EdgeType edge_type, int edge_depth, double* indicator1, double* indicator2);
	
	double Histogram(ImageData& image, double angle);	
	//DJP 07Feb13 Changed int row to unsigned int row to avoid compiler warning
	unsigned int CastRay(ImageData& raster, unsigned int row, const double &dx, int dy);
	unsigned int bit_count(unsigned char* buf, unsigned int start, unsigned int end);
	
	double sqr(double x)
	{
		return x * x;
	}
	
	double m_skew;
	int m_sample_skip;
	// DJP 07Feb13 Changed int m_max_rows to unsigned int m_max_rows to avoid compiler warning
	unsigned int m_max_rows;
	unsigned int* m_rows;
};

#endif // __SKEWMANAGER_H__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Technical Lead Kotha Technologies
Bangladesh Bangladesh
If you are not in - you are out !
- Chapter 1

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

Comments and Discussions