python - 在特定区域查找轮廓
问题描述
我在查找表格中特定列的轮廓时遇到问题。使用 opencv 时的问题是,我必须指定在 x、y 和 w(2 个轴和宽度)上使用 if 循环在哪个区域绘制矩形,如果表格线完全水平或垂直,但如果它们'不是那么你就得不到好的结果。
我用这段代码来获取边界框:
import numpy as np
import cv2
img = r'C:\Users\lenovo\PycharmProjects\SoftOCR_Final\Filtered_images\filtered_image1.png'
table_image_contour = cv2.imread(img, 0)
table_image = cv2.imread(img)
ret, thresh_value = cv2.threshold(table_image_contour, 180, 255, cv2.THRESH_BINARY_INV)
kernel = np.ones((5, 5), np.uint8)
dilated_value = cv2.dilate(thresh_value, kernel, iterations=1)
contours, hierarchy = cv2.findContours(dilated_value, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
# bounding the
if 2271 > x > 746 and w > 300 and y > 1285:
table_image = cv2.rectangle(table_image, (x, y), (x + w, y + h), (0, 0, 255), 3)
roi = table_image[y: y + h, x: x + w]
width = int(table_image.shape[1] * 20 / 100)
height = int(table_image.shape[1] * 20 / 100)
dsize = (width, height)
table_image = cv2.resize(table_image, dsize)
cv2.imshow('1', table_image)
cv2.waitKeyEx(0)
这是我正在处理的图像:
这就是我想画边界框的地方。只有那部分
现在这就是我真正得到的。
如您所见,我丢失了一些线条并在表格之外获得了边界框。
解决方案
推荐阅读
- flask - 关于 2 个不同模型之间的一对多关系的 Flask SQL Alchemy 问题
- javascript - 带有回调细节的 JS CustomEvent
- java - 为什么 arrayList.contains() 在这个用例中返回 false,而 System.out.print() 在控制台上写的相同?
- python - Visual Studio Code 中出现问题
- python - 如何就地获取数组中项目的出现次数
- python - “for”循环未正确循环
- javascript - 反应:用数组信息按列填充表格
- go - Sql.Open 全局连接的最佳方法
- java - Spring Boot 多数据源设置
- javascript - 在 javascript 中执行 asnyc 方法的传统方式