0

我得到了一些缺少行的表格,因此单元格没有关闭(封面丢失)。我不想垂直延伸的开放线,而是我想画一条水平线来关闭它们。有没有办法检测开放线并检索它们的结束位置以准确绘制轮廓?

在此处输入图像描述

我尝试了以下代码(来自https://stackoverflow.com/questions/22240746/recognize-open-and-closed-shapes-openc v ),但它没有在图像中绘制线条:

img_vh = cv2.imread('YOURPATH')

ret, thresh = cv2.threshold(img_vh, 200, 255, cv2.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
hierarchy = hierarchy[0]


for i, c in enumerate(contours):
    if hierarchy[i][2] < 0 and hierarchy[i][3] < 0:
        lined = cv2.drawContours(img_vh, contours, i, (0, 0, 255), 2)
    else:
        lined = cv2.drawContours(img_vh, contours, i, (0, 255, 0), 2)
cv2.imwrite("YOURPATH/lined.jpg", lined)
4

1 回答 1

0

你可以用霍夫变换解决这个问题。示例用法可以在这里找到:

https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html

于 2020-04-09T08:19:48.727 回答