2015-07-13 67 views
0

我是EmguCV和OpenCV的新手。我想使用EmguCV從圖像中檢測文本區域。使用EmguCV從圖像中檢測文本區域

已經有一些使用OpenCV發佈在Stack上的解決方案。

  1. Extracting text OpenCV

但不能說OpenCV的代碼轉換爲EmguCV。

+1

見[文本檢測](http://docs.opencv.org/3.0-beta/modules/text/doc/erfilter.html)。您可能需要安裝最新的EmguCV版本 – Miki

回答

2

下面是您在提供給c#的鏈接中接受的答案與EMGU的直接轉換。你可能需要做一些改動,因爲它有一個稍微不同的實現,但它應該讓你開始。我也懷疑它是一個非常強大的,所以根據您的具體用途,它可能不適合。祝你好運。

public List<Rectangle> detectLetters(Image<Bgr, Byte> img) 
{ 
    List<Rectangle> rects = new List<Rectangle>(); 
    Image<Gray, Single> img_sobel; 
    Image<Gray, Byte> img_gray, img_threshold; 
    img_gray = img.Convert<Gray, Byte>(); 
    img_sobel = img_gray.Sobel(1,0,3); 
    img_threshold = new Image<Gray, byte>(img_sobel.Size); 
    CvInvoke.cvThreshold(img_sobel.Convert<Gray, Byte>(), img_threshold, 0, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_OTSU); 
    StructuringElementEx element = new StructuringElementEx(3, 17, 1, 6, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT); 
    CvInvoke.cvMorphologyEx(img_threshold, img_threshold, IntPtr.Zero, element, Emgu.CV.CvEnum.CV_MORPH_OP.CV_MOP_CLOSE, 1); 
    for (Contour<System.Drawing.Point> contours = img_threshold.FindContours(); contours != null; contours = contours.HNext) 
    { 
     if (contours.Area > 100) 
     { 
      Contour<System.Drawing.Point> contours_poly = contours.ApproxPoly(3); 
      rects.Add(new Rectangle(contours_poly.BoundingRectangle.X, contours_poly.BoundingRectangle.Y, contours_poly.BoundingRectangle.Width, contours_poly.BoundingRectangle.Height)); 
     } 
    } 
    return rects; 
} 

用法:

Image<Bgr, Byte> img = new Image<Bgr, Byte>("VfDfJ.png"); 
List<Rectangle> rects = detectLetters(img); 
for (int i=0;i<rects.Count();i++) 
    img.Draw(rects.ElementAt<Rectangle>(i),new Bgr(0,255,0),3); 
CvInvoke.cvShowImage("Display", img.Ptr); 
CvInvoke.cvWaitKey(0); 
CvInvoke.cvDestroyWindow("Display");