Small Intro
In this project, I've designed a Lane Follower Robot using OpenCV and Raspberry pi. This robot is capable of follow a lane(like in roadways). It can be used in used in industries for assisting the production process, human assistance etc. Okay!..Don't waste our time on theoretical things. Let's go through the code line by line. Now, I am going to explain how to process image and then video.
Step 1 : Importing Libraries
First we import OpenCV, Numpy, math and serial libraries.
import cv2
import numpy as np
import math
import serial
Step 2 : Read Image from your directory
image = cv2.imread(r'C:\Users\aasai\Desktop\new1.jpeg')
Step 3 : Color Conversion
Convert our BGR image into Gray. Gray Image only contain value ranging from [0-255].
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Step 4 : Image Smoothing
Image Blurring/Smoothing image smoothing in order to reduce noise. It is done with the help of various low pass filter kernels. Here we used Gaussian Kernel. To know more about different kernel. Click here
# given input image, kernel width =5 height = 5, Gaussian kernel standard deviation
k_width = 5
k_height = 5
blurred = cv2.GaussianBlur(gray, (k_width, k_height), 0)
Step 5 : Edge Detection
Canny edge detection algorithm is popular edge detection algorithm and it's used to detect edges in images. In order to preserve high gradient values in image, we select low and high threshold values. This will help us to preserve the strong edges in image.
# Find the edges in the image using canny detector
threshold1 = 80
threshold2 = 80
edged = cv2.Canny(blurred, threshold1, threshold2)
Step 6 : Find Lines
Hough transform is a feature extraction method for detecting simple shapes such as circles, lines etc in an image. Here, you can learn more about that.
# it will return line coordinates,it will return 3darray.
lines = cv2.HoughLinesP(edged,1,np.pi/180,max_slider,minLineLength,maxLineGap)
for x in range(0, len(lines)):
for x1,y1,x2,y2 in lines[x]:
# draw line in image using cv2.line function.
cv2.line(image,(x1,y1),(x2,y2),(255,0,0),3)
theta=theta+math.atan2((y2-y1),(x2-x1))
print(theta)
About theta calculation:
atan2() function return value in radians, which means value between –pi and pi representing the angle theta of a (x, y) point and positive x-axis.
you can take the coordinate of first line by using this line of code.
for x1,y1,x2,y2 in lines[0]:
Step 7 : Decision Making Part
Now, we are gonna set the threshold value, based on difference between threshold and theta value, we will send command to arduino for drive the motor in particular direction.
threshold=5
if(theta>threshold):
print("Go left")
if(theta<-threshold):
print("Go right")
if(abs(theta)<threshold):
print("Go straight")
Step 8 : Integrate
Arduino Code :
Arduino
will receive commands from Raspberry Pi Serial Port.
// assign pin num
int right_pin = 12;
int left_pin = 13;
int const ENA = 10;
int const ENB = 11;
// initial command
int command = 0;
void setup()
{
pinMode(right_pin, OUTPUT);
pinMode(left_pin, OUTPUT);
pinMode(ENA, OUTPUT); // set all the motor control pins to outputs
pinMode(ENB, OUTPUT);
Serial.begin(115200);
while (!Serial);
Serial.println("Opencv Lane Detect Autonomous Robot");
}
void loop() {
if (Serial.available())
{
//int state = Serial.parseInt();
int state = Serial.read();
if (state == 4)
{
digitalWrite(left_pin, LOW);
digitalWrite(right_pin, HIGH);
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
Serial.println("Left");
}
if (state == 2)
{
digitalWrite(left_pin, LOW);
digitalWrite(right_pin, LOW);
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
Serial.println("Right");
Serial.println("Reverse");
}
if (state == 3)
{
digitalWrite(left_pin, HIGH);
digitalWrite(right_pin, LOW);
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
Serial.println("Right");
}
if (state == 1)
{
digitalWrite(left_pin, HIGH);
digitalWrite(right_pin, HIGH);
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
Serial.println("Forward");
}
if (state == 5)
{
digitalWrite(left_pin, LOW);
digitalWrite(right_pin, LOW);
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
Serial.println("Stop");
}
}
}
Thanks Note
Thanks to OpenCV Python Documentation.
Thanks
for reading this project. Hope, this project gives you some insights about OpenCV Lane Following robot and you can do the same by following the given steps.
I work hard in silence, My robots makes Noise.
Comments