打开CV:绘制表格缺少的轮廓/线条

时间:2020-04-09 07:38:38

标签: python opencv image-processing object-recognition opencv-drawcontour

我得到了一些缺少行的表,以便不关闭单元格(缺少封面)。空心线我不想垂直延伸,但是我想画一条水平线将其封闭。有什么方法可以检测空线并检索其最终位置以精确地在其中绘制轮廓?

enter image description here

我尝试了以下代码(来自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)

1 个答案:

答案 0 :(得分:0)

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

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

相关问题