#include<string>
#include<vector>
#include<iostream>
#include"roadDetection.h"
//#define __TEST__
#define __DEBUG__
using namespace std;
using namespace cv;
//调试时显示处理后的图片
void myShow(std::string str, Mat inputImage)
{
#ifdef __DEBUG__
imshow(str, inputImage);
#endif
}
void testShow(std::string str, Mat inputImage)
{
#ifdef __TEST__
imshow(str, inputImage);
#endif
}
//高斯去噪
void RoadDetection::deNoise(Mat inputImage, Mat& outputImage)
{
GaussianBlur(inputImage, outputImage, Size(3, 3), 0, 0);
//blur(inputImage, outputImage, Size(7, 7)); //均值
//medianBlur(inputImage, outputImage, 7); //中值
//bilateralFilter(inputImage, outputImage, 25, 25 * 2, 25 / 2); //双边
}
//闭运算
void RoadDetection::CloseOperation(Mat inputImage, Mat& outputImage)
{
Mat element = getStructuringElement(MORPH_RECT, Size(7, 7));
//进行形态学操作
//morphologyEx(inputImage, outputImage, MORPH_GRADIENT, element);
//闭运算
morphologyEx(inputImage, outputImage, MORPH_CLOSE, element);
//morphologyEx(inputImage, outputImage, MORPH_OPEN, element);
//morphologyEx(inputImage, outputImage, MORPH_DILATE, element);
//膨胀
//dilate(inputImage, outputImage, element);
//腐蚀
//erode(inputImage, outputImage, element);
}
//设置ROI
void RoadDetection::mask(Mat inputImage, Mat& outputImage) {
Mat mask = Mat::zeros(inputImage.size(), inputImage.type());
Point pts[4] = {
//Point(inputImage.cols / 4, inputImage.rows / 4),
Point(0, inputImage.rows>>1),
//Point(inputImage.cols /4, inputImage.rows >> 1),
Point(0, inputImage.rows),
Point(inputImage.cols, inputImage.rows),
Point(inputImage.cols, inputImage.rows >>1)
//Point(inputImage.cols/4*3, inputImage.rows >> 1)
};
// Create a binary polygon mask
fillConvexPoly(mask, pts, 4, Scalar(255, 255, 255));
// Multiply the edges image and the mask to get the output
bitwise_and(inputImage, mask, outputImage);
////定义一个Mat类型并给其设定ROI区域
//Mat imageROI;
////方法一
//imageROI = inputImage(Rect(0, inputImage.rows >> 1, inputImage.cols, inputImage.rows>>1));
}
Mat Histogram(Mat inputImage)
{
/// 分割成3个单通道图像 ( R, G 和 B )
vector<Mat> rgb_planes;
split(inputImage, rgb_planes);
/// 设定bin数目
int histSize = 255;
/// 设定取值范围 ( R,G,B) )
float range[] = { 0, 255 };
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat r_hist, g_hist, b_hist;
/// 计算直方图:
calcHist(&rgb_planes[0], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);
//calcHist(&rgb_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate);
//calcHist(&rgb_planes[2], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
// 创建直方图画布
int hist_w = 400; int hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_w, hist_h, CV_8UC3, Scalar(0, 0, 0));
/// 将直方图归一化到范围 [ 0, histImage.rows ]
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
//normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
//normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
/// 在直方图画布上画出直方图
for (int i = 1; i < histSize; i++)
{
line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(r_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h - cvRound(r_hist.at<float>(i))),
Scalar(255, 255, 255), 2, 8, 0);
//line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(g_hist.at<float>(i - 1))),
// Point(bin_w*(i), hist_h - cvRound(g_hist.at<float>(i))),
// Scalar(0, 255, 0), 2, 8, 0);
//line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))),
// Point(bin_w*(i), hist_h - cvRound(b_hist.at<float>(i))),
// Scalar(255, 0, 0), 2, 8, 0);
}
return histImage;
}
//直方图均衡化
void RoadDetection::HistEqualize(Mat inputImage, Mat& outputImage)
{
cvtColor(inputImage, inputImage, CV_RGB2GRAY);
testShow("cvtColor", inputImage);
testShow("均衡化前直方图", Histogram(inputImage));
vector<Mat> splitBGR(inputImage.channels());
split(inputImage, splitBGR);
for (int i = 0; i < inputImage.channels(); ++i)
{
equalizeHist(splitBGR[i], splitBGR[i]);
}
Mat mergeImg;
merge(splitBGR, mergeImg);
mergeImg.copyTo(outputImage);
testShow("均衡化后直方图", Histogram(outputImage));
//outputImage.convertTo(outputImage, -1, 1, 20);
//testShow("convertTo", outputImage);
}
//亮度特征
void RoadDetection::Brightness(Mat inputImage, Mat& outputImage)
{
//cvtColor(inputImage, inputImage, CV_RGB2GRAY);
//testShow("cvtColor", inputImage);
//Mat tmp1,tmp2;
//threshold(inputImage, tmp1, 140, 255, THRESH_TOZERO);
//threshold(inputImage, tmp1, 245, 255, THRESH_BINARY);
//testShow("250", tmp1);
//threshold(inputImage, tmp2, 200, 255, THRESH_BINARY);
//testShow("200", tmp2);
//bitwise_and(tmp1, tmp2, outputImage);
//addWeighted(tmp1, 1, tmp2, 1, 0.0, outputImage);
threshold(inputImage, outputImage, 250, 255, THRESH_BINARY);
}
//颜色阈值
void RoadDetection::colorThreshold(Mat inputImage, Mat& outputImage)
{
//cvtColor(inputImage, outputImage, CV_RGB2GRAY);
//threshold(inputImage, outputImage, 140, 255, THRESH_BINARY);
Mat bgr,hsv, mask,result;
testShow("input", inputImage);
inputImage.convertTo(bgr, CV_32FC3, 1.0 / 255, 0);
testShow("bgr", bgr);
cvtColor(bgr, hsv, COLOR_BGR2HSV);
testShow("COLOR_BGR2HSV", hsv);
inRange(hsv, Scalar(0, 0, (float)(230)/255), Scalar(90, (float)255/255, (float)(255)/255), mask);
//inRange(hsv, Scalar(0, 0, (float)(150) / 255), Scalar(90, (float)255 / 255, (float)(255) / 255), mask);
testShow("inRange", mask);
//只保留
//result = Mat::zeros(bgr.size(), CV_32FC3);
//for (int r = 0; r < bgr.rows; r++)
//{
// for (int c = 0; c < bgr.cols; c++)
// {
// if (mask.at<uchar>(r, c) == 255)
// {
// result.at<Vec3f>(r, c) = bgr.at<Vec3f>(r, c);
// }
// }
//}
result = mask;
result.convertTo(result, CV_8UC3, 255.0, 0);
outputImage = result.clone();
}
//边缘检测
void RoadDetection::edgeDetector(Mat inputImage, Mat& outputImage)
{
//testShow("threshold", outputImage);
Canny(inputImage, outputImage, 150, 100, 3); //Canny边缘检测算子
//Laplacian(inputImage, outputImage, CV_16S, 3, 1, 0, BORDER_DEFAULT); //拉普拉斯算子
//convertScaleAbs(outputImage, outputImage);
//Soble边缘检测算子
//Mat grad_x, grad_y;
//Mat abs_grad_x, abs_grad_y;
////【3】求 X方向梯度
//Sobel(inputImage, grad_x, CV_16S, 1, 0, 3, 1, 1, BORDER_DEFAULT);
//convertScaleAbs(grad_x, abs_grad_x);
//imshow("【效果图】 X方向Sobel", abs_grad_x);
////【4】求Y方向梯度
//Sobel(inputImage, grad_y, CV_16S, 0, 1, 3, 1, 1, BORDER_DEFAULT);
//convertScaleAbs(grad_y, abs_grad_y);
//imshow("【效果图】Y方向Sobel", abs_grad_y);
////【5】合并梯度(近似)
//addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, outputImage);
//imshow("【效果图】整体方向Sobel", outputImage);
}
//霍夫线概率变换
vector<Vec4i> RoadDetection::houghLines(Mat inputImage)
{
vector<Vec4i> line;
HoughLinesP(inputImage, line, 1, CV_PI / 180, 20, 20, 30);
return line;
}
//画线
void drawDetectedLines(Mat &image, vector<Vec4i> lines, Scalar color = Scalar(255, 255, 255))
{
vector<Vec4i>::const_iterator it2 = lines.begin();
while (it2 != lines.end()) {
Point pt1((*it2)[0], (*it2)[1]);
Point pt2((*it2)[2], (*it2)[3]);
line(image, pt1, pt2, color);
++it2;
}
}
//selected_lines
void RoadDetection::lineSeparation(const vector<Vec4i>& lines, vector<std::vector<cv::Vec4i> >& output)
{
size_t j = 0;
Point ini;
Point fini;
double slope_thresh = 0.3;
vector<double> slopes;
vector<Vec4i> selected_lines;
vector<Vec4i> right_lines, left_lines;
// 根据斜率筛选
for (auto i : lines)
{
ini = cv::Point(i[0], i[1]);
fini = cv::Point(i[2], i[3]);
没有合适的资源?快使用搜索试试~ 我知道了~
基于C++实现的车道线检测识别系统源码+注释说明
共12个文件
jpg:9个
cpp:2个
h:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 192 浏览量
2024-01-26
19:59:49
上传
评论 1
收藏 1.56MB ZIP 举报
温馨提示
<项目介绍> 基于C++实现的车道线检测识别系统源码+注释说明 - 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 --------
资源推荐
资源详情
资源评论
收起资源包目录
Lane-Detection-master.zip (12个子文件)
Lane-Detection-master
roadDetection.cpp 18KB
road
test1.jpg 212KB
test3.jpg 80KB
straight_lines1.jpg 151KB
test6.jpg 227KB
test5.jpg 238KB
test4.jpg 196KB
straight_lines2.jpg 189KB
test7.jpg 144KB
test2.jpg 170KB
main.cpp 3KB
roadDetection.h 2KB
共 12 条
- 1
资源评论
机器学习的喵
- 粉丝: 1050
- 资源: 1439
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功