Skip to content
Advertisement

How to de-skew a text image and retrieve the new bounding box of that image Python OpenCV?

Here’s a receipt image that I’ve got and I’ve plotted it using matplotlib and If you see the image the text in it is not straight. How can I de-skew and fix it?

JavaScript

receipt image

I think if we want to de-skew first we have to find the edges, so I tried to find the edges using canny algorithm and then get contours like below.

JavaScript

The edges that I’ve got from above code is the edges of each text but that is not what I needed. I need to get edges of receipt right?

Also I need a way to get the new bounding box coordinates after de-skewing the image (i.e straightening the image)?

If anyone has worked on similar problem please help me out? Thanks.

Advertisement

Answer

Here’s a modified implementation of the Projection Profile Method to correct skewed images as described in Projection profile based skew estimation algorithm for JBIG compressed images. After obtaining a binary image, the idea is to rotate the image at various angles and generate a histogram of pixels in each iteration. To determine the skew angle, we compare the maximum difference between peaks and using this skew angle, rotate the image to correct the skew. The amount of peaks to determine can be adjusted by the delta value, the lower the delta, the more peaks will be checked with the tradeoff that the process will take longer.


Before -> After

Skew angle: -2

Code

JavaScript

Note: You may have to adjust the delta or limit values depending on the image. The delta value controls iteration step, it will iterate up until the limit which controls the maximum angle. This method is straightforward by iteratively checking each angle + delta and currently only works to correct skew in the range of +/- 5 degrees. If you need to correct at a larger angle, adjust the limit value. For another approach to handle skew, take a look at rotate skewed image to upright position for an alternative method.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement