Demo entry 6719624

Cplus

   

Submitted by anonymous on Mar 13, 2018 at 07:11
Language: C++. Code size: 2.1 kB.

	CameraParameter parameter;
	ReadIpcParam(parameter_file_path, &parameter);	//从文件中读取车道参数
	left_array = parameter.left_array[no], right_array = parameter.right_array[no];//取某个车道
	mask = cv::Mat::zeros(cv::Size(parameter.width, parameter.height),CV_8UC1);	//车道的掩码图像
	for (j = 0; j < parameter.height; j++) {
		for (k = left_array[j]; k < right_array[j]; k++){
			mask.at<uchar>(j, k) = 255;		}	}
	cv::VideoCapture capture(video_file_path);		//读入视频
	while (1)	{
		capture >> frame;					//读取每一帧的图像  
		if (frame.empty()) break;				//读取完毕,退出循环
		cv::cvtColor(frame, gray_image, CV_BGR2GRAY);	//灰度化
		gray_image.copyTo(lane_image, mask);			//使用车道模版进行掩码操作
		memset(point_gray_statistics, 0, 256 * sizeof(int));	//灰度统计
		for (j = 0; j < parameter.height; j++){
			total_gray = 0, point_number = 0;
			for (i = left_array[j]; i < right_array[j]; i++){
				total_gray += lane_image.at<uchar>(j, i);
				point_number++;			}
			point_gray_statistics[total_gray / point_number]++;		
		}
		max_statistics_time = 0;
		for (j = 0; j < 256; j++){
			if (point_gray_statistics[j] > max_statistics_time){
				max_statistics_time = point_gray_statistics[j];
				statistics_background_gray = j;			}
		}
		for (j = 0; j < parameter.height; j++){		//背景差法取得前景图像
			for (i = left_array[j]; i < right_array[j]; i++){
		lane_image.at<uchar>(j, i) = abs(lane_image.at<uchar>(j, i) - statistics_background_gray);	}
		}		
		otsu_value = GetOtsuValue(lane_image, left_array, right_array);	//OTSU分析取得otsu值
		cv::threshold(lane_image, lane_image, otsu_value, 255, CV_THRESH_BINARY);//二值化
		cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5)); //矩形卷积核
		cv::erode(lane_image, lane_image, element);			//中值滤波
		cv::dilate(lane_image, lane_image, element);		//膨胀处理
		queue_y = GetQueueLine(lane_image, left_array, right_array);	//获得排队位置
		if (queue_y >= 0) cv::line(lane_image, cv::Point(left_array[queue_y], queue_y), 					cv::Point(right_array[queue_y], queue_y), cv::Scalar(200,200,200));
		imshow("lane", lane_image); imshow("TrafficVideo", frame);
		cv::waitKey(40);
	}
	capture.release();

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).