-1

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\numpy\lib\function_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

lafeo_007
  • 74
  • 9
  • 1
    Does this answer your question? ["OverflowError: Python int too large to convert to C long" on windows but not mac](https://stackoverflow.com/questions/38314118/overflowerror-python-int-too-large-to-convert-to-c-long-on-windows-but-not-ma) – Giorgos Myrianthous Feb 05 '21 at 15:24
  • @GiorgosMyrianthous hey, it does but since my code is a little vast your solution doesn't allow me to understand the exact source of my error. it would be better if someone provided the source error. – lafeo_007 Feb 05 '21 at 15:32

1 Answers1

0

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)
Andrewgmz
  • 355
  • 3
  • 11
  • hi, @Andrewgmz well i might sound like an absolute beginner here, however, this is the error that springs up when i try making the change you suggested. `cv2.line(line_image, (x1, y1), (x2, y2), (0, 255, 0), 2) TypeError: Expected Ptr for argument 'img'` – lafeo_007 Feb 06 '21 at 19:32
  • What happens is that you can do operations like multiply images, add etc. with a floating data type, double or other to get better precision. However, the format of the images is integer 8bits, so whenever you use OpenCV functions you must always cast to integer. Sorry, I forgot that detail. Could you attach the images that you are using, so I can better see the type of data that the displayLines function generates. – Andrewgmz Feb 08 '21 at 12:38
  • sure, i am editing the document with the videos – lafeo_007 Feb 09 '21 at 14:03
  • the links been attahed to the original post can u have a look? – lafeo_007 Feb 11 '21 at 20:42
  • I need the video "challenge_video.mp4" to test the code on my own – Andrewgmz Feb 13 '21 at 13:15
  • its attached do have a look again? – lafeo_007 Feb 14 '21 at 19:59