Astar astar;
Point_stc start(maze.rows-20, maze.cols/2);
Point_stc end(20, maze.cols/2);
maze = maze/255;
list<Point_stc *> path = astar.GetPath(start, end, false, maze);
Point_stc *p = path.front();
list<Point_stc *>::iterator testiterator;
int w = 0;
//int m =0, n = 0;
for(testiterator = path.begin(); testiterator != path.end(); testiterator++)
{
m = (*testiterator)->x;
n = (*testiterator)->y;
cv::Point A = cv::Point(n,m);
cv::circle(maze, A, 0.1, cv::Scalar(255,255,255),0.1);
w++;
}
maze = maze*255;
//*/
cv::namedWindow("PATH");
cv::imshow("PATH",maze);
return result;
main.cpp
#include<iostream>
#include<vector>
#include "Astar.h"
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
//using namespace cv;
using namespace std;
int main()
{
int i = 0, j = 0;
cv::Mat maze = cv::Mat(200, 200, CV_8UC1);
/*
int A[8][12] = {{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1 },
{ 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1 },
{ 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
int m, n;
for(m = 0; m < maze.size(); m++)
{
for(n = 0; n < maze[m].size(); n++)
{
maze[m][n] = A[m][n];
cout << maze[m][n] << " ";
}
cout << "\n";
}
*/
cv::Mat B = cv::Mat(200,200,CV_8UC1);
int map[200][200] = {0};
ifstream infile;
infile.open("data.txt");
for(i = 0; i < 200; i ++)
{
for(j = 0; j < 200; j++)
{
infile >> map[i][j];
maze.at<uchar>(i, j) = map[i][j];//maze的取值为 0 or 1
//
(1 == map[i][j]) ? (map[i][j] = 255) : (map[i][j] = 0);
B.data[i*200+j]=map[i][j]; //B的取值为 0 or 255
//cout << maze;
}
//cout << endl;
}
//cout << endl;
infile.close();
cv::namedWindow("MAP");
cv::imshow("MAP",B);
cv::waitKey();
//cv::Mat B1;
//maze.copyTo(B);
//标出起始点与终点
cv::Point kaishi = cv::Point(15,120);
cv::Point jieshu = cv::Point(180,110);
cv::circle(B, kaishi, 3, cv::Scalar(255,255,255),1);//白色
cv::circle(B, jieshu, 3, cv::Scalar(255,255,255),1);
cv::imshow("MAP",B);
cv::waitKey();
//cout << " start = " << map[120][15] << endl;
//cout << " end = " << map[110][180] << endl;
Astar astar;
// astar.InitAstar(maze);
//ÉèÖÃÆðÊŒºÍœáÊøµã
Point_stc start(120, 15);
Point_stc end(110, 180);
map[15][120] = 2;
map[180][110] = 2;
//A*Ëã·šÕÒѰ·Ÿ¶
list<Point_stc *> path = astar.GetPath(start, end, false, maze);
//ŽòÓ¡
Point_stc *p = path.front();
list<Point_stc *>::iterator testiterator;
i = 0;
int m =0, n = 0;
for(testiterator = path.begin(); testiterator != path.end(); testiterator++)
{
m = (*testiterator)->x;
n = (*testiterator)->y;
cv::Point A = cv::Point(n,m);
cv::circle(B, A, 0.1, cv::Scalar(255,255,255),0.1);
cout << "line :" << i <<" "<<"("<< (*testiterator)->x << ", " << (*testiterator)->y << ")" << endl;
map[m][n] = 2;
i++;
}
cv::namedWindow("PATH");
cv::imshow("PATH",B);
cv::waitKey();
for(i = 0; i < 200; i ++)
{
for(j = 0; j < 200; j++)
{
//cout << map[i][j];
}
//cout << endl;
}
//cout << endl;
//system("pause");
//getchar();
return 0;
}
路径规划程序中函数list<Point_stc *> path = astar.GetPath(start, end, false, maze); 函数中的参数 maze 我改写后传的是 cv::Mat 类型, 传入maze的结果显示规划出来的路径是正确的
当用cv::Mat B传地图进来后结果是错误的
原因总结:maze中的值是 0 or 1 ,而 B 中的值是 0 or 255,因此要路径规划的话最好把Mat中的值像最开始那样处理到 0 or 1