簡體   English   中英

(opencv+python) 在使用概率霍夫線變換時去除重疊線

[英](opencv+python) Getting rid of overlapping lines when using probabilistic hough line transform

美好的一天! 我對python還是很陌生。 我正在開發一個可以檢測行人線的程序。 我的問題是,當使用概率霍夫變換時,檢測到線條但它們彼此重疊。 如何擺脫重疊的線條? 這是我的代碼:

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.58204381​​8522273, 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 條線(邊緣),但如上面的數據(rho,slope)所示,總共檢測到 72 條線。 任何人都可以提出一種消除這些不必要線條的方法嗎? 謝謝。

對於那種問題,不要考慮語言特定的問題。

讓我先澄清你的問題。 我將舉一個我之前的實驗的例子。
(我創建了一個新帳戶,因此我的聲譽很低並且圖像顯示為鏈接)

這是我的原始圖像: original_image

當我使用 HoughLinesP 時,我得到了這樣的結果: houghlinep_result
我很高興只看到兩行,但我得到了類似這樣的行列表:

[[[  0  59 104   3]]
 [[  2  59  56  32]]
 [[ 96   3 174  57]]
 [[  4  59  49  36]]
 [[125  25 174  58]]
 [[ 53  34  99   7]]
 [[111  12 165  50]]]

我的目標是有兩行: better_result

[[  0  59 104   3]
 [ 96   3 174  57]]

這就是我想要的,因為它們在同一條線上,具有不同的長度和非常相似的角度。

如果我的問題的這個解決方案是您所需要的,您可以找到兩點之間的角度並檢查是否已經存在類似的角度。

一小部分示例:

lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), min_line_length, max_line_gap)
cleans = np.empty(shape=[0,4], dtype=np.int32)

for l in lines:
        alfa = degrees(atan2(l[0][2]-l[0][0], l[0][3]-l[0][1]))

        if len(cleans) == 0:
            cleans = np.append(cleans, [l[0]], axis=0)
            continue

        similar = False
        for c in cleans:
            beta = degrees(atan2(c[2]-c[0], c[3]-c[1]))
            if abs(alfa-beta) <= 3.5:
                similar = True
                break

        if not similar:
            cleans = np.append(cleans, [l[0]], axis=0)

    print(cleans)

    for line in [cleans]:
        for x1,y1,x2,y2 in line:
            cv2.line(img,(x1,y1),(x2,y2),255,2)

它使用atan2函數找到兩點之間的角度。
請記住,代碼沒有優化。
它將角度與示例中的閾值3.5進行比較。
如果低於閾值,則認為是相似線,不添加到干凈數組中。

在您的問題中,如果您有超過 2 條線並且可能在不同坐標中具有相似的角度,則您必須進行額外檢查,可能使用網格大小。
如果是這種情況,請分享幾個屏幕截圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM