简体   繁体   中英

Wrong coefficients for the line equation

Suppose I have a list of line segments [[[x1,y1],[x2,y2],...] detected with cv2.HoughLinesP . This list represent only the endpoints of each line segment.

Segments were sorted by It's x2, x2 value ie ordered "from left to right" with respect to the image, from which the segments were taken from.

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

For better understanding, they were drawn on the image that is shown below:

在此处输入图像描述

I need to grab the left-most and the right-most lines end extend them to image boundaries (left-most and the right-most segment endpoint respectively).

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

The problem is that, for all the segments that I fit into get_line_coefficients() the c coefficient value is too big, like -36662 or 23697 .

I thought previously to update the x,y coordinate of the endpoint.

# 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 breakes at

...
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'

А dummy problem was caused by type mismatch.

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

After several tests It becomes apparent that c value was computed in the right way.

the image with right-most line extended在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM