【发布时间】:2021-10-25 14:44:40
【问题描述】:
我想删除水平线和垂直线以下代码可以很好地删除水平线但是在这个Original Image
这是去除水平线的输出图像
这是附上的代码,你能告诉我如何删除垂直线
import cv2
import matplotlib.pyplot as plt
import glob
import os
def remove_lines(image_path,outdir):
image = cv2.imread(image_path)
result = image.copy()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Remove horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(remove_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(result, [c], -1, (255,255,255), 5)
# Remove vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
remove_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
#remove_vertical =cv2.dilate(remove_vertical , vertical_kernel, iterations=2)
cnts = cv2.findContours(remove_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(result, [c], -1, (255,255,255), 5)
cv2.imwrite(os.path.join(outdir,os.path.basename(image_path)), result)
plt.imshow(result)
for jpgfile in glob.glob(r'C:\custom\TableDetectionWork\images/*'):
print(jpgfile)
remove_lines(jpgfile,r'C:\custom\TableDetectionWork\output/')
【问题讨论】:
-
是什么阻碍了您理解您拥有的代码并将其从水平调整为垂直?请说明您的代码的来源。我知道它是从另一个 SO 答案中复制而来的。
-
删除水平线效果很好,但无法删除垂直线,如上图我使用了stackoverflow.com/questions/57961119/…中的代码
-
一种方法是将图像旋转 90 度,然后应用相同的代码,然后再旋转 90 度。另一种方法是在形态学中将水平内核更改为垂直内核。
-
水平内核到垂直内核?看不懂
-
你了解在这种情况下什么是“内核”吗?即此代码中的哪个变量称为“内核”?你知道......你的代码包含声称已经删除垂直线的代码。请编辑您的帖子并显示此代码生成的输入图像和输出图像。
标签: python opencv image-processing cv2 mathematical-morphology