Correcting Image Rotation with Hough Transform

Stephane Schwarz
Sinch Blog
Published in
3 min readJul 5, 2020
Adapted photo by Charles Deluvio on Unsplash

The world is facing a new era in which the digital ecosystem plays an essential role in our lives. To offer user-friendly interactions and valuable experiences to our customers, we need to make sure they have the best from beginning to end, from the easiest to more complex.

WAVY is one of the global leaders in messaging services. In this sense, a recurrent problem we deal with almost all the days is the alignment of digitalized documents. We receive and process a lot of document photos, and naturally, people do not care if the paper position is tilted differently from the scanner axis — and they should don’t, this is our work. The correct angle of the image is fundamental for future processing, for example, text recognition.

In this post, we will learn how to use the Hough Transform to adjust image rotation.

The Hough Transform-based image skew detection assumes that the text characters are aligned. Thus, the lines formed by the text regions are located using such transformation, which converts the coordinates pairs (x, y) of the image into curves in the polar coordinates (ρ, θ). The rationality behind this idea is that dominant lines (aligned black pixels) generate peaks in the Hough plane, which allows us to capture the tilt angle θ of the document.

Now that we already know the background concepts, let’s code.
The code was written Python®, and we will need to import some image processing, visualization and math libraries.

To minimize computational cost and noise to detect the most relevant lines in the Hough transform, we need to binarize the input image.

Input Image

After binarizing the image, we get the image edges using any filter, this is because the document tilt can be detected only by looking at the borders. This also saves some computational power and avoid noise.

Once we have taken the image borders, the lines and the peaks can be achieved.

Finally, afterwards we have gotten the Hough lines and find the peaks, we can rotate the image based on the angle found.

Of course, this solution has some constraints, for instance, the background can add noise in the process. Although, one way to boost the algorithm is by applying some image segmentation algorithm initially.

Now, you can try it by yourself. The complete code with plots can be found at GitHub.

If you have any question, comment below.

--

--