栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在扫描的文档中分割文本行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在扫描的文档中分割文本行

在输入图像中,您需要将文本设置为白色,将背景设置为黑色

然后,您需要计算账单的旋转角度。一种简单的方法是找到

minAreaRect
所有白点(
findNonZero
),然后得到:

然后,您可以旋转帐单,使文本为水平:

现在您可以计算水平投影(

reduce
)。您可以在每行中取平均值。
th
在直方图上应用阈值以解决图像中的一些噪点(在此我使用了
0
,即没有噪点)。仅背景的行将具有值
>0
,文本行在
0
直方图中将具有值。然后,获取直方图中白色bin的每个连续序列的平均bin坐标。那将是
y
您行的坐标:

这里的代码。它使用C ++,但是由于大多数工作都是使用OpenCV函数,因此应该可以轻松转换为Python。至少,您可以将其用作参考:

#include <opencv2/opencv.hpp>using namespace cv;using namespace std;int main(){    // Read image    Mat3b img = imread("path_to_image");    // Binarize image. Text is white, background is black    Mat1b bin;    cvtColor(img, bin, COLOR_BGR2GRAY);    bin = bin < 200;    // Find all white pixels    vector<Point> pts;    findNonZero(bin, pts);    // Get rotated rect of white pixels    RotatedRect box = minAreaRect(pts);    if (box.size.width > box.size.height)    {        swap(box.size.width, box.size.height);        box.angle += 90.f;    }    Point2f vertices[4];    box.points(vertices);    for (int i = 0; i < 4; ++i)    {        line(img, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0));    }    // Rotate the image according to the found angle    Mat1b rotated;    Mat M = getRotationMatrix2D(box.center, box.angle, 1.0);    warpAffine(bin, rotated, M, bin.size());    // Compute horizontal projections    Mat1f horProj;    reduce(rotated, horProj, 1, CV_REDUCE_AVG);    // Remove noise in histogram. White bins identify space lines, black bins identify text lines    float th = 0;    Mat1b hist = horProj <= th;    // Get mean coordinate of white white pixels groups    vector<int> ycoords;    int y = 0;    int count = 0;    bool isSpace = false;    for (int i = 0; i < rotated.rows; ++i)    {        if (!isSpace)        { if (hist(i)) {     isSpace = true;     count = 1;     y = i; }        }        else        { if (!hist(i)) {     isSpace = false;     ycoords.push_back(y / count); } else {     y += i;     count++; }        }    }    // Draw line as final result    Mat3b result;    cvtColor(rotated, result, COLOR_GRAY2BGR);    for (int i = 0; i < ycoords.size(); ++i)    {        line(result, Point(0, ycoords[i]), Point(result.cols, ycoords[i]), Scalar(0, 255, 0));    }    return 0;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/456003.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号