naim shaikh
naim shaikh

Reputation: 1111

Detecting Text Regions from image using EmguCV

I am new to EmguCV and OpenCV. I want to detect the text Regions from an Image using EmguCV.

There are already some solutions posted on Stack using OpenCV.

  1. Extracting text OpenCV

But unable to convert that OpenCV code to EmguCV.

Upvotes: 1

Views: 14102

Answers (1)

Mike
Mike

Reputation: 56

Here is a direct conversion of the accepted answer in the link you provided into c# with EMGU. 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");

Upvotes: 2

Related Questions