0

I am working a program that extract the area of the given text from one frame using OpenCV. After got it, it should be blur processing in that area. The text which is displayed in a frame is given, and the text is always horizontal status and the color is white. But I don't know in where the text is displayed in a frame. Occasionally the position of text is changed.

  • How can I extract the area(x,y,width,height) of text with OpenCV?
  • Is there anything tools to do this?

I have attached two samples frames. You can see the 8-length hexadecimal code under the game mark.

sample 1: a case of complex color background

sample 2: a case of single color background

Please advice, thanks.

1 Answers1

2

There are several resources in the web. One way is to detect text by finding close edge elements (link). Summarized you first detect edges in your image with cv::Canny or cv::Sobel or any other edge detection method (try out which works the best in your case). Then you binarize your image with a threshold.

enter image description here

To remove artifacts you can apply a rank filter . Then you merge your letters with morphological operations cv::morphologyEx. You could try a dilation or a closing. The closing will close the space between the letters and merge them together without changing the size too much. You have to play with the kernel size and shape. Know you detect contrours with cv::findContours, do a polygon approximation and compute the bounding rect of your contour.

enter image description here

To detect just the right contour you should test for the right size or so (e.g. if (contours[i].size()>100)). Then you can order your found fields according to this article where it is explained in detail.

This is the code from the first post:

#include "opencv2/opencv.hpp"

std::vector<cv::Rect> detectLetters(cv::Mat img)
{
    std::vector<cv::Rect> boundRect;
    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(17, 3) );
    cv::morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element); //Does the trick
    std::vector< std::vector< cv::Point> > contours;
    cv::findContours(img_threshold, contours, 0, 1); 
    std::vector<std::vector<cv::Point> > contours_poly( contours.size() );
    for( int i = 0; i < contours.size(); i++ )
        if (contours[i].size()>100)
        { 
            cv::approxPolyDP( cv::Mat(contours[i]), contours_poly[i], 3, true );
            cv::Rect appRect( boundingRect( cv::Mat(contours_poly[i]) ));
            if (appRect.width>appRect.height) 
                boundRect.push_back(appRect);
        }
    return boundRect;
}
Community
  • 1
  • 1
PSchn
  • 718
  • 4
  • 14
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/13481838) – SurvivalMachine Aug 27 '16 at 13:15
  • 1
    Oh okay i'm new here. I will update my answer. Thanks – PSchn Aug 27 '16 at 13:16
  • @PSchn thanks for your help. Your answer would be a little help for me. But it is not complete answer. Please check the sample frames I attached. (I have attached them after your answer). The background color is fixed in your offered case. In sample frames, however, the background is dynamically changed and color is not single, and is complex. It seems like that the parameters need to change according to background color. Thanks again for your answer, Please help me if you have a good solution. – Wiktor Kostus Aug 28 '16 at 15:12
  • You could try the [scene text detection](https://github.com/opencv/opencv_contrib/blob/master/modules/text/README.md) mentioned in OpenCV3.0. its designed to find text in natural images like traffic/navigation sign with an additional OCR module. [Here](https://github.com/opencv/opencv_contrib/blob/master/modules/text/samples/textdetection.cpp) you can find the source code. – PSchn Aug 28 '16 at 18:25
  • @PSchn I have tested with the samples you offered. They are good solutions, but they couldn't get the right result. I think I need the text training to use them to extract the my texts. Could you tell me the tutorials for it? Thanks very much for your help. – Wiktor Kostus Aug 30 '16 at 06:41