线方程的系数错误

时间:2019-10-25 09:57:59

标签: python numpy opencv math computational-geometry

假设我有一个用[[[x1,y1],[x2,y2],...]检测到的线段cv2.HoughLinesP的列表。此列表仅表示每个线段的端点。

分段按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)]]

为更好地理解,它们绘制在下面显示的图像上:

enter image description here

我需要抓住最左边和最右边的线,将它们延伸到图像边界(分别是最左边和最右边的线段端点)。

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'

1 个答案:

答案 0 :(得分:2)

А虚拟问题是由类型不匹配引起的。

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

经过几次测试后,很明显c值是用正确的方法计算的。

最右行延伸的图像 enter image description here