gpt4 book ai didi

c++ - opencv,找到位于图片特定位置的字母?

转载 作者:搜寻专家 更新时间:2023-10-31 00:31:25 24 4
gpt4 key购买 nike

friend ,你能帮我解答一下吗?

我在 C++ 中使用 opencv。
我从相机 View 中随机裁剪了一张小图片。我想找到位于这张裁剪图片底部的单词,并且这个单词也应该被这张裁剪图片的垂直中心线(虚部)穿透。请看下面的代码:

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,应检测字母“preceding”:

enter image description here

my_cropped_image 示例 # 2,应检测字母“advantageous”:

enter image description here

my_cropped_image 示例 # 3,应检测字母“Correlation”:

enter image description here

my_cropped_image 示例 # 4,应检测字母“密度”:

enter image description here

my_cropped_image 示例 # 5,应检测字母“时间”:

enter image description here

感谢您帮助我更新 do_ocr 函数。

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

最佳答案

这些是您要查找的结果吗?

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

关于c++ - opencv,找到位于图片特定位置的字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34182810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com