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

相关文章

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

使用PyTorch实现手写数字识别功能

《使用PyTorch实现手写数字识别功能》在人工智能的世界里,计算机视觉是最具魅力的领域之一,通过PyTorch这一强大的深度学习框架,我们将在经典的MNIST数据集上,见证一个神经网络从零开始学会识... 目录当计算机学会“看”数字搭建开发环境MNIST数据集解析1. 认识手写数字数据库2. 数据预处理的

Java利用poi实现word表格转excel

《Java利用poi实现word表格转excel》这篇文章主要为大家详细介绍了Java如何利用poi实现word表格转excel,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、每行对象类需要针对不同的表格进行对应的创建。package org.example.wordToEx

Pytorch微调BERT实现命名实体识别

《Pytorch微调BERT实现命名实体识别》命名实体识别(NER)是自然语言处理(NLP)中的一项关键任务,它涉及识别和分类文本中的关键实体,BERT是一种强大的语言表示模型,在各种NLP任务中显著... 目录环境准备加载预训练BERT模型准备数据集标记与对齐微调 BERT最后总结环境准备在继续之前,确

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis

讯飞webapi语音识别接口调用示例代码(python)

《讯飞webapi语音识别接口调用示例代码(python)》:本文主要介绍如何使用Python3调用讯飞WebAPI语音识别接口,重点解决了在处理语音识别结果时判断是否为最后一帧的问题,通过运行代... 目录前言一、环境二、引入库三、代码实例四、运行结果五、总结前言基于python3 讯飞webAPI语音