title: converting cv::Mat for tesseract
body: <p>I'm using OpenCV to extract a subimage of a scanned document and would like to use <a href="http://code.google.com/p/tesseract-ocr/">tesseract</a> to perform OCR over this subimage. </p>
  
  <p>I found out that I can use two methods for text recognition in tesseract, but so far I wasn't able to find a working solution. </p>
  
  <p><b>A.) How can I convert a <code>cv::Mat</code> into a <code>PIX*</code>?</b>
   (PIX* is a datatype of <a href="http://code.google.com/p/leptonica/">leptonica</a>)</p>
  
  <p>Based on vasiles code below, this is essentially my current code:</p>
  
  <pre><code> cv::Mat image = cv::imread("c:/image.png"); 
   cv::Mat subImage = image(cv::Rect(50, 200, 300, 100)); 
  
   int depth;
   if(subImage.depth() == CV_8U)
      depth = 8;
   //other cases not considered yet
  
   PIX* pix = pixCreateHeader(subImage.size().width, subImage.size().height, depth);
   pix-&gt;data = (l_uint32*) subImage.data; 
  
   tesseract::TessBaseAPI tess;
   STRING text; 
   if(tess.ProcessPage(pix, 0, 0, &amp;text))
   {
      std::cout &lt;&lt; text.string(); 
   }   
  </code></pre>
  
  <p>While it doesn't crash or anything, the OCR result still is wrong. It should recognize one word of my sample image, but instead it returns some non-readable characters. </p>
  
  <p>The method <code>PIX_HEADER</code> doesn't exist, so I used <code>pixCreateHeader</code>, but it doesn't take the number of channels as an argument. So how can I set the number of channels?</p>
  
  <p><b>B.) How can I use <code>cv::Mat</code> for <code>TesseractRect()</code> ?</b></p>
  
  <p>Tesseract offers another method for text recognition with this signature: </p>
  
  <pre><code>char * TessBaseAPI::TesseractRect   (   
      const UINT8 *   imagedata,
      int     bytes_per_pixel,
      int     bytes_per_line,
      int     left,
      int     top,
      int     width,
      int     height   
  )   
  </code></pre>
  
  <p>Currently I am using the following code, but it also returns non-readable characters (although different ones than from the code above. </p>
  
  <pre><code>char* cr = tess.TesseractRect(
             subImage.data, 
             subImage.channels(), 
             subImage.channels() * subImage.size().width, 
             0, 
             0, 
             subImage.size().width, 
             subImage.size().height);   
  </code></pre>