A*算法实现路径规划后路径的显示错误总结

本文详细介绍了一种基于A*算法的路径规划实现过程,通过使用C++编程语言结合OpenCV库,展示了如何在不同地图数据类型下进行路径寻优,并分析了数据格式对规划结果的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值