简体   繁体   English

使用EmguCV从图像中检测文本区域

[英]Detecting Text Regions from image using EmguCV

I am new to EmguCV and OpenCV. 我是EmguCV和OpenCV的新手。 I want to detect the text Regions from an Image using EmguCV. 我想使用EmguCV从图像中检测文本区域。

There are already some solutions posted on Stack using OpenCV. 使用OpenCV在Stack上已经发布了一些解决方案。

  1. Extracting text OpenCV 提取文本OpenCV

But unable to convert that OpenCV code to EmguCV. 但是无法将该OpenCV代码转换为EmguCV。

Here is a direct conversion of the accepted answer in the link you provided into c# with EMGU. 这是您提供的链接中接受的答案直接转换为EMGU的c#。 You might have to make some alterations since its a slightly different implementation but it should get you started. 由于其实现略有不同,因此您可能必须进行一些更改,但这应该可以帮助您入门。 I also doubt it is a very robust so depending on your specific use it might not be suitable. 我也怀疑它是否非常健壮,因此根据您的特定用途,可能不合适。 Best of luck. 祝你好运。

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;
}

Usage: 用法:

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM