朋友们,你能帮忙解答一下我的问题吗?
我在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的示意图和一些示例:
my_cropped_image示例#1,应检测到"前面"字母:
my_cropped_image示例#2,应检测到"有利"字母:
my_cropped_image示例#3,应检测到字母"Correlation":
my_cropped_image示例#4,应检测字母"密度":
my_cropped_image示例#5,应检测到字母"time":
我将非常感谢您更新我的do_ocr函数的帮助.
谢谢你,祝你有个美好的一天!
这些是您正在寻找的结果吗?
方法:
1)二进制图像,白色是前景.这是简单的完成img = img < 150;
.你可以使用更复杂的方法,比如adaptiveThreshold
.你会得到类似的东西:
2)应用一个开放的形态学操作,使单个blob中单个单词中的所有字母:
3)找到每个连接组件的矩形:
4)从中间拿下底部.
这里是完整的代码:
#include#include 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 > contours; findContours(img2.clone(), contours, CV_RETR_LIST, CHAIN_APPROX_NONE); int minArea = 60; vector 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; }