gui/mainwindow.cpp
// This func taken from gImageReader source and I modified a little
This func taken from gImageReader source and I modified a little
This func taken from gImageReader source and I modified a little
void MainWindow::autoDetectLayout() {
  removeItems(*m_scene, "rect");
  double avgDeskew = 0.0;
  int nDeskew = 0;
  QList<QRectF> rects;
  rects_size = 0;
  QImage img = getImage(*m_scene, m_pixmapItem->sceneBoundingRect());
  tesseract::TessBaseAPI tess;
  tess.InitForAnalysePage();
  tess.SetPageSegMode(tesseract::PSM_AUTO_ONLY);
  tess.SetImage(img.bits(), img.width(), img.height(), 4, img.bytesPerLine());
  tesseract::PageIterator *it = tess.AnalyseLayout();
  if (it && !it->Empty(tesseract::RIL_BLOCK)) {
    do {
      int x1, y1, x2, y2;
      tesseract::Orientation orient;
      tesseract::WritingDirection wdir;
      tesseract::TextlineOrder tlo;
      float deskew;
      it->BoundingBox(tesseract::RIL_BLOCK, &x1, &y1, &x2, &y2);
      it->Orientation(&orient, &wdir, &tlo, &deskew);
      avgDeskew += static_cast<double>(deskew);
      ++nDeskew;
      float width = 2 + x2 - x1, height = 2 + y2 - y1;
      if (width > 10 && height > 10) {
        rects.append(QRectF(x1 - 1, y1 - 1, static_cast<double>(width),
                            static_cast<double>(height)));
      }
    } while (it->Next(tesseract::RIL_BLOCK));
  }
  delete it;
  avgDeskew = qRound(((avgDeskew / nDeskew) / CV_PI * 180.0) * 10.0) / 10.0;
  if (std::abs(avgDeskew) > 0.1) {
    double newangle = m_rwidget->getDSpinBox()->value() - avgDeskew;
    m_rwidget->getDSpinBox()->setValue(newangle);
  } else {
    for (int i = rects.size(); i-- > 1;) {
      for (int j = i; j-- > 0;) {
        if (rects[j].intersects(rects[i])) {
          rects[j] = rects[j].united(rects[i]);
          rects.removeAt(i);
          break;
        }
      }
    }
    for (int i = 0, n = rects.size(); i < n; ++i) {
      m_rect = new MyRectF(m_scene, rects[i].topLeft(), rects[i].bottomRight(),
                           ++rects_size, m_pixmapItem->sceneBoundingRect());
      m_scene->addItem(m_rect);
      m_scene->items().first()->setData(0, "rect");
      m_scene->items().first()->setData(1, m_rect->getRect());
      m_scene->items().first()->setData(2, rects_size);
    }
  }
  tess.Clear();
  tess.End();
}
void MainWindow :: autoDetectLayout ( ) { removeItems ( * m_scene , " " ) ; double avgDeskew = 0.0 ; int nDeskew = 0 ; QList < QRectF > rects ; rects_size = 0 ; QImage img = getImage ( * m_scene , m_pixmapItem -> sceneBoundingRect ( ) ) ; tesseract :: TessBaseAPI tess ; tess . InitForAnalysePage ( ) ; tess . SetPageSegMode ( tesseract :: PSM_AUTO_ONLY ) ; tess . SetImage ( img . bits ( ) , img . width ( ) , img . height ( ) , 4 , img . bytesPerLine ( ) ) ; tesseract :: PageIterator * it = tess . AnalyseLayout ( ) ; if ( it && ! it -> Empty ( tesseract :: RIL_BLOCK ) ) { do { int x1 , y1 , x2 , y2 ; tesseract :: Orientation orient ; tesseract :: WritingDirection wdir ; tesseract :: TextlineOrder tlo ; float deskew ; it -> BoundingBox ( tesseract :: RIL_BLOCK , & x1 , & y1 , & x2 , & y2 ) ; it -> Orientation ( & orient , & wdir , & tlo , & deskew ) ; avgDeskew += static_cast < double > ( deskew ) ; ++ nDeskew ; float width = 2 + x2 - x1 , height = 2 + y2 - y1 ; if ( width > 10 && height > 10 ) { rects . append ( QRectF ( x1 - 1 , y1 - 1 , static_cast < double > ( width ) , static_cast < double > ( height ) ) ) ; } } while ( it -> Next ( tesseract :: RIL_BLOCK ) ) ; } delete it ; avgDeskew = qRound ( ( ( avgDeskew / nDeskew ) / CV_PI * 180.0 ) * 10.0 ) / 10.0 ; if ( std :: abs ( avgDeskew ) > 0.1 ) { double newangle = m_rwidget -> getDSpinBox ( ) -> value ( ) - avgDeskew ; m_rwidget -> getDSpinBox ( ) -> setValue ( newangle ) ; } else { for ( int i = rects . size ( ) ; i -- > 1 ; ) { for ( int j = i ; j -- > 0 ; ) { if ( rects [ j ] . intersects ( rects [ i ] ) ) { rects [ j ] = rects [ j ] . united ( rects [ i ] ) ; rects . removeAt ( i ) ; break ; } } } for ( int i = 0 , n = rects . size ( ) ; i < n ; ++ i ) { m_rect = new MyRectF ( m_scene , rects [ i ] . topLeft ( ) , rects [ i ] . bottomRight ( ) , ++ rects_size , m_pixmapItem -> sceneBoundingRect ( ) ) ; m_scene -> addItem ( m_rect ) ; m_scene -> items ( ) . first ( ) -> setData ( 0 , " " ) ; m_scene -> items ( ) . first ( ) -> setData ( 1 , m_rect -> getRect ( ) ) ; m_scene -> items ( ) . first ( ) -> setData ( 2 , rects_size ) ; } } tess . Clear ( ) ; tess . End ( ) ; }
This func taken from gImageReader source and I modified a little
This func taken from gImageReader source and I modified a little