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 ...
Link to screenshot
unfortunately, it's hard to help you, as long as all you got is a youtube vid, and some screenshot, all from other folks.
get your feet wet, and try the python tutorials , come back, if you have a more specific question ?
Hi berak, the screenshot is from my own project. I have been able to detect the road lines but I'm getting stuck in the next step i.e. how to detect the car moving away from the center and also detecting curves. I have spoken about the algorithm I was intending to use but I'm getting stuck in trying to translate it into OpenCV code. The video was merely linked to make it clear what I was trying to get at the end of the project.
oh, ofc. then - show us, what you've tried so far ;)
(edit question, and put it there.. seing your code would help, but please, as text, not as a screenshot ..)
Hi berak, I've updated the post with the code. I hope you had a chance to have a look at the youtube video. That is exactly what I'm trying to implement in this project. Thanks