如何检测桌子的水平线和垂直线并消除噪音?

2023-12-21

I am trying to get the horizontal and vertical lines of the table in an image in order to extract the texts in cells. Here's a picture I use:enter image description here

我使用下面的代码来提取垂直线和水平线:

img = cv2.imread(img_for_box_extraction_path, 0)  # Read the image
(thresh, img_bin) = cv2.threshold(img, 200, 255,
                                  cv2.THRESH_BINARY | cv2.THRESH_OTSU)  # Thresholding the image
img_bin = 255-img_bin  # Invert the image
cv2.imwrite("Image_bin_2.jpg",img_bin)

# Defining a kernel length
kernel_length = np.array(img).shape[1]//140

# A verticle kernel of (1 X kernel_length), which will detect all the verticle lines from the image.
verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length))

# A horizontal kernel of (kernel_length X 1), which will help to detect all the horizontal line from the image.
hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1))

# A kernel of (3 X 3) ones.
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

# Morphological operation to detect verticle lines from an image
img_temp1 = cv2.erode(img_bin, verticle_kernel, iterations=3)
verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=3)
cv2.imwrite("verticle_lines_2.jpg",verticle_lines_img)

# Morphological operation to detect horizontal lines from an image
img_temp2 = cv2.erode(img_bin, hori_kernel, iterations=3)
horizontal_lines_img = cv2.dilate(img_temp2, hori_kernel, iterations=3)
cv2.imwrite("horizontal_lines_2.jpg",horizontal_lines_img)

The pictures below are the horizontal lines and vertical lines: horizontal lines vertical lines

我使用下面的代码将两个图像添加在一起

# Weighting parameters, this will decide the quantity of an image to be added to make a new image.
alpha = 0.5
beta = 1.0 - alpha

# This function helps to add two image with specific weight parameter to get a third image as summation of two image.
img_final_bin = cv2.addWeighted(verticle_lines_img, alpha, horizontal_lines_img, beta, 0.0)
img_final_bin = cv2.erode(~img_final_bin, kernel, iterations=2)
(thresh, img_final_bin) = cv2.threshold(img_final_bin, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

# For Debugging
# Enable this line to see verticle and horizontal lines in the image which is used to find boxes
cv2.imwrite("img_final_bin_2.jpg",img_final_bin)

However, I get a picture like this: Summation Result How do I remove the noise and get a better result? Thanks in advance.


这是一个简单的方法:

二值图像

检测到水平

检测到垂直

组合面罩

需要删除的绿色线

Result

import cv2
import numpy as np

# Load image, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Detect horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
horizontal_mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=1)

# Detect vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
vertical_mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=1)

# Combine masks and remove lines
table_mask = cv2.bitwise_or(horizontal_mask, vertical_mask)
image[np.where(table_mask==255)] = [255,255,255]

cv2.imshow('thresh', thresh)
cv2.imshow('horizontal_mask', horizontal_mask)
cv2.imshow('vertical_mask', vertical_mask)
cv2.imshow('table_mask', table_mask)
cv2.imshow('image', image)
cv2.waitKey()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何检测桌子的水平线和垂直线并消除噪音? 的相关文章

  • 光学标记阅读器的开源库[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我想要一个用于光学标记读取 OMR 的开源库 有这样的库吗 我想在我的 java 应用程序中使用这个库 zxing 可能对你有用 http
  • 子进程改变目录

    我想在子目录 超级目录中执行脚本 我需要首先进入该子目录 超级目录 我无法得到subprocess进入我的子目录 tducin localhost Projekty tests ve python Python 2 7 4 default
  • conda 无法从 yml 创建环境

    我尝试运行下面的代码来从 YAML 文件创建虚拟 Python 环境 我在 Ubuntu 服务器上的命令行中运行代码 虚拟环境名为 py36 当我运行下面的代码时 我收到下面的消息 环境也没有被创建 这个问题是因为我有几个必须使用 pip
  • 如何从谷歌云存储桶读取音频文件并在datalab笔记本中使用ipd播放

    我想在数据实验室笔记本中播放我从谷歌云存储桶中读取的声音文件 这个怎么做 import numpy as np import IPython display as ipd import librosa import soundfile as
  • 我可以将 OpenCV 的发布配置与我的应用程序的调试配置一起使用吗?

    我正在编写一个通用 Windows 应用程序 它使用 OpenCV 进行相机校准和标签检测等 我希望能够在我自己的 DLL 处于调试模式时使用发布模式 完全优化的 OpenCV DLL 这可能吗 如果是这样 我如何配置 CMake 来实现它
  • 为 Networkx 图添加标题?

    我希望我的代码创建一个带有标题的图 使用下面的代码 可以创建绘图 但没有标题 有人可以告诉我我做错了什么吗 import pandas as pd import networkx as nx from networkx algorithms
  • 用Python中的嵌套for循环替换重复的if语句?

    在我编写的下面的代码中 n 4 所以有五个 if 语句 所以如果我想将 n 增加到 比如说 10 那么就会有很多 if 语句 因此我的问题是 如何用更优雅的东西替换所有 if 语句 n p 4 5 number of trials prob
  • 如何对这个 Flask 应用程序进行单元测试?

    我有一个 Flask 应用程序 它使用 Flask Restless 来提供 API 我刚刚写了一些身份验证来检查 如果消费者主机被识别 该请求包含一个哈希值 通过加密 POST 的请求内容和 GET 的 URL 以及秘密 API 密钥来计
  • Django - 电子邮件发送两次

    每当我使用如下所示的电子邮件设置从views py调用下面的方法时 电子邮件的两份副本都会发送给收件人 并且我收到如下所示的错误 def sendEmailBasic request msg EmailMessage Request Cal
  • 如何使用 PySpark 有效地将这么多 csv 文件(大约 130,000 个)合并到一个大型数据集中?

    我之前发布了这个问题并得到了一些使用 PySpark 的建议 如何有效地将这一大数据集合并到一个大数据框中 https stackoverflow com questions 60259271 how can i merge this la
  • Matplotlib 图例不工作

    自从升级 matplotlib 以来 每当尝试创建图例时 我都会收到以下错误 usr lib pymodules python2 7 matplotlib legend py 610 UserWarning Legend does not
  • 如何在 Python 中从 HTML 页面中提取 URL [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我必须用Python 编写一个网络爬
  • 从 IMDbPy 结果中的片目中获取电影 ID

    我正在尝试创建一个数据集 允许我根据 Python IMDb API 中的演员 ID 和电影 ID 加入演员和电影 现在 我正在尝试从演员的电影作品中提取电影 ID 列表 但无法做到 例如 我知道 Rodney Dangerfield 在
  • numpy.cov() 返回意外的输出

    我有一个 X 数据集 有 9 个特征和 683 行 683x9 我想获取这个 X 数据集和另一个与 X 具有相同形状的数据集的协方差矩阵 我使用np cov originalData generatedData rowvar False 代
  • 大型数据集上的 Sklearn-GMM

    我有一个很大的数据集 我无法将整个数据放入内存中 我想在这个数据集上拟合 GMM 我可以用吗GMM fit sklearn mixture GMM 重复小批量数据 没有理由重复贴合 只需随机采样您认为机器可以在合理时间内计算的尽可能多的数据
  • 如何向 SCons 构建添加预处理和后处理操作?

    我正在尝试在使用 SCons 构建项目时添加预处理和后处理操作 SConstruct 和 SConscript 文件位于项目的顶部 预处理动作 生成代码 通过调用不同的工具 gt 不知道在此预处理之后将生成的确切文件 可以创建用于决定生成哪
  • Spyder 如何在同一线程的后台运行 asyncio 事件循环(或者确实如此?)

    我已经研究 asyncio 模块 功能几天了 因为我想将它用于我的应用程序的 IO 绑定部分 并且我认为我现在对它的工作原理有一个合理的理解 或者在至少我认为我已经理解了以下内容 任一时刻 任一线程中只能运行一个异步事件循环 一旦一切都设置
  • 如何正确消除字典中的元素直到只剩下一个字符串

    我真的需要这方面的帮助 def get winner dict winner new dict for winner in dict winner first letter winner 0 value dict winner winner
  • 如何使用Featuretools按列值从单个数据框中的多个列创建特征?

    我正在尝试根据之前的结果来预测足球比赛的结果 我在 Windows 上运行 Python 3 6 并使用 Featuretools 0 4 1 假设我有以下代表结果历史记录的数据框 原始数据框 https i stack imgur com
  • 从数据集的给定日期范围中提取属于一天的数据

    我有一个数据集 日期范围为 2018 年 1 月 12 日到 8 月 3 日 其中包含一些值 维数为my df数据框是 my df shape 9752 2 每行包含半小时频率 第一行开始于2018 01 12 my df iloc 0 D

随机推荐