Ask Your Question
0

How to check Lane Departing in OpenCV 3?

asked 2017-03-17 08:57:46 -0500

updated 2017-03-29 11:50:06 -0500

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

(more)
edit retag flag offensive close merge delete

Comments

CVLearner gravatar imageCVLearner ( 2017-03-17 08:58:48 -0500 )edit

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 ?

berak gravatar imageberak ( 2017-03-17 10:15:57 -0500 )edit
1

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.

CVLearner gravatar imageCVLearner ( 2017-03-17 13:14:07 -0500 )edit

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

berak gravatar imageberak ( 2017-03-17 13:20:39 -0500 )edit

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

CVLearner gravatar imageCVLearner ( 2017-03-26 13:02:06 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-03-29 12:23:27 -0500

updated 2017-03-29 12:30:58 -0500

These line following robots usually use always the same tricks.

If you detect two lines, you can easily find out the center of these two lines by calculating the mean position of both lines.

Then, if the center of the image (where the robot is looking at) is not aligned with the center of the two lines, you just need to compensate by moving left or right, depending on which direction the offset is.

These are pretty basic computer vision and programming algorithms in general, if you can't think of a way of translating this into code then you need some more practice :)

Good luck!

edit flag offensive delete link more

Comments

totally agree! I needed qualified https://handmadewritings.com (writing help) for similar project and I ended passing online Python courses. And after completing them I finally was able to finish this project(well, to be honest, I rewrote it from the scratch)Other obstacles are: OpenCV library adding, numpy adding and that's all. The only key is practice!

BaileyKicksAss gravatar imageBaileyKicksAss ( 2017-04-21 09:09:00 -0500 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2017-03-17 08:57:46 -0500

Seen: 535 times

Last updated: Mar 29 '17