我对 Python 很陌生,我正在尝试在插图中定义手绘线条,正如我编写的代码所示,在我使用 cv2.Canny 对图像进行模糊、腐蚀和阈值处理后,那些显示为线条的图像在我们看来,代码是平行的
然后我只显示平行的,但我不知道如何定义最小和最大距离值(以像素为单位)
请问有什么帮助吗?
这是我到目前为止所写的:
import numpy as np
import cv2
import time
import math
start_time = time.time()
base = cv2.imread('prova/eq.jpg')
gray = cv2.cvtColor(base, cv2.COLOR_BGR2GRAY)
img = cv2.bilateralFilter(gray, 9, 90, 1)
kernel = np.ones((2,2), np.uint8)
img_dilation = cv2.dilate(img, kernel, iterations=2)
img_erosion = cv2.erode(img, kernel, iterations=4)
edges = cv2.Canny(img_erosion,cv2.THRESH_BINARY+cv2.THRESH_OTSU,550,apertureSize = 3)
#edges = cv2.Canny(gray,1,250,apertureSize = 3)
cv2.imwrite('edges.jpg',edges)
minLineLength=50
maxLineGap=10
threshold=20
image=edges
lines = cv2.HoughLinesP(image=image,rho=1,theta=np.pi/180, threshold=threshold,lines=np.array([]), minLineLength=minLineLength,maxLineGap=maxLineGap)
angles = []
for x1,y1,x2,y2 in lines[0]:
theta = math.atan(float(y2 - y1) / (x2 - x1))
angles.append({'theta': theta, 'p1': (x1, y1), 'p2': (x2, y2)})
for i in range(len(angles)):
for j in range(i + 1, len(angles)):
delta = abs(angles[i]['theta'] - angles[j]['theta'])
if delta < 0.01:
print(delta)#, angles[i], angles[j])
cv2.line(base, angles[i]['p1'], angles[i]['p2'], (0, 0, 255), 2, cv2.CV_AA)
cv2.line(base, angles[j]['p1'], angles[j]['p2'], (255, 0, 0), 2, cv2.CV_AA)
a,b,c = lines.shape
#for i in range(a):
# cv2.line(base, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 13, cv2.CV_AA)
cv2.imwrite('prova/prova.jpg',base)
print("--- %s seconds ---" % (time.time() - start_time))