我正在构建一个使用OpenCV Mat的搜索算法,我将Mat转换为灰色图像,然后检查像素以使其坐标可行走或不可行走。我用vector>网格。当我尝试从网格中打印nodeID时,程序突然关闭(例如grid.grid[10][10]->NodeID
)。
using namespace std;
int gridZise;
class location{
public:
int x;
int y;
};
class Node{
public:
int gridX;
int gridY;
bool walkable;
location worldPosition;
int NodeID;
int gCost;
int hCost;
Node *parent;
Node(bool _walkable, int _gridX, int _gridY)
{
walkable = _walkable;
gridX = _gridX;
gridY = _gridY;
NodeID = gridY * gridZise + gridX;
}
Node(int _gridX, int _gridY){
gridX = _gridX;
gridY = _gridY;
NodeID = gridY * gridZise + gridX;
}
int fCost(){
return gCost + hCost;
}
};
class Grid{
public:
cv::Mat map;
vector<vector<Node*> > grid;
int gridx;
int gridy;
Grid(cv::Mat _map){
map = _map;
gridx = map.cols;
gridy = map.cols;
gridZise = map.cols;
}
void CreateGrid(){
// Set up sizes. (HEIGHT x WIDTH)
grid.resize(gridy);
for (int i = 0; i < gridy; ++i)
grid[i].resize(gridx);
// build up the grid
for(int i=0; i <gridx;i++){
for(int j=0; j < gridy;j++){
int pixel_val = map.at<int>(i,j);
bool _walkable = false;
if(pixel_val > 120){//if the value of the pixel is bigger than 120 is walkable
_walkable = true;
}
grid[i][j]->walkable = _walkable;
grid[i][j]->gridX = i;
grid[i][j]->gridY = j;
}
}
}
void PrintGrid(){
for(int i=0; i <gridx;i++){
for(int j=0; j < gridy;j++){
cout << grid[i][j]->NodeID <<endl;
}
}
}
vector<Node> GetNeighbours(Node node)
{
vector<Node> neighbours;
for (int x = -1; x <=1; x++)
{
for (int y = -1; y <= 1; y++)
{
if (x == 0 && y == 0)
continue;
int checkX = node.gridX + x;
int checkY = node.gridY + y;
if(checkX >=0 && checkX < gridx && checkY >=0 && checkY < gridy)
{
Node neighbour(checkX,checkY);
neighbours.push_back(neighbour);
}
}
}
return neighbours;
}
Node nodeFromLocation(location _node){
Node currentNode = *grid[_node.x][_node.y];
return currentNode;
}
};
using namespace cv;
int main()
{
cv::Mat img;
img = imread("C:\\Users\\abdulla\\Pictures\\maze.jpg");
if(img.empty()){
cout<<"image not load "<<endl;
return -1;
}
cvtColor(img,img,CV_BGR2GRAY);
imshow("image",img);
waitKey();
Grid grid(img);
grid.PrintGrid();
return 0;
}
谢谢。
答案 0 :(得分:0)
首先,摆脱using namespace std;
。虽然它看起来很方便,但您可能会遇到一些令人讨厌的惊喜(请参阅this问题)。
Grid
的构造函数使用Grid::grid
的默认构造函数,它创建一个空向量。
在PrintGrid
中,您会得到未定义的行为。
grid; // ok, grid is a member of Grid
grid[0]; // Returns a reference. No bounds checking is performed.
grid[0][0]; // undefined behaviour. grid[0] is outside of bounds.
方法CreateGrid
与此无关,因为您从不称呼它。但是我们假设你已经打过电话了。那么PrintGrid会像这样工作:
grid; // ok, grid is a member of Grid
grid[0]; // ok
grid[0][0]; // ok, return a pointer to a Node. Which you never initialized.
grid[0][0]->NodeID; // Undefined behaviour. You're trying to access a random memory location.
你真的需要将节点存储为指针吗?你也可以使用vector<vector<Node>>
。这样,某人(std::vector<...
)将负责分配和删除节点。您仍然可以使用指针指向父节点,只要指针用作引用而不是所有权,那就没问题。
如果您确实需要存储指针,make use of smart pointers。这样,有人会负责删除节点。
最后,班级Grid
负责维持适当的状态。总是。所以构造函数应该已经完成了CreateGrid
所做的事情。问题是你不能从构造函数中调用CreateGrid
,因为那时你将调用一个生命尚未开始的对象的方法(如果我错了那个人就会纠正我)。
如果你想把它作为一个单独的函数,你可以使CreateGrid
静态:
class Grid {
Grid(cv::Mat _map):
map(_map),
gridx(_map.cols),
gridy(_map.cols),
gridZise(_map.cols),
{
GreateGrid(grid, map, gridx, gridy);
}
//...
static void CreateGrid(std::vector<std::vector<Node>> & grid, cv::Mat map, int gridx, int grid y) {
//...
}
//...
};