(opencv+python)利用概率hough线变换消除重叠线

2024-10-06 12:36:40 发布

您现在位置:Python中文网/ 问答频道 /正文

再见!我对python还很陌生。我正在开发一个可以探测行人路线的程序。我的问题是,当使用概率Hough变换时,线被检测出来,但它们彼此重叠。我如何去除重叠的线条? 这是我的代码:

import cv2
import numpy as np
import math
from matplotlib import pyplot as plt
from calibrate import undist

def auto_canny(img, sigma = 0.5):
    v = np.median(img)

    lower = int(max(0,(1.0-sigma)*v))
    upper = int(min(255,(1.0+sigma)*v))
    edged = cv2.Canny(img, lower, upper)
    edged = cv2.dilate(edged, None, iterations=1)
    edged = cv2.erode(edged, None, iterations=1)

    return edged

img  = cv2.imread('sample.jpg')
img.shape
green = img[:,:,1]
blurred = cv2.GaussianBlur(green, (5,5), 0)

autoEdges = auto_canny(blurred)

minLineLength = img.shape[1]-10000
lines = cv2.HoughLinesP(image=autoEdges,rho=1,theta=np.pi/500,threshold=10,lines=np.array([]),minLineLength=minLineLength,maxLineGap=90)

a,b,c = lines.shape
for i in range(a):
    xst=lines[i][0][0]
    yst=lines[i][0][1]
    xnd=lines[i][0][2]
    ynd=lines[i][0][3]
    cv2.line(img,(xst, yst), (xnd, ynd), (0,0,255), 1, cv2.LINE_AA)
    l = math.sqrt(((abs(xnd-xst))^2) + ((abs(ynd-yst))^2))
    rho = (xst*ynd - xnd*yst)/l
    dist = abs(rho)
    m = (ynd - yst)/(xnd-xst)   
    print (dist,m)


cv2.imshow('result',img)
cv2.imshow('canny',autoEdges)

k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()
elif k == ord('a'):
    cv2.imwrite('OUTPUTCANNY.png',autoEdges)
    cv2.imwrite('OUTPUTCANNYWITHHOUGH.png',img)
    cv2.destroyAllWindows()

以下是每行的输出值(rho,slope):

(2138.987461393825, 0)

(9352.1609578182488, 0)

(2786.3685089632231, 0)

(459.45861938801005, 0)

(74.176540269582901, 0)

(7768.377424418768, 0)

(4323.5582400556614, 0)

(1457.9223924831122, 0)

(4029.5491996504829, 0)

(353.1785277501566, 0)

(3429.0843443517056, 0)

(687.44444444444446, 0)

(1001.540320481475, 0)

(4891.3687385623834, 0)

(6324.1371540947503, 0)

(5782.5260784389111, 0)

(2142.4394280125407, 0)

(3419.373032213327, 0)

(79.606923443428798, 0)

(4081.4477628728268, 0)

(2548.076237638998, 0)

(2075.2538668232146, 0)

(96.599999999999994, 0)

(28.918275651682048, 0)

(457.23808531952665, 0)

(563.81287237538288, 0)

(4522.6641535572326, 0)

(21.582043818522273, 0)

(2072.2164335243606, 0)

(446.51735688548547, 0)

(4145.9017474324037, 0)

(181.84369168362207, 0)

(2232.0294867294269, 0)

(2003.5982177527055, 0)

(5148.1880307541214, 0)

(654.14939315987181, 0)

(114.49162997063731, 0)

(1256.9505554297596, 0)

(1765.2144695745915, 0)

(835.27600228906385, 0)

(331.66247903554, 0)

(433.90321501459283, 0)

(80.786267723119749, 0)

(678.50865875094041, 0)

(75.599999999999994, 0)

(1698.1082622291476, 0)

(4893.1250194343038, 0)

(870.45171061088456, 0)

(714.65656087382285, 0)

(605.84788121475981, 0)

(2227.8458409210211, 0)

(475.17575695735991, 0)

(6150.4292926708586, 0)

(2489.7061482035415, 0)

(75.894663844041105, 0)

(603.33333333333337, 0)

(973.49884437527714, 0)

假设应该有14条线(边缘)被检测,但是总共检测到72条线,如上面的数据所示(rho,斜率)。有人能提出一种消除这些不必要的线路的方法吗?谢谢您。在


Tags: importimgnpcv2sigmalinesshaperho