1

我对 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))
4

2 回答 2

1

这是我写的,现在应该可以了

import numpy as np
import cv2
import time
import math
from PIL import Image

start_time = time.time()
base = cv2.imread('img/2009/no.jpg')
#goldouzian5 //  karimzadech4 // kitatani5 watanabe4   baccala5
gray = cv2.cvtColor(base, cv2.COLOR_BGR2GRAY)
img = cv2.bilateralFilter(gray, 11, 80, 11)

kernel = np.ones((2,2), np.uint8)
img_dilation = cv2.dilate(img, kernel, iterations=2)
img_erosion = cv2.erode(img, kernel, iterations=5)



edges = cv2.Canny(img_erosion,cv2.THRESH_BINARY+cv2.THRESH_OTSU,550,apertureSize = 3)
cv2.imwrite('edges.jpg',edges)

base = base



minLineLength=50
maxLineGap=11
threshold=25
image=edges

lines = cv2.HoughLinesP(image=image,rho=1,theta=np.pi/180, threshold=threshold,lines=np.array([]), minLineLength=minLineLength,maxLineGap=maxLineGap)
print("--- %s seconds (hough) ---" % (time.time() - start_time))

def segmentProjectedLength(p1, p2, q1, q2, theta):
    rot = lambda p: (p[0] * math.cos(theta) + p[1] * math.sin(theta), p[0] * math.sin(theta) - p[1] * math.cos(theta))
    p1 = rot(p1)
    p2 = rot(p2)
    q1 = rot(q1)
    q2 = rot(q2)

    minP = min(p1[0], p2[0])
    maxP = max(p1[0], p2[0])
    minQ = min(q1[0], q2[0])
    maxQ = max(q1[0], q2[0])

    return (min(maxP, maxQ) - max(minP, minQ)) if maxP >= minQ and maxQ >= minP else 0

lines = lines[0] if lines is not None else []
angles = []
for x1,y1,x2,y2 in lines:
    theta = math.atan(float(y2 - y1) / (x2 - x1)) if (x2 != x1) else math.pi / 2
    Rho = (x1 * y2 - x2 * y1) / math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
    angles.append({'theta': theta, 'p1': (x1, y1), 'p2': (x2, y2), 'rho': Rho})
    #cv2.line(base, (x1, y1), (x2, y2), (0, 255, 0), 2, cv2.CV_AA)
print('--- %d lines found ---' % len(angles))
for i in range(len(angles)):
    for j in range(i + 1, len(angles)):

        delta = abs(angles[i]['theta'] - angles[j]['theta'])
        dist = abs(angles[i]['rho'] - angles[j]['rho'])
        projLength = segmentProjectedLength(angles[i]['p1'], angles[i]['p2'], angles[j]['p1'], angles[j]['p2'], angles[j]['theta'])

        if delta < 0.05 and dist > 5 and dist < 20 and projLength > 5:

                #print  (dist)
                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)
cv2.imwrite('prova/prova.jpg',base)
print("--- %s seconds (complete) ---" % (time.time() - start_time))
于 2017-05-02T10:18:03.810 回答
0

对于平行线(相同角度),绝对rho差等于线之间的距离。

Dist = |line1.rho - line2.rho|

如果无法访问线的“rho”参数(例如没有 P返回的函数 HoughLines),请使用

Len = Sqrt((x2-x1)^2 + (y2-y1)^2)
Rho = (x1 * y2 - x2 * y1) / Len
于 2017-04-28T08:10:51.960 回答