opencv,找到一张位于图片特定位置的信件?

时间:2015-12-09 15:36:56

标签: c++ opencv ocr

朋友们,你能帮忙解答一下我的问题吗?

我在c ++中使用opencv 我从相机视图中随机裁剪一张小图片。我想找到位于此裁剪图片底部的单词,此单词也应该被此裁剪图片的垂直中心线(虚构)穿透。请参阅以下代码:

char* my_word = do_ocr(my_cropped_image);

并且do_ocr函数是这样的:

char* do_ocr(cv::Mat im)
{
cv::Mat gray;
cv::cvtColor(im, gray, CV_BGR2GRAY);
// ...other image pre-processing here...

// Pass it to Tesseract API
tesseract::TessBaseAPI tess;
tess.Init(NULL, "eng", tesseract::OEM_DEFAULT);
tess.SetPageSegMode(tesseract::PSM_SINGLE_BLOCK);
tess.SetImage((uchar*)gray.data, gray.cols, gray.rows, 1, gray.cols);

// Get the text
char* out = tess.GetUTF8Text();
std::cout << out << std::endl;
return out;
}

以下是my_cropped_image的示意图和一些示例:

enter image description here

my_cropped_image样本#1,字母&#34;前面&#34;应该被发现:

enter image description here

my_cropped_image示例#2,字母&#34;有利&#34;应该被发现:

enter image description here

my_cropped_image示例#3,字母&#34;相关&#34;应该被发现:

enter image description here

my_cropped_image示例#4,字母&#34;密度&#34;应该被发现:

enter image description here

my_cropped_image示例#5,字母&#34;时间&#34;应该被发现:

enter image description here

我很感激你帮助我更新我的do_ocr功能。

谢谢你,祝你有个美好的一天!

1 个答案:

答案 0 :(得分:5)

这些是你想要的结果吗?

enter image description here enter image description here enter image description here enter image description here enter image description here

方法:

1)二进制图像,白色为前景。这里只用img = img < 150;完成。您可以使用更复杂的方法,例如adaptiveThreshold。 你会得到类似的东西:

enter image description here

2)应用打开形态操作,以便单个单词中的所有字母为单个blob:

enter image description here

3)找到每个连通组件的矩形:

enter image description here

4)从中间拿下底部。

这里是完整的代码:

#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;

Mat3b dbg;

int main()
{
    Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
    cvtColor(img, dbg, COLOR_GRAY2BGR);

    Mat3b result;
    cvtColor(img, result, COLOR_GRAY2BGR);

    Mat1b img2;
    img2 = img < 150; 

    Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(5,3));
    morphologyEx(img2, img2, MORPH_DILATE, kernel);

    // Apply a small border
    copyMakeBorder(img2, img2, 5, 5, 5, 5, BORDER_CONSTANT, Scalar(0));

    // Take the bounding boxes of all connected components
    vector<vector<Point>> contours;
    findContours(img2.clone(), contours, CV_RETR_LIST, CHAIN_APPROX_NONE);

    int minArea = 60;
    vector<Rect> rects;
    for (int i = 0; i < contours.size(); ++i)
    {
        Rect r = boundingRect(contours[i]);
        if (r.area() >= minArea)
        {
            // Account for border
            r -= Point(5,5);
            rects.push_back(r);
        }
    }

    int middle = img.cols / 2;

    // Keep bottom rect, containig middle point
    if (rects.empty()) return -1;

    Rect word;
    for (int i = 1; i < rects.size(); ++i)
    {
        Point pt(middle, rects[i].y + rects[i].height/2);
        if (rects[i].contains(pt))
        {
            if (rects[i].y > word.y)
            {
                word = rects[i];
            }
        }
    }

    // Show results
    Mat3b res;
    cvtColor(img, res, COLOR_GRAY2BGR);
    for (int i = 0; i < rects.size(); ++i)
    {
        rectangle(res, rects[i], Scalar(0, 255, 0));
    }

    rectangle(result, word, Scalar(0, 0, 255), 2);

    imshow("Rects", res);
    imshow("Result", result);
    waitKey();

    return 0;
}