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