Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

Why is Python (int) too large for C in my OpenCV Lane Detection?

The below code is for a lane detection algo, I have used Canny Edge detection to work the follow code. However, there are a few errors that I am facing if you could check and let me know it would be of great help. I feel the errors are somewhere around the various arguments and parameters in Python since I might not entirely be thorough with the maths of the problem.

import cv2 import numpy as np import matplotlib.pyplot as plt #108 300 def empty(a) : pass

cv2.namedWindow('image')
cv2.resizeWindow('image',640,240)

#---track-bars

cv2.createTrackbar('MIN-VAL','image', 0, 300, empty)
cv2.createTrackbar('MAX-VAL','image',0, 300, empty)

# while True:





def ROI1(image):
    height = image.shape[0]
    triangle = np.array([
        [(200, height), (1150, height), (700, 400)]
    ])
    mask = np.zeros_like(image)
    cv2.fillPoly(mask, triangle, 255)
    return mask

def ROI2(image):
    height = image.shape[0]
    triangle = np.array([
        [(0, 650), (1050, height), (750, 445)]
    ])
    mask = np.zeros_like(image)
    cv2.fillPoly(mask, triangle, 255)
    return mask

def avergaeLines(image, linez):
    left_fit = []
    right_fit = []

    for line in linez:
        x1, y1, x2, y2 = line.reshape(4)
        parameters = np.polyfit((x1, x2), (y1, y2), 1)
        slope = parameters[0]
        intercept = parameters[1]
        if slope < 0:
             left_fit.append((slope, intercept))
        else:
            right_fit.append((slope, intercept))
    left_Fit_Average = np.average(left_fit, axis=0)
    right_Fit_Average = np.average(right_fit, axis=0)
    left_line = coord(image, left_Fit_Average)
    right_line = coord(image, right_Fit_Average)
    return np.array([left_line, right_line])

def displayLines(image, lines):
    line_image = np.zeros_like(image)
    if lines is not None:
        for line in lines:
            x1, y1, x2, y2 = line.reshape(4)
            cv2.line(line_image, (x1, y1), (x2, y2), (0, 255, 0), 5)
    return line_image

def coord(image, linePara):
    try:
        slope, intercept = linePara
    except TypeError:
        slope, intercept = 0.001, 0
    y1 = image.shape[0]
    y2 = int(y1*(4/5))
    x1 = int((y1 - intercept)/slope)
    x2 = int((y2-intercept)/slope)
    return np.array([x1, y1, x2, y2])

cap = cv2.VideoCapture("challenge_video.mp4")



#img = cv2.imread("test3.jpg")




while True:
    success, img = cap.read()
    while True:
        lane_image = np.copy(img)
        # cv2.imshow("a", lane_image)
        gray = cv2.cvtColor(lane_image, cv2.COLOR_RGB2GRAY)
        # cv2.imshow("b", gray)
        blur_gray = cv2.GaussianBlur(gray, (5, 5), 0)
        # cv2.imshow("c", blur_gray)
        min_val = cv2.getTrackbarPos('MIN-VAL', 'image')
        max_val = cv2.getTrackbarPos('MAX-VAL', 'image')
        canny_blur_gray = cv2.Canny(blur_gray, 0, 159)
        # cv2.imshow("f", canny_blur_gray)
        mask = ROI2(canny_blur_gray)
        ADDED = cv2.bitwise_and(canny_blur_gray, mask)
        lines = cv2.HoughLinesP(ADDED, 2, np.pi/180, 100, np.array([]), minLineLength=70, maxLineGap=4)
        #70 AND 7 WORKS FINE
        averagedLines = avergaeLines(lane_image, lines)
        linesImage = displayLines(lane_image, averagedLines)
        comboImage = cv2.addWeighted(lane_image, 0.8, linesImage, 1, 1)
        key = cv2.waitKey(100)
        cv2.waitKey(0) & 0xFF

        cv2.imshow("RESULT", comboImage)
        if cv2.waitKey(0) & 0xFF == ord('c'):
            break





        #188 but 209 seems perfect
    # print(min_wal, max_val)
    #ROI_image = ROI(canny_blur_gray)

Below are the various errors that come -

packages
umpylibfunction_base.py:380: RuntimeWarning: Mean of empty slice.
avg = a.mean(axis)
RuntimeWarning: invalid value encountered in double_scalars
ret = ret.dtype.type(ret / rcount)
Traceback (most recent call last):
     
linesImage = displayLines(lane_image, averagedLines)
in displayLines
cv2.line(line_image, (x1, y1), (x2, y2), (0, 255, 0), 5)
OverflowError: Python int too large to convert to C long

https://youtu.be/LfnBPeoyGBg

https://youtu.be/SCXg2fDbkCg

question from:https://stackoverflow.com/questions/66065926/why-is-python-int-too-large-for-c-in-my-opencv-lane-detection

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need the array elements to have a larger data type, like double. Try this. I think that would be enough:

line_image = np.zeros_like(image,dtype=np.longdouble)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...