Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Lane Detection by C++ OpenCV

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "C++ OpenCV how to achieve lane detection", the content is easy to understand, clear, hope to help you solve your doubts, now let the editor lead you to study and learn "C++ OpenCV how to achieve lane detection" this article.

1. Get the lane ROI area

The original picture is shown in the picture.

Use the following code snippet to get the ROI area. The ROI region point set is set according to the image features. Fill the ROI area with fillPoly, and finally deduct ROI from the original image through copyTo.

Void GetROI (Mat src, Mat & image) {Mat mask = Mat::zeros (src.size (), src.type ()); int width = src.cols; int height = src.rows; / / get the ROI area of the lane and only process this part vectorpts; Point ptA ((width / 8) * 2, (height / 20) * 19); Point ptB ((width / 8) * 2, (height / 8) * 7) Point ptC ((width / 10) * 4, (height / 5) * 3); Point ptD ((width / 10) * 5, (height / 5) * 3); Point ptE ((width / 8) * 7, (height / 8) * 7); Point ptF ((width / 8) * 7, (height / 20) * 19); pts = {ptA, ptB,ptC,ptD,ptE, ptF} FillPoly (mask, pts, Scalar::all); src.copyTo (image, mask);}

The mask image is shown in the figure. With mask images, we can do better follow-up processing to detect lane lines.

Second, lane detection 1. Grayscale, threshold Mat gray; cvtColor (image, gray, COLOR_BGR2GRAY); Mat thresh; threshold (gray, thresh, 180,255, THRESH_BINARY); imshow ("thresh", thresh)

The image after grayscale and threshold is shown in the following image.

two。 Get non-zero pixels

We split the image in two. The left half gets the left lane outline point; the right half gets the right lane outline point.

Vectorleft_line; vectorright_line; for (int I = 0; I

< thresh.cols / 2; i++) { for (int j = 0; j < thresh.rows; j++) { if (thresh.at(j, i) == 255) { left_line.push_back(Point(i, j)); } } } for (int i = thresh.cols / 2; i < thresh.cols; i++) { for (int j = 0; j < thresh.rows; j++) { if (thresh.at(j, i) == 255) { right_line.push_back(Point(i, j)); } } }3.绘制车道线 我们将从left_line、right_line容器中各拿出首尾两个点作为车道线的起始点。 注意:这里要加一个if判断语句,否则当容器为空时(未检测到车道线),容器会溢出。 if (left_line.size() >

0 & & right_line.size () > 0) {Point bounded L = (left_line [0]); Point tagged L = (left_ line [left _ line.size ()-1]); Point Point R = (right_line [0]); Point bound R = (right_ line [right _ line.size ()-1]) Circle (src, Beverl, 10, Scalar (0,0,255),-1); circle (src, Thuml, 10, Scalar (0255,0),-1); circle (src, Thornr, 10, Scalar (255,0,0),-1); circle (src, Benzr, 10, Scalar (0255,255),-1) Line (src, Point (Boulder), Point (Troul), Scalar (0,255,0), 10); line (src, Point (Thorr), Point (Bourr), Scalar (0,255,0), 10); vectorpts; pts = {Bauer, Thuml, Taur, Bourr} FillPoly (src, pts, Scalar (133,230,238);}

The final effect is shown in the figure.

These are all the contents of the article "how C++ OpenCV implements Lane Detection". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report