使用向量< vector< node>创建opencv mat到2d网格> [英] Creating opencv mat to 2d grid using vector<vector<node> >
问题描述
我正在构建一个使用OpenCV Mat的搜索算法,我将Mat转换为灰色图像,然后检查像素以使其坐标可行走或不可行走。我使用2D矢量来创建节点网格。 Node是一个包含像素信息的类。当我尝试从网格中打印nodeID时,程序突然关闭(例如
I am building a search algorithm using OpenCV Mat where I convert the Mat to gray image and then check the pixel to sign it to walkable or not walkable with their coordinate. I used a 2D vector to create a grid of node. The Node is a class to contain information about the pixel. when I try to print the nodeID from the grid the program sudden shutdown (e.g
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("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;
}
谢谢。
我尝试了什么:
我尝试使用迭代,如下所示。
Thank you.
What I have tried:
I try to use to use iterating as below this print nothing.
for(auto vec : grid)
{
for(auto x : vec)
std::cout<<x->NodeID<<" , ";
std::cout << std::endl;
}
推荐答案
您的代码中有两个错误:
There are two errors in your code:
- 你永远不会打电话给
CreateGrid()
(Grid :: grid
是空的)
- 从未分配
Grid :: grid
向量
修复构造函数中的第一个错误调用 CreateGrid()
:
To fix the first error call CreateGrid()
in the constructor:
Grid(cv::Mat _map){
map = _map;
gridx = map.cols;
gridy = map.cols;
gridZise = map.cols;
CreateGrid();
}
要修复第二个错误,请在 CreateGrid中初始化
:节点
()
To fix the second error initialise the Node
s in CreateGrid()
:
//grid[i][j]->walkable = _walkable;
//grid[i][j]->gridX = i;
//grid[i][j]->gridY = j;
grid[i][j] = new Node(i, j, _walkable);
但最好不要使用已分配的节点
s:
But it would be better to not use allocated Node
s:
vector<vector<Node> > grid;
// ...
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;
}
}
}
// ...
// Change other affected code too
这篇关于使用向量< vector< node>创建opencv mat到2d网格>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!