当前位置:   article > 正文

第十八届全国大学生智能车室外组5g开源(小白小白小白队)

第十八届全国大学生智能车室外组5g开源(小白小白小白队)

第一次参加智能车室外赛,我和队友都是小白,所以队名叫小白小白小白(重复字语音识别准确率高哈哈),虽然经历很多坎坷,但是也学到很多东西。代码本身没有多少东西,都是最基本的图像处理,方案开源旨在互相学习,下面简述方案,仅供学习参考。

1.基本循迹

室外环境复杂多变,得到一个稳定的图像尤为重要,其中方案的鲁棒性是首要,同时要便于调试。秉承大道至简的原则,使用双边滤波弱化图像赛道纹理,高斯模糊去除图像绝大多数的高频信号,保留图像低频信号,最后使用canny边缘检测获得清晰的赛道边界。

在得到理想的边缘检测图像后可以发现,图像中仍会因为各种原因对循迹造成影响,使用霍夫线变换提取指定角度线条即可达到较为理想的图像。

  1. 代码仅供参考
  2. std::vector<cv::Vec4i> lines;
  3. cv::HoughLinesP(dilated_ca, lines, 1, CV_PI / 180, 70, 25, 5);
  4. cv::Mat line_image = cv::Mat::zeros(dilated_ca.size(), CV_8UC1);
  5. // 过滤指定范围外的角度=============《《《开始》》》
  6. std::vector<cv::Vec4i> filtered_lines;
  7. //先画左线
  8. double min_angle = -90; // 最小角度(以度为单位)
  9. double max_angle = -18; // 最大角度(以度为单位)
  10. for (size_t i = 0; i < lines.size(); i++)
  11. {
  12. cv::Vec4i line = lines[i];
  13. double angle_rad = atan2(line[3] - line[1], line[2] - line[0]); // 计算角度(弧度)
  14. double angle_deg = angle_rad * 180 / CV_PI; // 将角度从弧度转换为度
  15. if (angle_deg >= min_angle && angle_deg <= max_angle) {
  16. filtered_lines.push_back(line);
  17. }
  18. }
  19. // 在图像上绘制筛选后的线条
  20. for (size_t i = 0; i < filtered_lines.size(); i++)
  21. {
  22. const cv::Vec4i& line = filtered_lines[i];
  23. const cv::Point pt1(line[0], line[1]);
  24. const cv::Point pt2(line[2], line[3]);
  25. cv::line(line_image, pt1, pt2, cv::Scalar(255), 2, cv::LINE_AA);
  26. }
  27. //再画右线
  28. min_angle = 18; // 最小角度(以度为单位)
  29. max_angle = 90; // 最大角度(以度为单位)
  30. for (size_t i = 0; i < lines.size(); i++)
  31. {
  32. cv::Vec4i line = lines[i];
  33. double angle_rad = atan2(line[3] - line[1], line[2] - line[0]); // 计算角度(弧度)
  34. double angle_deg = angle_rad * 180 / CV_PI; // 将角度从弧度转换为度
  35. if (angle_deg >= min_angle && angle_deg <= max_angle)
  36. {
  37. filtered_lines.push_back(line);
  38. }
  39. }
  40. // 在图像上绘制筛选后的线条
  41. for (size_t i = 0; i < filtered_lines.size(); i++)
  42. {
  43. const cv::Vec4i& line = filtered_lines[i];
  44. const cv::Point pt1(line[0], line[1]);
  45. const cv::Point pt2(line[2], line[3]);
  46. cv::line(line_image, pt1, pt2, cv::Scalar(255), 2, cv::LINE_AA);
  47. }
  48. // 最后对图像进行膨胀处理
  49. // 膨胀核
  50. cv::Mat kernel2 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(2, 2));
  51. // 对边缘检测后的图像进行膨胀处理
  52. cv::Mat dilated_ca2;
  53. cv::dilate(line_image, dilated_ca2, kernel2, cv::Point(-1, -1), 1);
  54. // 过滤指定范围外的角度=============《《《结束》》》》

Video_1697621771909

 

最后扫线方案多样,参考室内智能车,这里不多赘述。

由于操场赛道弯道相当平缓,所以PID算法使用最简单的位置式即可,在图像处理稳定,帧率跑满时,参数基本上瞎给都能达到极高的循迹速度。引入该车自带组合导航模块获取偏航角角速度适当消抖便可进一步提升速度(参考逐飞)。

2.元素识别

2.1.斑马线识别。

依旧秉承大道至简的原则,利用循迹时获得的边缘检测图像,检测其闭合轮廓即可,适当限制条件便可准确识别到斑马线。

当然由于规则上说明要停止到30cm内,仅仅看到斑马线直接刹车是不够的,实时获取斑马线位置提前减速,检测斑马线与小车距离,以此达到精准识别和精准停车。

  1. 代码仅供参考
  2. int BanMa_Find(cv::Mat BanMa_Find_data)
  3. {
  4. // 查找图像中的轮廓
  5. std::vector<std::vector<cv::Point>> contours;
  6. cv::findContours(BanMa_Find_data, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
  7. // 创建一个副本以便绘制轮廓
  8. cv::Mat contour_img = BanMa_Find_data.clone();
  9. static int count_BMXduilie[8];
  10. int Y_point[20];//避障坐标值
  11. int count_BMX = 0;//斑马线标志
  12. // 定义矩形的大小宽度阈值(根据实际需要调整)
  13. int min_wh = 5; // 最小宽度
  14. int max_wh = 55; // 最大宽度
  15. // 遍历每个找到的轮廓
  16. for (const auto& contour : contours)
  17. {
  18. cv::Rect rect = cv::boundingRect(contour);
  19. if (min_wh <= rect.height && rect.height < max_wh && min_wh <= rect.width && rect.width < max_wh)
  20. {
  21. //过滤赛道外的轮廓
  22. if (rect.y >= 10 && rect.y <= 85)
  23. {
  24. if (rect.y % 2 == 0) rect.y = rect.y - 1;
  25. if (rect.y >= 85)
  26. {
  27. if(rect.x >= (20) && rect.x <= (300))
  28. {
  29. cv::rectangle(contour_img, rect, cv::Scalar(255), 2);
  30. // 打印轮廓坐标信息
  31. // std::cout << "轮廓坐标X = " << rect.x << " 轮廓坐标Y = " << rect.y << std::endl;
  32. count_BMX++;
  33. Y_point[count_BMX] = rect.y;
  34. }
  35. }
  36. else
  37. {
  38. if (rect.x >= (Left_Add_Line[rect.y] - 20) && rect.x <= (Right_Add_Line[rect.y] + 20))
  39. {
  40. cv::rectangle(contour_img, rect, cv::Scalar(255), 2);
  41. // 打印轮廓坐标信息
  42. // std::cout << "轮廓坐标X = " << rect.x << " 轮廓坐标Y = " << rect.y << std::endl;
  43. count_BMX++;
  44. Y_point[count_BMX] = rect.y;
  45. }
  46. }
  47. }
  48. if(count_BMX >= 6) count_BMX = 6;
  49. }
  50. }
  51. int pingJun = (Y_point[1] + Y_point[2] + Y_point[3]+ Y_point[4])/4;
  52. pingJun = (Q_jdz(pingJun - Y_point[1]) + Q_jdz(pingJun - Y_point[2]) + Q_jdz(pingJun - Y_point[3])+ Q_jdz(pingJun - Y_point[4]))/4;
  53. //printQ("pingJun",pingJun);
  54. if (count_BMX >= 4)
  55. {
  56. banmaxian_Y = (Y_point[1] + Y_point[2] +Y_point[3] +Y_point[4])/4;
  57. // std::cout << "斑马线坐标 = " << banmaxian_Y << std::endl; //
  58. count_BMX = 0;
  59. count_BMXduilie[7] = 1;
  60. if(pingJun >= 5)//看他们横坐标的平均值
  61. {
  62. return 0;
  63. }
  64. }
  65. else
  66. {
  67. count_BMX = 0;
  68. count_BMXduilie[7] = 0;
  69. }
  70. count_BMXduilie[0] = count_BMXduilie[1];
  71. count_BMXduilie[1] = count_BMXduilie[2];
  72. count_BMXduilie[2] = count_BMXduilie[3];
  73. count_BMXduilie[3] = count_BMXduilie[4];
  74. count_BMXduilie[4] = count_BMXduilie[5];
  75. count_BMXduilie[5] = count_BMXduilie[6];
  76. count_BMXduilie[6] = count_BMXduilie[7];
  77. for (int num = 0; num <= 7; num++)
  78. {
  79. if (count_BMXduilie[num] == 1)
  80. {
  81. count_BMX++;
  82. }
  83. }
  84. // 显示带有轮廓外接矩形的图像
  85. // cv::imshow("contour_img", contour_img);
  86. //最终返回值
  87. if (count_BMX >= 3) //4个就停车
  88. {
  89. return 1;
  90. }
  91. else return 0;
  92. }

 

2.2.避障

使用hsv颜色阈值二值化图像,判断其蓝色色块是否在小车所处的赛道内,过滤赛道外干扰色块,返回赛道内障碍物坐标映射在循迹图像中,根据色块坐标适当规划循迹路径即可完成避障。

  1. 代码仅供参考
  2. int find_XYdata[3];//障碍物坐标
  3. int find_XYdata_second[3];//障碍物坐标
  4. int BZ_chuli(cv::Mat BZdata)//传入原图
  5. {
  6. cv::Mat hsvvvv;
  7. cv::cvtColor(cropped_imageddddd, hsvvvv, cv::COLOR_BGR2HSV);
  8. // cv::imshow("yuantu2", BZdata);
  9. // cv::imshow("yuantu", cropped_imageddddd);
  10. // 定义红色和蓝色范围
  11. //红色阈值
  12. cv::Scalar red_lower1(150 ,0,0);
  13. cv::Scalar red_upper1(180, 255, 255);
  14. // cv::Scalar red_lower1(Hmin ,Smin, Vmin);
  15. //cv::Scalar red_upper1(Hmax, Smax, Vmax);
  16. cv::Scalar red_lower2(0, 140, 150);
  17. cv::Scalar red_upper2(10, 255, 250);
  18. //蓝色阈值
  19. //cv::Scalar blue_lower(Hmin ,Smin, Vmin);
  20. // cv::Scalar blue_upper(Hmax, Smax, Vmax);
  21. cv::Scalar blue_lower(80,85, 107);
  22. cv::Scalar blue_upper(115, 255, 255);
  23. //黄色阈值
  24. // cv::Scalar yellow_lower(Hmin ,Smin, Vmin);
  25. //cv::Scalar yellow_upper(Hmax, Smax, Vmax);
  26. cv::Scalar yellow_lower(0, 19, 71);
  27. cv::Scalar yellow_upper(54, 255, 255);
  28. // std::cout << "BZdata图像尺寸: " << BZdata.size() << std::endl;
  29. // 高斯模糊
  30. //cv::imshow("yuantu1", BZdata);
  31. // cv::GaussianBlur(BZdata, BZdata, cv::Size(5, 5), 2);
  32. //cv::imshow("yuantu2", BZdata);
  33. // 颜色提取(红色和蓝色)
  34. cv::Mat red_mask, blue_mask,red_mask2;
  35. //黄色图像
  36. cv::Mat yellow_mask;
  37. cv::inRange(BZdata, yellow_lower, yellow_upper, yellow_mask);
  38. cv::inRange(hsvvvv, blue_lower, blue_upper, blue_mask);
  39. cv::inRange(BZdata, red_lower1, red_upper1, red_mask);
  40. //cv::inRange(BZdata, red_lower2, red_upper2, red_mask2);
  41. //腐蚀黄色图像------------------------------
  42. cv::Mat kerneyellow = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(1, 1)); // 可以调整核的大小
  43. cv::erode(yellow_mask, yellow_mask, kerneyellow, cv::Point(-1, -1), 1);
  44. // //膨胀黄色图像
  45. cv::Mat kerneyellow22 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(1, 1)); // 可以调整核的大小
  46. cv::dilate(yellow_mask, yellow_mask, kerneyellow22, cv::Point(-1, -1), 1);
  47. CAR_STOP_FLAG = CAR_STOP(yellow_mask);//看到黄色后停车 要改一下找点的范围,不然会越界---------------------------------------------------------
  48. // //腐蚀蓝色和红色
  49. cv::Mat kernel3 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(2, 2)); // 可以调整核的大小
  50. cv::erode(blue_mask, blue_mask, kernel3, cv::Point(-1, -1), 1);
  51. cv::erode(blue_mask, blue_mask, kernel3, cv::Point(-1, -1), 1);
  52. // // 膨胀蓝色和红色
  53. cv::Mat kernel2 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(1,1));
  54. cv::dilate(blue_mask, blue_mask, kernel2, cv::Point(-1, -1), 1);
  55. cv::dilate(blue_mask, blue_mask, kernel2, cv::Point(-1, -1), 1);
  56. //发车识别
  57. UI_music = CAR_FaChe(red_mask);
  58. //
  59. if(BZ_Imageflag == 1) cv::imshow("red_mask", red_mask);
  60. if(yellow_Imageflag == 1) cv::imshow("yellow_mask", yellow_mask);
  61. if(blue_Imageflag == 1) cv::imshow("blue_mask", blue_mask);
  62. if(TIME_BIZHANG == 0) return 0;//跳过避障
  63. // 寻找蓝色色块的轮廓
  64. std::vector<std::vector<cv::Point>> red_contours;
  65. cv::findContours(blue_mask, red_contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
  66. // 找到最大的红色色块
  67. double max_area = 0;
  68. int max_area_index = -1;
  69. double second_max_area = 0;
  70. int second_max_area_index = -1;
  71. for (int i = 0; i < red_contours.size(); i++)
  72. {
  73. double area = cv::contourArea(red_contours[i]);
  74. //
  75. if (area > max_area)
  76. {
  77. second_max_area = max_area;
  78. second_max_area_index = max_area_index;
  79. max_area = area;
  80. max_area_index = i;
  81. }
  82. else if(area > second_max_area)
  83. {
  84. second_max_area_index = i;
  85. second_max_area = area;//如果当前区域小于最大区域但是大于第二大区域
  86. }
  87. }
  88. if(BZ_Imageflag == 2) cv::imshow("标记红色色块", BZdata);
  89. // 标记最大的红色色块并打印中心点坐标
  90. if (max_area_index >= 0)
  91. {
  92. // cv::drawContours(BZdata, red_contours, max_area_index, cv::Scalar(0, 0, 255), 2);
  93. // 获取最大红色色块的边界框
  94. cv::Rect bounding_rect = cv::boundingRect(red_contours[max_area_index]);//最大
  95. // 计算最大红色色块的中心点坐标
  96. int center_x = (bounding_rect.x + bounding_rect.width / 2)/2;
  97. int center_y = (bounding_rect.y + bounding_rect.height / 2)/2;
  98. // 打印中心点坐标
  99. //std::cout << "最大红色色块的中心点坐标: (" << center_x << ", " << center_y << ")" <<"像素点数目"<<max_area<< std::endl;//
  100. if(max_area <= 2) return 0;//障碍物大小
  101. find_XYdata[0] = center_x;
  102. find_XYdata[1] = center_y;
  103. // cv::imshow("标记红色色块", BZdata);
  104. if(second_max_area_index >= 0)//第二个
  105. {
  106. cv::Rect bounding_rect = cv::boundingRect(red_contours[second_max_area_index]);//第二大
  107. // 计算最大红色色块的中心点坐标
  108. int center_x = (bounding_rect.x + bounding_rect.width / 2)/2;
  109. int center_y = (bounding_rect.y + bounding_rect.height / 2)/2;
  110. // 打印中心点坐标
  111. // std::cout << "第二大红色色块的中心点坐标: (" << center_x << ", " << center_y << ")" <<"像素点数目"<<max_area<< std::endl;
  112. if(second_max_area <= 2) return 0;//障碍物大小
  113. find_XYdata_second[0] = center_x;
  114. find_XYdata_second[1] = center_y;
  115. }
  116. return 1;
  117. }
  118. // 展示标记后的图像
  119. return 0;
  120. }
  121. void bizhangBuxian(int data_X, int data_Y, int bizhang_fangxiang)//0 向左避障 1向右避障
  122. {
  123. if (bizhang_fangxiang == 0)//左避障
  124. {
  125. for (int i = ROW-1; i >= 9; i -= 2)
  126. {
  127. Right_Add_Line[i] = data_X - Bizhang_line_move;
  128. }
  129. }
  130. else if (bizhang_fangxiang == 1)//右避障
  131. {
  132. for (int i = ROW-1; i >= 9; i -= 2)
  133. {
  134. Left_Add_Line[i] = data_X + Bizhang_line_move + 10;
  135. }
  136. }
  137. }
  138. int BZ_PANDUAN_2(void)//判断障碍物是否在赛道内
  139. {
  140. int flagd = 0;
  141. int Y = 0;
  142. if(find_XYdata[1] <=10 && find_XYdata_second[1] <=10) return 0;
  143. for (int i = ROW-1; i >= 9; i -= 2)
  144. {
  145. if (find_XYdata[1] >= i)//找到障碍点所在行
  146. {
  147. Y = i;
  148. break;
  149. }
  150. if (i <= 11) {flagd = 1;}//不认为在.试试2号大
  151. }
  152. if(flagd == 1)
  153. {
  154. for (int i = ROW-1; i >= 9; i -= 2)
  155. {
  156. if (find_XYdata_second[1] >= i)//找到障碍点所在行
  157. {
  158. Y = i;
  159. flagd = 2;
  160. break;
  161. }
  162. if (i <= 11) {return 0;}//不认为在
  163. }
  164. }
  165. if(flagd == 0)
  166. {
  167. if (find_XYdata[0] <= Left_Add_Line[Y] -25 || find_XYdata[0] >= Right_Add_Line[Y] + 25 )//判断是否在赛道内
  168. {
  169. flagd = 2;
  170. }
  171. else return 1;
  172. }
  173. if(flagd == 2)
  174. {
  175. for (int i = ROW-1; i >= 9; i -= 2)
  176. {
  177. if (find_XYdata_second[1] >= i)//找到障碍点所在行
  178. {
  179. Y = i;
  180. break;
  181. }
  182. if (i <= 11) {return 0;}//不认为在
  183. }
  184. }
  185. if(flagd == 2) //2号大
  186. {
  187. if (find_XYdata_second[0] <= find_XYdata_second[Y] -25 || find_XYdata_second[0] >= find_XYdata_second[Y] + 25 )//判断是否在赛道内
  188. {
  189. return 0;
  190. }
  191. else
  192. {
  193. find_XYdata[0] = find_XYdata_second[0];
  194. return 1;
  195. }
  196. }
  197. return 0;
  198. }
  199. void BZ_LuoJISET(void)//避障逻辑
  200. {
  201. static int zhangai_flag1 = 0;
  202. static int zhangai_flag22 = 1;
  203. static int zhangai_Left_or_Right = 0;
  204. static int nums = 0;
  205. if (find_XYdata[1] > 85 && zhangai_flag1 == 0)//在图像下方出现
  206. {
  207. nums++;
  208. if(nums >= 1)//1帧
  209. {
  210. nums = 0;
  211. zhangai_flag1 = 1;
  212. }
  213. printQ("下方下方下方下方下方下方下方下方", 1);
  214. }
  215. if (zhangai_flag1 == 1)//又出现在上方
  216. {
  217. if (find_XYdata[1] <= 75)
  218. {
  219. printQ("上方上方上方上方上方", 1);
  220. nums++;
  221. if (nums >= 1)//3帧
  222. {
  223. nums = 0;
  224. zhangai_flag1 = 2;
  225. }
  226. }
  227. }
  228. if (zhangai_flag1 == 2)//切换
  229. {
  230. zhangai_flag22++;
  231. if (zhangai_flag22 >= 2) zhangai_flag22 = 0;
  232. zhangai_flag1 = 0;
  233. }
  234. if (zhangai_flag22 == 0) { printQ("左避障", 0); zhangai_Left_or_Right = 0; }
  235. if (zhangai_flag22 == 1) { printQ("右避障", 1); zhangai_Left_or_Right = 1; }
  236. //--------------
  237. bizhangBuxian(find_XYdata[0], find_XYdata[1], zhangai_Left_or_Right);//避障改线
  238. Mid_Line_Repair();
  239. }

 

 

video_20231210_201610_edit

2.3.其他

发车识别赛道颜色,停车识别黄线,没啥说的,两个一毛一样。

  1. int CAR_STOP(cv::Mat yellow_Find_data) //320 * 96
  2. {
  3. //std::cout << "yellow_Find_data: " << yellow_Find_data.size() << std::endl;
  4. int yellowPoint_num = 0;
  5. for(int i = 95;i>= 95-70;i-=1)
  6. {
  7. for(int j = 319-30;j>=30;j-=2)
  8. {
  9. if(yellow_Find_data.at<uchar>(i, j) > 100)
  10. {
  11. yellowPoint_num++;
  12. }
  13. }
  14. }
  15. if(0)
  16. {
  17. printQ("黄点数目",yellowPoint_num);
  18. cv::Point pA(10, 169);//记录点
  19. cv::Point pB(10, 100);//记录点
  20. //描点画线
  21. cv::circle(yellow_Find_data, pA, 8, 255);
  22. cv::circle(yellow_Find_data, pB, 8, 255);
  23. cv::imshow("contour_img", yellow_Find_data);//障碍物
  24. }
  25. //printQ("黄色数目",yellowPoint_num);
  26. if(yellowPoint_num >= 200)
  27. {
  28. return 1;
  29. }
  30. return 0;
  31. }
  32. int CAR_FaChe(cv::Mat FaChe_data)//发车
  33. {
  34. static int flag_1 = 0;
  35. static int numstc = 0;
  36. int Red_Point_num = 0;
  37. if(flag_1 == 1) return 0;
  38. for(int i = 95;i>= 95-70;i-=2)
  39. {
  40. for(int j = 319-30;j>=30;j-=2)
  41. {
  42. if(FaChe_data.at<uchar>(i, j) > 100)
  43. {
  44. Red_Point_num++;
  45. }
  46. }
  47. }
  48. printQ("红点数目",Red_Point_num);
  49. if(Red_Point_num >= 200) numstc++;
  50. else numstc = 0;
  51. if(numstc >= 20){ flag_1 = 1;return 0;}
  52. else return 1;
  53. }

尾言

大道至简,代码和我有一个能跑就行

最后感谢安徽工程大学电子科技协会,感谢每一位指导老师。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号