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;
img_per = per_wt(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
{
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);
}
if (cv::waitKey(1) == 27)break;
//if(car_blake_flag == 1) ;//break;//结束代码
}
gpioTerminate();
capture.release(); // 释放摄像头
destroyAllWindows(); // 关闭所有窗口
//uart_close(GPS);
return 0;
}void find_blue_card_remove(void)//蓝色挡板移开
{
cout << "detecting blue card remove" << endl;
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(img_HSV_mask, contours, hierarcy, RETR_EXTERNAL, CHAIN_APPROX_NONE);
if (contours.size() > 0)
{
sort(contours.begin(), contours.end(), Contour_Area);
vector<vector<Point>> newContours;
for (const vector<Point>& contour : contours)
{
Point2f center;
float radius;
minEnclosingCircle(contour, center, radius);
if (center.y > 90 && center.y < 160)
{
newContours.push_back(contour);
}
}
contours = newContours;
if (contours.size() == 0)
{
start_flag = 0;
cout << "blue card remove" << endl;
Start_motor();
sleep(2);
}
}
else
{
start_flag = 0;
cout << "blue card remove" << endl;
Start_motor();
sleep(2);
}
}主程序调用find_line函数,find_line函数里又用到了find_blue_card_remove函数(拿开篮板启动电机),现在遇到问题是拿开篮板小车立刻跑,但是图像要延缓一会才能出来并巡线,更改程序位置后又有拿开篮板要呆一会开图像才能启动电机,我想要达到效果为拿开篮板,小车立刻跑的同时也能开启图像巡线
最新发布