Auto rotate scanned text image (OCR via command line?)

2

Can I auto-rotate an image that contains mainly text? Maybe via OCR? The algorithm or whatever needs to scan the image and decide if it has to rotate it 90°, 180° or 270°

I want to include this feature into an existing PowerShell script, so command-line ability is mandatory.

I'm open for ImageMagick solutions

Example

enter image description here
Click to enlarge

I have already read the following SO questions. But they rotate photos.
and I'm only trying to rotate pure text scans which have no EXIF metadata.

nixda

Posted 2014-03-19T19:37:26.273

Reputation: 23 233

Answers

1

I currently do this with tesseract using the switches "-psm 0" which will detect orientation of the image.

Sample output :

Tesseract Open Source OCR Engine v3.04.00 with Leptonica
Orientation: 3
Orientation in degrees: 90
Orientation confidence: 3.94
Script: 1
Script confidence: 13.81

Then I run ImageMagick to rotate the image to the correct orientation. It does work for most of the images, except for handwritten documents and photos.

A bit kludgy, but it is a lot faster than running OCR four times.

teikjoon

Posted 2014-03-19T19:37:26.273

Reputation: 178

0

You want the ImageMagick convert utility. Just run

convert -rotate 90 image.png

and it will be rotated 90 degrees and saved back as the same file name.

MattDMo

Posted 2014-03-19T19:37:26.273

Reputation: 4 968

Well, I thought it is obvious from the question (» OCR). Apparently not. Sometimes the scans must be rotated 270°, 90° or 180°. That is not fixed – nixda – 2014-03-19T19:58:17.137

@nixda There is also an -auto-orient option that may be of use - I haven't used it, so I don't know how accurate it is. Sorry, I didn't pick up from your original question that they weren't all in the same orientation. – MattDMo – 2014-03-19T20:02:57.770

The documentation says it's using EXIF data. So this is no solution. If the EXIF profile was previously stripped, the -auto-orient operator will do nothing.

– nixda – 2014-03-20T09:27:18.940

0

Not the most elegant way, but you could try to OCR in all four orientations and whichever one has the least amount of gibberish is the correct one. I did some quick searching, and I found a few references which discuss using such a technique, for example:

Phase two of document correction is the contextual auto-rotate. Using a full-page OCR read at several orientations the software can determine at which orientation the quality of the read is best. This is the most accurate way to rotate a document. Documents with little text, or text at various angles are the only risky documents. In these cases, the software chooses the orientation of the MOST readable text.

Eric G

Posted 2014-03-19T19:37:26.273

Reputation: 1 010