定义线之间的距离

时间:2017-04-28 07:19:05

标签: python opencv math line canny-operator

我是Python的新手,我正试图在插图中定义手绘线条,因为我写的代码显示,在我模糊之后,侵蚀并用cv2对图像做一个阈值。但是那些那些出现在我们眼中的线条在代码中是平行的

(此行line显示为此this

然后我只显示并行的但我不知道如何定义最小和最大距离值(以像素为单位)

请帮忙吗?

这是我到目前为止写的:

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))

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分:0)

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

Dist = |line1.rho - line2.rho|

如果无法访问行的'rho'参数(如函数HoughLines 没有P 返回),请使用

计算它
Len = Sqrt((x2-x1)^2 + (y2-y1)^2)
Rho = (x1 * y2 - x2 * y1) / Len