Lane Departure Warning System Based on OpenCV

Keywords: OpenCV Embedded system Computer Vision image processing

Lane Departure Warning

development environment

Ubuntu 16.04

OpenCV

C++

Explanation of lane departure warning concept

Lane Departure Warning System is called Lane Department warning system in English. Therefore, Lane Departure Warning System is referred to as LDW system in many models. Its main function is to remind the driver by means of sound, flash and vibration when the vehicle unconsciously deviates from the lane through sensors, controllers and other components on the vehicle.

This article uses the traditional image processing method of OpenCV to realize the functions of lane detection and deviation alarm. Help you get started with image processing and OpenCV.

Environment construction

There are two ways to install OpenCV under Linux: compiling source code and directly installing binary files.

Compiling the source code takes a long time and has high requirements for the environment. It is easy to make mistakes in the process of compiling. The easiest way is to install using apt get.

apt-get update
apt-get install libcv-dev libopencv-photo-dev libopencv-contrib-dev

A test procedure:

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main (int argc, char **argv)
{
    Mat image, image_gray;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR );
    if (argc != 2 || !image.data) {
        cout << "No image data\n";
        return -1;
    }

    cvtColor(image, image_gray, CV_RGB2GRAY);
    namedWindow("image", CV_WINDOW_AUTOSIZE);
    namedWindow("image gray", CV_WINDOW_AUTOSIZE);

    imshow("image", image);
    imshow("image gray", image_gray);

    waitKey(0);
    return 0;
}

compile:

g++ test.cpp -o test -lopencv_core -lopencv_imgproc -lopencv_highgui

When running, add a picture to process the color picture into black-and-white picture, such as:

./test test.jpg

Image processing technology involved in lane detection

1. Read camera or video data

OpenCV is very simple to read video data. If you do not set too many parameters (frame rate and resolution), you only need to call the read function in the VideoCapture class.

VideoCapture cap;  
Mat mFrame;

cap.open(VIDEO_FILE_NAME);

while (cap.read(mFrame))
{
  imshow("mytest", mFrame);
  waitKey(10);
}

The imshow() function realizes the function of displaying a picture on the screen.

The waitKey() function is used to wait in milliseconds.

If it is to read the camera data, change the open parameter to 0. If it is a USB camera, change the parameter to 1.

2. Intercepted image

Most of the data read from the test video are unnecessary. What we need is the lane line on the ground. So you can cut off the top half of the intercepted picture first.

Use the Rect function in OpenCV.

mFrame(Rect(0, mFrame.rows / 2, mFrame.cols, mFrame.rows / 2));

The meanings of the four parameters are: starting from (0, mFrame.rows / 2) coordinates, mFrame.cols pixels to the right and mframe.rows/2 pixels to the down.


3. Convert to grayscale image

Color in nature is easily affected by light, and the amount of color picture data is very large, which is not conducive to later image processing. Therefore, if the image is converted into gray image, the amount of calculation in subsequent processing will be less.

The cvtColor function is used in OpenCV.

cvtColor(imageROI, mGray, COLOR_BGR2GRAY);

The meaning of three parameters: input image, output image and conversion to gray image.


4. Gaussian filtering

There are many fuzzy pixels or pixels that may affect post-processing (called "noise") in the gray image, which are filtered out by Gaussian filter.

The Gaussian blur function is used in OpenCV.

GaussianBlur(mGray, IPM_Gray, Size(7, 7), 1.5, 1.5);

Significance of each parameter: input image, output image, size of Gaussian kernel (size.width and size.height), standard deviation of Gaussian kernel function in X direction and standard deviation of Gaussian kernel function in Y direction.

Note: if you need to understand the meaning of the following three parameters in detail, you can baidu "Gaussian filtering"; If it's just to write code, it's not necessary.


5. Edge detection

Canny, a well-known edge detection algorithm, calculates the gradient value and direction according to the pixels around the pixels to determine the real and potential edges.

Canny function is used in OpenCV.

Canny(IPM_Gray, IPM_Gray, 80, 100, 3);

Meaning of each parameter: input image, output image, minimum threshold, maximum threshold, Sobel kernel size (internal use).

6. Hough transform

Hough transform is used to identify the features in the image, such as lines. The line consists of two points, and each point contains abscissa and ordinate, that is, a line segment consists of four data. Using Vec4i to represent line segments in OpenCV can be understood as an array, which contains four elements.

vector<Vec4i> lines;
HoughLinesP(IPM_Gray, lines, 1, CV_PI / 180, 10, 20, 50);

Meaning of each parameter: input image, all detected line segments, radius resolution, angle resolution, line segment length threshold, threshold between the nearest two points on the line segment.

7. Lane departure detection algorithm

There are many lane departure detection algorithms. Explain a relatively simple judgment method.


Through mathematical operation, the intersection of two lane lines and X axis can be obtained, which are left respectively_ X and right_x. If left_x and right_ If x is within a certain range, it is considered as normal driving; If left_x and right_ If x moves to the left, it is considered to be a right turn, otherwise it is a left turn.

8. Draw the lane

After the lane is detected, use the line function of OpenCV to draw a straight line to draw the lane.

line(mFrame, Point(right_lane[0], right_lane[1] + mFrame.rows * 2 / 3), 
                Point(right_lane[2], right_lane[3] + mFrame.rows * 2 / 3), 
                    Scalar(0, 255, 0), 3);

Meaning of each parameter: input image, start point of line, end point of line, color and line width.


8. Call the police

There are many alarm methods. Text is used in the code to remind. OpenCV edits text using the putText function.

putText(mFrame, "right", Point(450, 200), cv::FONT_HERSHEY_PLAIN,
                           4, cv::Scalar(0, 0, 255), 5);

Meaning of each parameter: input image, text, coordinates of the lower left corner of the text, font, font size, font color and font line width

Pay attention to official account and learn intelligent hardware

Background recovery lane detection source code

Posted by messer on Sun, 05 Dec 2021 09:36:08 -0800