Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C++
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;
       }
Posted
Updated 7-Apr-17 0:17am

1 solution

There are two errors in your code:

  1. You are never calling CreateGrid() (Grid::grid is empty)
  2. The Grid::grid vectors are never allocated

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();
}

To fix the second error initialise the Nodes 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);
But it would be better to not use allocated Nodes:
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
 
Share this answer
 
Comments
Member 12157883 7-Apr-17 6:34am    
Thank you, @Jochen Arndt. That sort my problem.
Jochen Arndt 7-Apr-17 6:36am    
Thank you for your feedback and accepting the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900