created by Dejavu
[完结]
目的
- 这里我们试图将路径规划的起点物体,终点靶目标,障碍物抽象成基本图形,分别为圆形和方形,这样为我们测试路径规划算法提供原始可视的目标数据
-
如下图所示,这里我们设定红色为起点物体,绿色为终点靶目标,灰色为障碍物
c++ opencv 目标分离
-
用到的头文件 a_star.h,这里外部调用时函数只需调用Obj all_obj(Mat src,Mat &take_look)这个即可
#include "a_star.h" Pt center_pt(Mat src) { Pt pt; int r; bool find(false); for (int i = 0;i < src.cols;i++) { for (int j = 0;j < src.rows;j++) { if (src.at
(j, i) >= 200) { pt.pt.y = j; r = i; find = true; break; } } if (find) break; } find = false; for (int i = 0;i < src.rows;i++) { for (int j = 0;j < src.cols;j++) { if (src.at (i, j) >= 200) { pt.pt.x = j; r = j - r; find = true; break; } } if (find) break; } pt.r = r; return pt; } bool foundFromGrid(Point index, Point tar, int r, Point &pt) { Point tmp = Point(index.x*r, index.y*r); if (tmp.x <= tar.x && tmp.y <= tar.y && tmp.x + r >= tar.x && tmp.y + r >= tar.y) { pt = index; return true; } return false; } GridMapInfo map2grid_map(Mat map, int _r, Point tar, Point dest,Mat &take_look) { GridMapInfo info; bool isFoundT(false), isFoundD(false); Mat grid_map(map); int r = _r * 2; int n = map.rows / r; int m = map.cols / r; for (int y = 0;y < n;y++) { for (int x = 0;x < m;x++) { bool isBlock(false); for (int i = 0;i < r;i++) { for (int j = 0;j < r;j++) { if (grid_map.at (y*r + i, x*r + j) < 100) { isBlock = true; break; } } } info.map[x][y].init(); info.map[x][y].isWall = isBlock; info.map[x][y].index = Point(x, y); info.map[x][y].pt = Point(x*r, y*r); info.map[x][y].center = Point(x*r + r / 2, y*r + r / 2); info.map[x][y].addNeighbors(); if (isBlock) { for (int i = 0;i < r;i++) { for (int j = 0;j < r;j++) { if (grid_map.at (y*r + i, x*r + j) < 100) continue; else grid_map.at (y*r + i, x*r + j) = 0; } } } if (!isFoundT) isFoundT = foundFromGrid(Point(x, y), tar, r, info.target); if (!isFoundD) isFoundD = foundFromGrid(Point(x, y), dest, r, info.dest); rectangle(grid_map, Rect(x*r, y*r, r, r), Scalar::all(150), 5); rectangle(take_look, Rect(x*r, y*r, r, r), Scalar::all(150), 5); } } info.obj_img = grid_map; if(DUBUG) imshow("obj", grid_map); return info; } Obj all_obj(Mat src,Mat &take_look) { Obj obj; vector ch(3); Mat element = getStructuringElement(MORPH_RECT, Size(4, 4)); split(src, ch); Mat b, g, r; b = ch.at(0); g = ch.at(1); r = ch.at(2); Mat block_img; Mat obj_src; addWeighted(b, 0.5, g, 0.5, 0, block_img); addWeighted(r, 0.5, block_img, 0.5, 0, block_img); Mat bg; threshold(block_img, bg, 250, 255, 1); threshold(block_img, block_img, 150, 255, 0); addWeighted(block_img, 0.5, bg, 0.5, 0, block_img); threshold(block_img, block_img, 250, 255, 1); blur(block_img, block_img, Size(7, 7)); threshold(block_img, block_img, 50, 255, 0); obj_src = block_img.clone(); erode(block_img, block_img, element); if(DUBUG) imshow("block", block_img); threshold(b, b, 10, 255, 1); threshold(g, g, 10, 255, 1); threshold(r, r, 10, 255, 1); Mat target_img, dest_img; addWeighted(b, 0.5, g, 0.5, 0, target_img); threshold(target_img, target_img, 200, 255, 0); addWeighted(r, 0.5, b, 0.5, 0, dest_img); threshold(dest_img, dest_img, 200, 255, 0); if(DUBUG) { imshow("target", target_img); imshow("dest", dest_img); } if(DUBUG) imshow("obj_src", obj_src); obj.obj_src = obj_src; obj.target = center_pt(target_img); obj.dest = center_pt(dest_img); obj.info = map2grid_map(block_img, obj.target.r, obj.target.pt, obj.dest.pt,take_look); return obj; }