opencv3.0.0 识别表格

2024-03-06 23:48
文章标签 表格 识别 opencv3.0

本文主要是介绍opencv3.0.0 识别表格,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载地址:http://answers.opencv.org/question/63847/how-to-extract-tables-from-an-image/

As the others proposed finding the horizontal and vertical lines seems to be a nice way to go. Below you can find such a solution. In case you have any question feel free to ask, though I have added comments through my code so it should not be hard to follow.

#include <iostream>
#include <opencv2/opencv.hpp>using namespace std;
using namespace cv;int main()
{// Load source imagestring filename = "table.jpg";Mat src = imread(filename);// Check if image is loaded fineif(!src.data)cerr << "Problem loading image!!!" << endl;//    // Show source image
//    imshow("src", src);// resizing for practical reasonsMat rsz;Size size(800, 900);resize(src, rsz, size);imshow("rsz", rsz);// Transform source image to gray if it is notMat gray;if (rsz.channels() == 3){cvtColor(rsz, gray, CV_BGR2GRAY);}else{gray = rsz;}// Show gray imageimshow("gray", gray);// Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbolMat bw;adaptiveThreshold(~gray, bw, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);// Show binary imageimshow("binary", bw);

image description

    // Create the images that will use to extract the horizonta and vertical linesMat horizontal = bw.clone();Mat vertical = bw.clone();int scale = 15; // play with this variable in order to increase/decrease the amount of lines to be detected// Specify size on horizontal axisint horizontalsize = horizontal.cols / scale;// Create structure element for extracting horizontal lines through morphology operationsMat horizontalStructure = getStructuringElement(MORPH_RECT, Size(horizontalsize,1));// Apply morphology operationserode(horizontal, horizontal, horizontalStructure, Point(-1, -1));dilate(horizontal, horizontal, horizontalStructure, Point(-1, -1));
//    dilate(horizontal, horizontal, horizontalStructure, Point(-1, -1)); // expand horizontal lines// Show extracted horizontal linesimshow("horizontal", horizontal);

image description

    // Specify size on vertical axisint verticalsize = vertical.rows / scale;// Create structure element for extracting vertical lines through morphology operationsMat verticalStructure = getStructuringElement(MORPH_RECT, Size( 1,verticalsize));// Apply morphology operationserode(vertical, vertical, verticalStructure, Point(-1, -1));dilate(vertical, vertical, verticalStructure, Point(-1, -1));
//    dilate(vertical, vertical, verticalStructure, Point(-1, -1)); // expand vertical lines// Show extracted vertical linesimshow("vertical", vertical);

image description

    // create a mask which includes the tablesMat mask = horizontal + vertical;imshow("mask", mask);

image description

    // find the joints between the lines of the tables, we will use this information in order to descriminate tables from pictures (tables will contain more than 4 joints while a picture only 4 (i.e. at the corners))Mat joints;bitwise_and(horizontal, vertical, joints);imshow("joints", joints);

image description

    // Find external contours from the mask, which most probably will belong to tables or to imagesvector<Vec4i> hierarchy;std::vector<std::vector<cv::Point> > contours;cv::findContours(mask, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));vector<vector<Point> > contours_poly( contours.size() );vector<Rect> boundRect( contours.size() );vector<Mat> rois;for (size_t i = 0; i < contours.size(); i++){// find the area of each contourdouble area = contourArea(contours[i]);//        // filter individual lines of blobs that might exist and they do not represent a tableif(area < 100) // value is randomly chosen, you will need to find that by yourself with trial and error procedurecontinue;approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );boundRect[i] = boundingRect( Mat(contours_poly[i]) );// find the number of joints that each table hasMat roi = joints(boundRect[i]);vector<vector<Point> > joints_contours;findContours(roi, joints_contours, RETR_CCOMP, CHAIN_APPROX_SIMPLE);// if the number is not more than 5 then most likely it not a tableif(joints_contours.size() <= 4)continue;rois.push_back(rsz(boundRect[i]).clone());//        drawContours( rsz, contours, i, Scalar(0, 0, 255), CV_FILLED, 8, vector<Vec4i>(), 0, Point() );rectangle( rsz, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 255, 0), 1, 8, 0 );}for(size_t i = 0; i < rois.size(); ++i){/* Now you can do whatever post process you want* with the data within the rectangles/tables. */imshow("roi", rois[i]);waitKey();}

image description image description

    imshow("contours", rsz);

image description

    waitKey();return 0;
}

Of course you will need to try it by yourself and apply any modifications that might be needed depending on your dataset. Enjoy ;-).

这篇关于opencv3.0.0 识别表格的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/781758

相关文章

阿里开源语音识别SenseVoiceWindows环境部署

SenseVoice介绍 SenseVoice 专注于高精度多语言语音识别、情感辨识和音频事件检测多语言识别: 采用超过 40 万小时数据训练,支持超过 50 种语言,识别效果上优于 Whisper 模型。富文本识别:具备优秀的情感识别,能够在测试数据上达到和超过目前最佳情感识别模型的效果。支持声音事件检测能力,支持音乐、掌声、笑声、哭声、咳嗽、喷嚏等多种常见人机交互事件进行检测。高效推

Prompt - 将图片的表格转换成Markdown

Prompt - 将图片的表格转换成Markdown 0. 引言1. 提示词2. 原始版本 0. 引言 最近尝试将图片中的表格转换成Markdown格式,需要不断条件和优化提示词。记录一下调整好的提示词,以后在继续优化迭代。 1. 提示词 英文版本: You are an AI assistant tasked with extracting the content of

vue2实践:el-table实现由用户自己控制行数的动态表格

需求 项目中需要提供一个动态表单,如图: 当我点击添加时,便添加一行;点击右边的删除时,便删除这一行。 至少要有一行数据,但是没有上限。 思路 这种每一行的数据固定,但是不定行数的,很容易想到使用el-table来实现,它可以循环读取:data所绑定的数组,来生成行数据,不同的是: 1、table里面的每一个cell,需要放置一个input来支持用户编辑。 2、最后一列放置两个b

关于使用cspreadsheet读写EXCEL表格数据的问题

前几天项目有读写EXCEL表格的需求,我就找了大概有几种,大致分为:COM方法、ODBC方法、OLE方法、纯底层格式分析方法。由于COM方法要求必须安装有OFFICE的EXCEL组件,纯底层格式分析方法又很多功能需要自行去完善,所有最终选择了数据库的方法,用数据库的方法去存取xls格式的数据。网上有一个高手写的CSpreedSheet,看了一下提供的接口,感觉挺好用的。在使用的过程中发现几个

Clion不识别C代码或者无法跳转C语言项目怎么办?

如果是中文会显示: 此时只需要右击项目,或者你的源代码目录,将这个项目或者源码目录标记为项目源和头文件即可。 英文如下:

自动化表格处理的革命:智能文档系统技术解析

在当今数据驱动的商业环境中,表格数据的自动化处理成为了企业提高效率、降低成本的关键。企业智能文档系统在智能表格识别方面展现出卓越的性能,通过精准识别和处理各种通用表格,显著提升了企业文档管理的智能化水平。本文将深入探讨该系统在表格识别方面的关键技术和应用优势,以及如何通过行业定制化服务满足不同行业的需求。 1. 通用表格识别 智能文档系统通过先进的OCR技术和表格结构识别算法,能够精准

BERN2(生物医学领域)命名实体识别与命名规范化工具

BERN2: an advanced neural biomedical named entity recognition and normalization tool 《Bioinformatics》2022 1 摘要 NER和NEN:在生物医学自然语言处理中,NER和NEN是关键任务,它们使得从生物医学文献中自动提取实体(如疾病和药物)成为可能。 BERN2:BERN2是一个工具,

python读取pdf内容写入到Excel表格中

要从每个 PDF 文件中提取全文内容,并将这些内容粘贴到一个新的或现有的表格中,你可以使用 Python 的库来完成这一任务。以下是一个简化的步骤和示例代码,展示如何实现这个过程。 步骤概述 读取文件夹中的所有 PDF 文件。提取每个 PDF 文件的全文内容。创建一个新的 Excel 表格或使用现有的表格。将提取的内容粘贴到表格中,每个 PDF 的内容放在一个垂直单元格中。保存表格文件。 所

行为智能识别摄像机

行为智能识别摄像机 是一种结合了人工智能技术和监控摄像技术的先进设备,它能够通过深度学习算法对监控画面进行实时分析,自动识别和分析监控画面中的各种行为动作。这种摄像机在安防领域有着广泛的应用,可以帮助监控人员及时发现异常行为,并采取相应的措施。 行为智能识别摄像机可以有效预防盗窃事件。在商场、超市等公共场所安装这种摄像机,可以通过识别异常行为等情况,及时报警并阻止不安全行为的发生

flutter开发实战-flutter build web微信无法识别二维码及小程序码问题

flutter开发实战-flutter build web微信无法识别二维码及小程序码问题 GitHub Pages是一个直接从GitHub存储库托管的静态站点服务,‌它允许用户通过简单的配置,‌将个人的代码项目转化为一个可以在线访问的网站。‌这里使用flutter build web来构建web发布到GitHub Pages。 最近通过flutter build web,通过发布到GitHu