簡體   English   中英

線方程的錯誤系數

[英]Wrong coefficients for the line equation

假設我有一個用cv2.HoughLinesP檢測到的線段列表[[[x1,y1],[x2,y2],...] 此列表僅表示每個線段的端點。

分段按它的 x2、x2 值排序,即相對於從中獲取分段的圖像“從左到右”排序。

segments_sorted = [[(0, 797), (46, 769)], [(2, 766), (138, 690)], [(220, 644), (399, 541)], [(427, 523), (615, 414)], [(460, 513), (615, 419)], [(495, 491), (614, 419)], [(753, 368), (843, 518)], [(958, 708), (1099, 706)], [(1047, 681), (1088, 729)], [(1047, 706), (1095, 761)]]

為了更好地理解,它們被繪制在下圖所示的圖像上:

在此處輸入圖像描述

我需要抓住最左邊和最右邊的線端將它們延伸到圖像邊界(分別是最左邊和最右邊的線段端點)。

left_most_segment = [(0, 797), (46, 769)]

right_most_segment = [(1047, 706), (1095, 761)]

def get_line_coefficients(line):
    (x1, y1), (x2, y2) = line
    a = y1 - y2,
    b = x2 - x1,
    c = (x1 - x2) * y1 + (y2 - y1) * x1

    return a, b, c

# using the Ax + By + C = 0 equation  - get the coefficients and update the  endpoints

r_a, r_b, r_c = get_line_coefficients(right_most_segment)

# l_a = -55
# l_b = 48
# l_c = 23697

問題是,對於我適合get_line_coefficients()的所有段, c數值太大,例如-3666223697

我之前想更新端點的 x,y 坐標。

# e.g. for the right-most segment, x1,y1 should now be updated

new_x = 0
new_y =  -(r_a*new_x + r_c) / r_b

new_x = image.shape[1]

new_y = r_a * new_x + r_c / r_b

sorted_lines[-1][1] = [new_x, new_y]

cv2.polylines(original, np.array(sorted_lines), False, RGB_BLUE, thickness=3)

cv2.polylines

...
cv2.error: OpenCV(4.1.1) /io/opencv/modules/imgproc/src/drawing.cpp:2435: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'polylines'

А 虛擬問題是由類型不匹配引起的。

new_y = int(-(r_a * new_x + r_c) / r_b) # should be an int

經過幾次測試,很明顯c值是以正確的方式計算的。

最右邊線延伸的圖像在此處輸入圖像描述

暫無
暫無

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

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