title: I need recognize text in image using opencvsharp
body: <p>I trying to identify text in an image.</p>
  
  <p>Actually I'm trying to identify the text positions in the image then convert it to text.</p>
  
  <p>I found a some code written on c++ and I am trying to convert it to c#.
  Can you help me please?</p>
  
  <p><a href="https://stackoverflow.com/questions/23506105/extracting-text-opencv">Extracting text OpenCV</a></p>
  
  <pre><code>std::vector&lt;cv::Rect&gt; detectLetters(cv::Mat img)
  {       std::vector&lt;cv::Rect&gt; boundRect;[enter image description here][1]
      cv::Mat img_gray, img_sobel, img_threshold, element;
      cvtColor(img, img_gray, CV_BGR2GRAY);
      cv::Sobel(img_gray, img_sobel, CV_8U, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);
      cv::threshold(img_sobel, img_threshold, 0, 255, CV_THRESH_OTSU+CV_THRESH_BINARY);
      element = getStructuringElement(cv::MORPH_RECT, cv::Size(10, 15) );
      cv::morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element); //Does the trick
      std::vector&lt; std::vector&lt; cv::Point&gt; &gt; contours;
      cv::findContours(img_threshold, contours, 0, 1); 
      std::vector&lt;std::vector&lt;cv::Point&gt; &gt; contours_poly( contours.size() );
      for( int i = 0; i &lt; contours.size(); i++ )
          if (contours[i].size()&gt;80)
          { 
              cv::approxPolyDP( cv::Mat(contours[i]), contours_poly[i], 17, true );
              cv::Rect appRect( boundingRect( cv::Mat(contours_poly[i]) ));
              if (appRect.width&gt;appRect.height) 
                  boundRect.push_back(appRect);
          }
          return boundRect;
  }
  </code></pre>
  
  <p>and i try to convert it to c#, but it didn't work</p>
  
  <pre><code>private List&lt;Rectangle&gt; detectLetters(IntPtr img)
  {
      //cvtColor(img, img_gray, CV_BGR2GRAY);
      List&lt;Rectangle&gt; boundRect = new List&lt;Rectangle&gt;();
  
      //cv::Mat img_gray, img_sobel, img_threshold, element;
      IntPtr 
          img_gray = IntPtr.Zero, 
          img_sobel= IntPtr.Zero, 
          img_threshold= IntPtr.Zero,
          img_tmp = IntPtr.Zero,
          element= IntPtr.Zero;
  
      //cvtColor(img, img_gray, CV_BGR2GRAY);
      CvInvoke.cvCvtColor(img, img_gray,COLOR_CONVERSION.CV_BGR2GRAY); //CV_BGR2GRAY);
  
      //cv::Sobel(img_gray, img_sobel, CV_8U, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);
      CvInvoke.cvSobel(img_gray, img_sobel, 0, 1, 1);//, 3, 1, 0, cv.BORDER_DEFAULT);
  
      //cv.threshold(img_sobel, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY);
      CvInvoke.cvThreshold(img_sobel, img_threshold, 0, 255, THRESH.CV_THRESH_BINARY|THRESH.CV_THRESH_OTSU);
  
      //element = getStructuringElement(cv.MORPH_RECT, cv.Size(10, 15));
      element = CvInvoke.cvCreateStructuringElementEx(1,1,10,15,CV_ELEMENT_SHAPE.CV_SHAPE_RECT,element);// GetStructuringElement(
  
      //cv.morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element); //Does the trick
      CvInvoke.cvMorphologyEx(img_threshold,img_threshold,img_tmp,element,CV_MORPH_OP.CV_MOP_CLOSE,1);
  
      //List&lt;List&lt;cv.Point&gt;&gt; contours = new List&lt;List&lt;cv.Point&gt;&gt;();
      var contours = new List&lt;IntPtr&gt;();
  
      //cv.findContours(img_threshold, contours, 0, 1);
      CvInvoke.cvFindContours(img_threshold, element,ref ((IntPtr)contours[0]), 1, RETR_TYPE.CV_RETR_EXTERNAL, CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE,new Point(0,0));
  
  
      //std::vector&lt;std::vector&lt;cv::Point&gt; &gt; contours_poly( contours.size() );
      var contours_poly = new List&lt;List&lt;Point&gt;&gt;(contours.Count);
  
      //for( int i = 0; i &lt; contours.size(); i++ )
      for (int i = 0; i &lt; contours.Count; i++)
      {
          //if (contours[i].size()&gt;80)
          if (contours[i].ToInt32() &gt; 80)
          {
              //cv.approxPolyDP(Emgu.CV.Matrix&lt;&gt;(contours[i]), contours_poly[i], 17, true);
              CvInvoke.cvApproxPoly(contours[i], 17,contours_poly[i],APPROX_POLY_TYPE.CV_POLY_APPROX_DP, 1,1);
              //cv::Rect appRect( boundingRect( cv::Mat(contours_poly[i]) ));
              Rectangle appRect = new Rectangle(CvInvoke.cvBoundingRect(contours_poly[i],false));
              //if (appRect.width&gt;appRect.height) 
              if (appRect.width &gt; appRect.height)
              {
                  //boundRect.push_back(appRect);
                  boundRect.Add(appRect);
              }
          }
      }
      //return boundRect;
      return boundRect;
  }
  </code></pre>