Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an image into an Occupancy Grid

Tags:

c++

opencv

I've just started to learn OpenCV and I'm wondering how can I convert an image like this one:

enter image description here

Into an Occupancy Grid, like this one:

int grid[ROW][COL] = 
    { 
        { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, 
        { 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 }, 
        { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 }, 
        { 0, 0, 1, 0, 1, 0, 0, 0, 0, 1 }, 
        { 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 }, 
        { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }, 
        { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 }, 
        { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, 
        { 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 } 
    }; 

1: cell is not blocked (white pixel).
0: cell is blocked (black pixel).

I'm not going to use exactly that picture. I'm going to use a picture with only walls: no texts, no furniture, no windows and no door's symbols. There will be only walls with "holes" to show the doors.

I want to read the image and return 1 when the pixel is white, and 0 when the pixel is black. Only that.

How can I do that with OpenCV?

I will store that matrix into a text file, but I know how to do that.

Don't worry about what I'm going to do with that matrix. I'm not asking that.

like image 976
VansFannel Avatar asked Oct 15 '22 14:10

VansFannel


1 Answers

Mat in OpenCV like the grid you mentioned. .pgm is the format used in the Robot Operating System for storing Occupancy Grid maps. Any image format is fine for representation.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{   
   //read input image as gray
   Mat image_gray = imread("input.jpg", CV_LOAD_IMAGE_GRAYSCALE);   

   // convert gray image to binary image 
   // After threshold, all values are either (0 or 200)
   Mat imgage_bw;
   cv::threshold(image_gray, imgage_bw, 200, 255.0, THRESH_BINARY);

   // if you really want images with 0 for blocked cell and 1 for free cell
    Mat image_grid = imgage_bw/255;  

    // save to disk
    imwrite("output.pgm", image_grid);

//write result to text file
FileStorage file("ouput.yaml", cv::FileStorage::WRITE);    
file <<"grid " <<image_grid;
file.release(); 

    return 0;
}

ouput.yaml

%YAML:1.0
grid : !!opencv-matrix
   rows: 400
   cols: 800
   dt: u
   data: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ....]
like image 100
nayab Avatar answered Oct 20 '22 23:10

nayab