int find_line(void) //循迹线程
{
// 初始化程序
//UI_init();
VideoCapture capture;
capture.open(0);
if (!capture.isOpened())
{
cout << "Can not open camera" << endl;
return -1;
}
else
cout << "camera open successful" << endl;
capture.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
capture.set(cv::CAP_PROP_FPS, 30);
capture.set(cv::CAP_PROP_FRAME_WIDTH, 320);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, 240);
this_thread::sleep_for(chrono::milliseconds(1000)); //让图像稳定
cout << "Program started" << endl;
while (1)
{
capture >> img;
/*
if (start_flag == 1)
{
cvtColor(img, img_HSV, COLOR_BGR2HSV);
Scalar HSV_L = Scalar(100, 43, 46);
Scalar HSV_H = Scalar(124, 255, 255);
inRange(img_HSV, HSV_L, HSV_H, img_HSV_mask);
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
morphologyEx(img_HSV_mask,img_HSV_mask, MORPH_OPEN, kernel);
morphologyEx(img_HSV_mask, img_HSV_mask, MORPH_CLOSE, kernel);
if (find_blue_card_flag == 0)find_blue_card();
else find_blue_card_remove();
}
else
{
img_per = per_wt(img);
Mat HLS_Lresult = HLS_Lthresh(img_per, HSL_Lmin, HSL_Lmax);
Mat LAB_Bresult = LAB_BThreshold(img_per, LAB_Bmin, LAB_Bmax);
img_combined = combineThresholds(HLS_Lresult, LAB_Bresult);
Mat white_image(img.size(), img.type(), Scalar::all(255));
LaneData lanes = slidingWindow(img_combined);
lanes.left_fit = polyFit(lanes.left_points);
lanes.right_fit = polyFit(lanes.right_points);
if (lanes.left_fit.size() != 3 || lanes.right_fit.size() != 3)
{
if(lanes.left_fit.size() != 3)center_x=lanes.right_fit[0] * line_y * line_y + lanes.right_fit[1]* line_y + lanes.right_fit[2]-rail_width_pix;
if(lanes.right_fit.size()!= 3)center_x=lanes.left_fit[0] * line_y * line_y + lanes.left_fit[1]* line_y + lanes.left_fit[2]+rail_width_pix;
cout<<center_x<<endl;
}
if (lanes.left_fit.size() == 3 && lanes.right_fit.size() == 3)
{
center_x=(lanes.right_fit[0] * line_y * line_y + lanes.right_fit[1]* line_y + lanes.right_fit[2]+lanes.left_fit[0] * line_y * line_y + lanes.left_fit[1]* line_y + lanes.left_fit[2])/2;
cout<<center_x<<endl;
}
line_error = center_x - 160;
//line_error=100;
cout << left_x << "," << right_x << "," << center_x << "," << line_error << endl;
visualize(white_image, lanes);
//namedWindow("img", cv::WINDOW_NORMAL); // 创建可调整大小的窗口
//resizeWindow("img", 320, 240); // 设置窗口尺寸
//moveWindow("img", 320, 50); // 设置窗口位置
//imshow("img", img_clone);
namedWindow("combined_image", cv::WINDOW_NORMAL); // 创建可调整大小的窗口
resizeWindow("combined_image", 320, 240); // 设置窗口尺寸
moveWindow("combined_image", 640, 50); // 设置窗口位置
imshow("combined_image", img_combined);
namedWindow("white_image", cv::WINDOW_NORMAL); // 创建可调整大小的窗口
resizeWindow("white_image", 320, 240); // 设置窗口尺寸
moveWindow("white_image", 960, 50); // 设置窗口位置
imshow("white_image", white_image);
//Start_motor();
}
*/
if (cv::waitKey(1) == 27)break;
//if(car_blake_flag == 1) ;//break;//结束代码
}
gpioTerminate();
capture.release(); // 释放摄像头
destroyAllWindows(); // 关闭所有窗口
//uart_close(GPS);
return 0;
}Mat per_wt(Mat& frame)
{
int img_h, img_w;
img_clone = frame.clone();
img_h = img_clone.rows; // 图像高度
img_w = img_clone.cols;
vector<cv::Point2f> src = {
Point2f(125, 198),
Point2f(213, 198),
Point2f(295, 237),
Point2f(55, 237)
};
vector<cv::Point2f> dst = {
Point2f(100,0),
Point2f(img_w - 100, 0),
Point2f(img_w - 100, img_h),
Point2f(100, img_h)
};
// 定义目标坐标(变换后的四个点)
vector<cv::Point> polygon;
for (const auto& pt : src)
{
polygon.push_back(Point(static_cast<int>(pt.x), static_cast<int>(pt.y)));
}
polylines(img_clone, polygon, 1, Scalar(0, 0, 255), 3, cv::LINE_AA);
//for (size_t i = 0; i < src.size(); ++i)
//{
// circle(img, cv::Point(static_cast<int>(src[i].x), static_cast<int>(src[i].y)), 5, cv::Scalar(0, 0, 255), -1);
//}
// 计算透视变换矩阵
Mat M = getPerspectiveTransform(src, dst);
Mat Minv = getPerspectiveTransform(dst, src);
// 应用透视
warpPerspective(frame, warped, M, cv::Size(img_w, img_h), cv::INTER_LINEAR);
waitKey(5);
return warped;
}void crossroad(void)//斑马线识别
{
Mat blur;
while(1)
{
GaussianBlur(img, blur, Size(5, 5), 0);
Mat hsv;
cvtColor(blur, hsv, COLOR_BGR2HSV);
Scalar lower_white = Scalar(0, 0, 200);
Scalar upper_white = Scalar(180, 40, 255);
Mat cross_mask;
inRange(hsv, lower_white, upper_white, cross_mask);
// Mat edges;
// Canny(blur, edges, 50, 150);
// bitwise_or(cross_mask, edges, cross_mask);
Mat kernel = getStructuringElement(MORPH_RECT, Size(7, 7));
//dilate(mask1, mask1, kernel);
//erode(mask1, mask1, kernel);
morphologyEx(cross_mask, cross_mask, MORPH_CLOSE, kernel);
Rect roi(0,240,640,240);
Mat region = cross_mask(roi);
imshow("combined_image", region);
//imshow("combined_image", mask1_roi);
int stripe_count = 0, transitions = 0;
for (int i = 0; i < region.rows; i++)
{
transitions = 0;
for (int j = 5; j < region.cols - 5; j++) {
if (region.at<uchar>(i, j) != region.at<uchar>(i, j - 1))
transitions++;
}
//cout<<"transitions:"<<transitions<<endl;
if (transitions >= 6) stripe_count++;
}
cross_flag = (stripe_count >= 20) ? 1 : 0;
cout << "stripe_count"<<stripe_count<<"cross_flag:" << cross_flag << endl;
if(cross_flag==1){Control_stop();audio();break;}
waitKey(5);
}
}