How to determine the orientation of a scanned document?

I would detect and, if necessary, correct the orientation of the scanned image of the document. I can already document the documents, however, it may happen that the document is turned upside down and needs to be rotated 180 °.

Using the tesseract layout analysis function, you can determine the orientation of the document using this code:

tesseract::TessBaseAPI api; api.Init(argv[0], "eng"); api.SetImage(img); api.SetPageSegMode(tesseract::PSM_AUTO_OSD); tesseract::PageIterator* it = api.AnalyseLayout(); tesseract::Orientation orient; tesseract::WritingDirection dir; tesseract::TextlineOrder order; float f; it->Orientation(&orient, &dir, &order, &f); if(orient == tesseract::Orientation::ORIENTATION_PAGE_UP) std::cout << "Page Up\t"; else if(orient == tesseract::Orientation::ORIENTATION_PAGE_LEFT) std::cout << "Page Left\t"; else if(orient == tesseract::Orientation::ORIENTATION_PAGE_DOWN) std::cout << "Page Down\t"; else if(orient == tesseract::Orientation::ORIENTATION_PAGE_RIGHT) std::cout << "Page Right\t"; 

However, the code does not work correctly, since it always returns ORIENTATION_PAGE_UP when the document is in portrait format and ORIENTATION_PAGE_LEFT when it is in landscape format. ( ORIENTATION_PAGE_DOWN and ORIENTATION_PAGE_RIGHT may be used, but never returned).

A.) Is there something wrong with the code above?

B.) How else can I determine the orientation of the documents?

+4
source share
1 answer

How about just starting a detection, determine the detection speed, and then do the same inverted? Better speed gives the right direction.

+5
source

Source: https://habr.com/ru/post/1381784/


All Articles