我试图找到图像中文本的边界框(矩形)。所以,我喜欢一次扫描整个图像并获得所有文本区域。问题是很少有文字被忽略,很少有文字被聚集在一起。分组应该是明智的。它不应该组合在一起有多个单词。每个单词都应该有自己的矩形。我正在使用下面给出的代码。
int COCR::method_gradient(int nonGradient, int showOutput)
{
Mat large = imread(INPUT_FILE);
Mat rgb;
rgb = large; //--> change
Mat small;
cvtColor(rgb, small, CV_BGR2GRAY);
// morphological gradient
Mat grad;
Mat morphKernel;
if (!nonGradient)
{
morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
morphologyEx(small, grad, MORPH_GRADIENT, morphKernel);
}
else
grad = small;
// binarize
Mat bw;
threshold(grad, bw, 0.0, 255.0, THRESH_BINARY | THRESH_OTSU);
// connect horizontally oriented regions
Mat connected;
morphKernel = getStructuringElement(MORPH_RECT, Size(9, 1));
morphologyEx(bw, connected, MORPH_CLOSE, morphKernel);
// find contours
Mat mask = Mat::zeros(bw.size(), CV_8UC1);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
if (nonGradient)
rgb = MatGradient;
// filter contours
for (int idx = 0; idx >= 0; idx = hierarchy[idx][0])
{
Rect rect = boundingRect(contours[idx]);
Mat maskROI(mask, rect);
maskROI = Scalar(0, 0, 0);
// fill the contour
drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
// ratio of non-zero pixels in the filled region
double r = (double)countNonZero(maskROI) / (rect.width*rect.height);
if (r > .25 /* assume at least 25% of the area is filled if it contains text */
&&
(rect.height > 8 && rect.width > 8)
)
{
rectangle(rgb, rect, Scalar(0, 255, 0), 2);
}
}
if (!nonGradient)
MatGradient = rgb;
if (showOutput)
imwrite(OUTPUT_FOLDER_PATH, rgb);
return 0;
}
// calling
COCR obj_ocr;
obj_ocr.method_gradient(0, 0);
obj_ocr.method_gradient(1, 1);
source img 请注意红色部分。 output img
请告知我如何纠正红日食中缺失的区域。
答案 0 :(得分:1)
将关闭从9,1更改为5,1可以修复两个图像。
// connect horizontally oriented regions
cv::Mat connected;
morphKernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 1));
cv::morphologyEx(bw, connected, cv::MORPH_CLOSE, morphKernel);