我有一个包含文本但有非直线绘制的图像。
我想删除这些行而不影响/删除文本中的任何内容。
为此,我使用了霍夫概率变换:
import cv2
import numpy as np
def remove_lines(filename):
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 200)
lines = cv2.HoughLinesP(edges, rho=1, theta=1*np.pi/180,
threshold=100, minLineLength=100, maxLineGap=5)
# Draw lines on the image
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 3)
cv2.imwrite('result', img)
Run Code Online (Sandbox Code Playgroud)
结果不如我预期的好:
没有完全检测到线条(仅检测到线条的某些线段,直线段)。
我对cv2.Canny和cv2.HoughLinesP参数做了一些调整,但是也没有用。
我也尝试过cv2.createLineSegmentDetector(由于许可证问题,在最新版本的opencv中不可用,因此我不得不将opencv降级到4.0.0.21版):
import cv2
import numpy as np
def remove_lines(filename):
im = cv2.imread(filename)
gray …Run Code Online (Sandbox Code Playgroud)