Ask Your Question

Revision history [back]

How to check LAne Departing in OpenCV 3?

Hey guys,

I'm using OpenCV 3 in Python 2.7. My project's aim is to build an autonomous lane departing robot that can detect the two lanes on its sides and continuously correct itself to remain within them. I want to achieve something like this project watch?v=R_5XhnmDNz4 (add to end of youtube link...can't post links).

So far I've done the line detection part from the live video feed using both HoughLines and HoughLinesP. I'm not very proficient in OpenCV and python and the part where I'm stuck at is converting the logic of detecting the lane departure of the robot. For straight roads, I thought of tracking the midpoint of the perpendicular that joins the two boundaries of the roads. I'm not able to convert this logic into code and would appreciate any help I can get in this. I cannot seem to share a link of the screenshot as the editor won't let me.

How to check LAne Lane Departing in OpenCV 3?

Hey guys,

I'm using OpenCV 3 in Python 2.7. My project's aim is to build an autonomous lane departing robot that can detect the two lanes on its sides and continuously correct itself to remain within them. I want to achieve something like this project watch?v=R_5XhnmDNz4 (add to end of youtube link...can't post links).

So far I've done the line detection part from the live video feed using both HoughLines and HoughLinesP. I'm not very proficient in OpenCV and python and the part where I'm stuck at is converting the logic of detecting the lane departure of the robot. For straight roads, I thought of tracking the midpoint of the perpendicular that joins the two boundaries of the roads. I'm not able to convert this logic into code and would appreciate any help I can get in this. I cannot seem to share a link of the screenshot as the editor won't let me.

How to check Lane Departing in OpenCV 3?

Edit 26/3/17

I've added the code I've worked on so far. It uses the Hough Transform function as in the OpenCV example. Based on the rho and theta values of the lines through trial and error I tried to implement a basic logic to check whether the car is drifting left or right.

What I want to achieve is a more robust way of tracking how the robot is departing from the lanes. Some sort of central line marker that can be used to detect if the robot has moved away from the center. My understanding is that averaging the lines on the lanes into two lines (left and right lanes) and then working with their slopes should give some result. However, I've not been able to transform this into code.

Code:

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import numpy as np
import serial

#The two element array is [rho theta] i.e. output of the Hough Transform

image=0
rawCapture=0
camera= PiCamera()
camera.resolution=(240,120)
camera.framerate=10
rawCapture=PiRGBArray(camera, size=(240,120))
time.sleep(0.1)
#ser=serial.Serial('/dev/ttyUSB0',9600)

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    image=frame.array
    img_f=cv2.flip(image,-1)
    gray= cv2.cvtColor(img_f,cv2.COLOR_BGR2GRAY)
    blur=cv2.GaussianBlur(gray,(5,5),0)
    edges= cv2.Canny(blur,50,150,apertureSize=3)
    lines= cv2.HoughLines(edges, 1, np.pi/180, 60)
    cords=[]
    if lines is not None:
        for x in range(0,len(lines)):
            for rho,theta in lines[x]:
                a=np.cos(theta)
                b=np.sin(theta)
                x0=a*rho
                y0=b*rho
                x1=int(x0+1000*(-b))
                cords.append(x1)
                y1=int(y0+1000*(a))
                cords.append(x1)
                x2=int(x0-1000*(-b))
                cords.append(x1)
                y2=int(y0-1000*(a))
                cords.append(x1)
                print rho,
                print "RRRRRRRR"
                print theta,
                print "TTTTTTTTTTTT"
                #print cords,
                #print "CCCCCCCCCCCCCCCC"
                #cv2.line(img_f,(x1,y1),(x2,y2),(0,255,0),1)
                if rho<60:
                    cv2.line(img_f,(x1,y1),(x2,y2),(0,255,0),1)
                    print lines[x]
                    if theta<1:
                        if theta>0.558:
                            print "Move Right"
                            #ser.write('R')
                        else:
                            print "Continue Straight"
                    if theta>1:
                        if theta>2.6 or rho>-120:
                            print "Move Left"
                            #ser.write('L')
                        else:
                            print "Continue Straight"
                    else:
                        print "Move Straight"

        else:
            print "No line"        

        cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
        cv2.imshow('frame',edges)
        key=cv2.waitKey(1)& 0xFF
        rawCapture.truncate(0)
        if(key==ord("q")):
              break

Hey guys,

I'm using OpenCV 3 in Python 2.7. My project's aim is to build an autonomous lane departing robot that can detect the two lanes on its sides and continuously correct itself to remain within them. I want to achieve something like this project watch?v=R_5XhnmDNz4 (add to end of youtube link...can't post links).

So far I've done the line detection part from the live video feed using both HoughLines and HoughLinesP. I'm not very proficient in OpenCV and python and the part where I'm stuck at is converting the logic of detecting the lane departure of the robot. For straight roads, I thought of tracking the midpoint of the perpendicular that joins the two boundaries of the roads. I'm not able to convert this logic into code and would appreciate any help I can get in this. I cannot seem to share a link of the screenshot as the editor won't let me.

How to check Lane Departing in OpenCV 3?

Edit 26/3/17

I've added the code I've worked on so far. It uses the Hough Transform function as in the OpenCV example. Based on the rho and theta values of the lines through trial and error I tried to implement a basic logic to check whether the car is drifting left or right.

Here are some screenshots of the project I've done so far.

What I want to achieve is a more robust way of tracking how the robot is departing from the lanes. Some sort of central line marker that can be used to detect if the robot has moved away from the center. My understanding is that averaging the lines on the lanes into two lines (left and right lanes) and then working with their slopes should give some result. However, I've not been able to transform this into code.

Code:

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import numpy as np
import serial

#The two element array is [rho theta] i.e. output of the Hough Transform

image=0
rawCapture=0
camera= PiCamera()
camera.resolution=(240,120)
camera.framerate=10
rawCapture=PiRGBArray(camera, size=(240,120))
time.sleep(0.1)
#ser=serial.Serial('/dev/ttyUSB0',9600)

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    image=frame.array
    img_f=cv2.flip(image,-1)
    gray= cv2.cvtColor(img_f,cv2.COLOR_BGR2GRAY)
    blur=cv2.GaussianBlur(gray,(5,5),0)
    edges= cv2.Canny(blur,50,150,apertureSize=3)
    lines= cv2.HoughLines(edges, 1, np.pi/180, 60)
    cords=[]
    if lines is not None:
        for x in range(0,len(lines)):
            for rho,theta in lines[x]:
                a=np.cos(theta)
                b=np.sin(theta)
                x0=a*rho
                y0=b*rho
                x1=int(x0+1000*(-b))
                cords.append(x1)
                y1=int(y0+1000*(a))
                cords.append(x1)
                x2=int(x0-1000*(-b))
                cords.append(x1)
                y2=int(y0-1000*(a))
                cords.append(x1)
                print rho,
                print "RRRRRRRR"
                print theta,
                print "TTTTTTTTTTTT"
                #print cords,
                #print "CCCCCCCCCCCCCCCC"
                #cv2.line(img_f,(x1,y1),(x2,y2),(0,255,0),1)
                if rho<60:
                    cv2.line(img_f,(x1,y1),(x2,y2),(0,255,0),1)
                    print lines[x]
                    if theta<1:
                        if theta>0.558:
                            print "Move Right"
                            #ser.write('R')
                        else:
                            print "Continue Straight"
                    if theta>1:
                        if theta>2.6 or rho>-120:
                            print "Move Left"
                            #ser.write('L')
                        else:
                            print "Continue Straight"
                    else:
                        print "Move Straight"

        else:
            print "No line"        

        cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
        cv2.imshow('frame',edges)
        key=cv2.waitKey(1)& 0xFF
        rawCapture.truncate(0)
        if(key==ord("q")):
              break

Hey guys,

I'm using OpenCV 3 in Python 2.7. My project's aim is to build an autonomous lane departing robot that can detect the two lanes on its sides and continuously correct itself to remain within them. I want to achieve something like this project watch?v=R_5XhnmDNz4 (add to end of youtube link...can't post links).

So far I've done the line detection part from the live video feed using both HoughLines and HoughLinesP. I'm not very proficient in OpenCV and python and the part where I'm stuck at is converting the logic of detecting the lane departure of the robot. For straight roads, I thought of tracking the midpoint of the perpendicular that joins the two boundaries of the roads. I'm not able to convert this logic into code and would appreciate any help I can get in this.