【发布时间】:2020-10-18 17:42:45
【问题描述】:
我正在尝试使用一些变换来去除图像中的虚线和实线,但我只能使用变形变换和霍夫线检测来去除其中的一些。
这里是一个例子:我需要删除虚线和长垂直线,同时不影响其他任何东西。
灰度图像输入:
到目前为止,这是我的代码:
thresh = cv2.threshold(num_bloc, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Remove vertical
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(num_bloc, [c], -1, (255,255,255), 2)
edges = cv2.Canny(num_bloc, 75, 150)
rho = 1 #Distance resolution of the accumulator in pixels.
theta = np.pi/180 #Angle resolution of the accumulator in radians.
threshold = 300 #Only lines that are greater than threshold will be returned.
minLineLength = 50 #Line segments shorter than that are rejected.
maxLineGap = 10 #Maximum allowed gap between points on the same line to link them
lines = cv2.HoughLinesP(edges, rho = rho, theta = theta, threshold = threshold,
minLineLength = minLineLength, maxLineGap = maxLineGap)
if lines is not None:
if lines.size>0 :
a,b,c = lines.shape
for i in range(a):
x1=lines[i][0][0]
y1=lines[i][0][1]-5
x2=lines[i][0][2]
y2=lines[i][0][3]+5
area = np.array([[x1, y1], [x2, y1], [x2, y2], [x1, y2]])
# cv2.line(table, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
# cv2.rectangle(table, (x1,y1-10 ), (x2,y2+10), (36,255,12), 2)
cv2.fillPoly(num_bloc, [area], color=(255,255,255))
我得到的输出(在输入图像的一个子集上):
如您所见,垂直虚线仍在此处。
关于如何删除所有线条(虚线和实线)、垂直、水平或任何角度的任何建议?
【问题讨论】: