title: iOS + Tesseract Ocr + OpenCV
body: <p>I wrote a digital OCR for ios.
  I have a test image png with two digits 5 and 4.
  I find the contours. How do I transfer the contour one at tesseract? </p>
  
  <p>init tesseract: </p>
  
  <pre><code>    tess = new tesseract::TessBaseAPI();
      tess-&gt;Init([dataPath cStringUsingEncoding:NSUTF8StringEncoding], "eng");
      tess-&gt;SetPageSegMode(tesseract::PSM_SINGLE_CHAR); //&lt;-- !!!!
      tess-&gt;tesseract::TessBaseAPI::SetVariable("tessedit_char_whitelist", "0123456789");
  </code></pre>
  
  <p>Function for detect contours:</p>
  
  <pre><code>- (std::vector&lt;std::vector&lt;cv::Point&gt; &gt;)findSquaresInImage:(cv::Mat)_image {
  std::vector&lt;std::vector&lt;cv::Point&gt; &gt; squares;
  cv::Mat pyr, timg, gray0(_image.size(), CV_8U), gray;
  int thresh = 50, N = 11;
  cv::pyrDown(_image, pyr, cv::Size(_image.cols/2, _image.rows/2));
  cv::pyrUp(pyr, timg, _image.size());
  std::vector&lt;std::vector&lt;cv::Point&gt; &gt; contours;
      int ch[] = {0, 0};
      mixChannels(&amp;timg, 1, &amp;gray0, 1, ch, 1);
      for( int l = 0; l &lt; N; l++ ) {
          if( l == 0 ) {
              cv::Canny(gray0, gray, 0, thresh, 5);
              cv::dilate(gray, gray, cv::Mat(), cv::Point(-1,-1));
          }
          else {
              gray = gray0 &gt;= (l+1)*255/N;
          }
          cv::findContours(gray, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
          std::vector&lt;cv::Point&gt; approx;
  
          CvRect rec1;
          std::string str;
          std::map&lt;int,IplImage*&gt; pic_list;
  
          for( size_t i = 0; i &lt; contours.size(); i++ )
          {
  
              rec1 = cv::boundingRect(contours[i]);
  
              if (rec1.height &gt; 0.5*gray.rows &amp;&amp; rec1.width &lt; 0.756*gray.cols) {
                  NSLog(@"%d %d %d %d", rec1.width, rec1.height, rec1.x, rec1.y);
                  cv::approxPolyDP(cv::Mat(contours[i]), approx, arcLength(cv::Mat(contours[i]), true)*0.02, true);
                  squares.push_back(approx);
              }
          }
      }
  
  return squares;  }
  </code></pre>
  
  <p>function for draw contours:</p>
  
  <pre><code>cv::Mat debugSquares( std::vector&lt;std::vector&lt;cv::Point&gt; &gt; squares, cv::Mat image ) {
  for ( int i = 0; i&lt; squares.size(); i++ ) {
      // draw contour
      cv::drawContours(image, squares, i, cv::Scalar(255,0,0), 1, 8, std::vector&lt;cv::Vec4i&gt;(), 0, cv::Point());
  
      // draw bounding rect
      cv::Rect rect = boundingRect(cv::Mat(squares[i]));
      cv::rectangle(image, rect.tl(), rect.br(), cv::Scalar(0,255,0), 2, 8, 0);
  
      // draw rotated rect
      cv::RotatedRect minRect = minAreaRect(cv::Mat(squares[i]));
      cv::Point2f rect_points[4];
      minRect.points( rect_points );
      for ( int j = 0; j &lt; 4; j++ ) {
          cv::line( image, rect_points[j], rect_points[(j+1)%4], cv::Scalar(0,0,255), 1, 8 ); // blue
      }
  }
  
  return image;
  }
  </code></pre>
  
  <p>method for btn Click:</p>
  
  <pre><code>- (IBAction)onMath:(id)sender {
      UIImage *image = [UIImage imageNamed:@"test1.png"];
  
      cv::Mat iMat = [self cvMatFromUIImage:image];
      std::vector&lt;std::vector&lt;cv::Point&gt; &gt; sq = [self findSquaresInImage:iMat];
      cv::Mat hui = debugSquares(sq, iMat);
  
      image = [self UIImageFromCVMat:hui];
      self.imView.image = image;
  }
  </code></pre>
  
  <p>image after:</p>
  
  <p><img src="https://i.stack.imgur.com/mpwUE.png" alt=""></p>
  
  <p>link to project on github: <a href="https://github.com/MaxPatsy/iORC" rel="nofollow noreferrer">https://github.com/MaxPatsy/iORC</a></p>