image - 如何从图像中删除所有线条? (水平、垂直、对角线)

标签 image opencv image-processing computer-vision imagemagick

我需要删除图像中的线条,这最终是一个表格。我找到了一种删除水平线和垂直线的方法:

convert 1.jpg -type Grayscale -negate -define morphology:compose=darken -morphology Thinning 'Rectangle:1x80+0+0<' -negate out.jpg

下图:

enter image description here

已转换为以下内容:

enter image description here

可以看到对角线仍然存在。我尝试将图像旋转 45 度,然后尝试将其删除,但也没有成功。怎么做到的?任何建议表示赞赏。我选择了 imagemagick,但欢迎任何其他选项

最佳答案

您可以尝试使用 cv2.HoughLinesP()检测对角线然后使用 mask 填充轮廓

import cv2
import numpy as np

image = cv2.imread('1.jpg')
mask = np.zeros(image.shape, np.uint8)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(gray,100,200)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
close = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel)
minLineLength = 10
maxLineGap = 350
lines = cv2.HoughLinesP(close,1,np.pi/180,100,minLineLength,maxLineGap)
for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(mask,(x1,y1),(x2,y2),(255,255,255),3)

mask = cv2.cvtColor(mask,cv2.COLOR_BGR2GRAY)
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

for c in cnts:
    cv2.drawContours(image, [c], -1, (255,255,255), -1)

cv2.imshow('mask', mask)
cv2.imshow('image', image)
cv2.imwrite('image.png', image)
cv2.waitKey()

关于image - 如何从图像中删除所有线条? (水平、垂直、对角线),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56978942/

相关文章:

c++ - cmake 和 tesseract,如何使用 cmake 链接

matlab - 使用 ASM 坐标进行眨眼检测

javascript - 如何将返回的数据ASCII显示为图像?

java - 图像 block 的缓冲图像

opencv - 基尼杂质,在 opencv 中生长随机树

python - 使用 cv2.resize 后获取旋转矩形的大小和位置

iphone - CGPoint 旋转改变与原点的距离

image-processing - 最快的图像处理库?

Javascript FileReader 多个图像

jquery - 如何设置jquery创建的内容的图像宽度