c++image-processingtesseractdocument-layout-analysis

how to detect orientation of a scanned document?


I'd to detect and, if necessary, correct the orientation of a scanned document image. I am already able to deskew documents, however it still might occur, that a document is upside down and it needs to be rotated by 180°.

Using tesseract's layout analysis feature it should be possible to determine a document's orientation 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 doesn't seems to work correctly as it always returns ORIENTATION_PAGE_UP when a document is in portrait format and ORIENTATION_PAGE_LEFT when it is in landscape format. (ORIENTATION_PAGE_DOWN and ORIENTATION_PAGE_RIGHT can be used, but are never returned).

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

B.) How else can I determine a documents orientation?


Solution

  • What about just running your detection evaluate the detection rate and then doing the same thing flipped ? The better rate gives the right direction.