grid_map icon indicating copy to clipboard operation
grid_map copied to clipboard

[grid_map_cv] Conversion to OpenCV force NaN to 0

Open romainreignier opened this issue 7 years ago • 8 comments

When converting a GridMap to an OpenCV image with the method grid_map:GridMapCvConverter::toImage, the cv::Mat is initialized with zeros (black pixels) (here) then, only finite values are copied from the GridMap to the cv::Mat (here).

In other words the NaN values are automatically translated into black pixels.

For my use case (processing a map during a SLAM), I have a lot of NaN values and I need that information in OpenCV. So maybe, setting the value of the pixels to the mid value between lowerValue and upperValue is more interesting than forcing to 0 by default.

romainreignier avatar Feb 22 '17 15:02 romainreignier

Are you using an image with a channel for the alpha layer? There's a round trip unit test that passes for the CV_8UC4 CV image type and I just added a nan-value/transparent pixel here.

If this does not solve your problem, could you please create a PR with a failing unit tests that shows the issue?

pfankhauser avatar Feb 24 '17 14:02 pfankhauser

Ah no, I forgot to mention that I am using a CV_8UC1 grayscale image, that is why, there is no other solution to translate a NaN to a mid value between Free and Occupied.

romainreignier avatar Feb 24 '17 16:02 romainreignier

So is there anything you would like to see differently in the behavior of Grid Map or should I close this issue?

pfankhauser avatar Mar 02 '17 09:03 pfankhauser

What do you think about setting NaN values to the mid value between lowerValue and upperValue?

romainreignier avatar Mar 02 '17 09:03 romainreignier

*if there is no alpha channel, of couse.

romainreignier avatar Mar 02 '17 10:03 romainreignier

I would keep the current behavior, since I feel it's the one with the least assumptions. Your use case is very special, and if I'm not mistaken, you could achieve your desired behavior by setting your empty/nan cells to the mid-value before converting it to the image format.

pfankhauser avatar Mar 02 '17 10:03 pfankhauser

Yes, of course, it is what I have done. But for the map_server for example, a unknown cell is a pixel with value 128 that is why I was surprised that grid_map did not picked a middle value. Anyway, as you said, my use case may be special for the grid_map because I did not managed to find an easy way to convert pixels position to grid_map coordinates then OccupancyGrid coordinates. So I have ended up writing my own conversion methods. Maybe you could add in the doc that the NaN values are converted to the value 0 for images with no alpha layer.

romainreignier avatar Mar 02 '17 10:03 romainreignier

I have also encountered this problem.

The issue is that the output of SLAM algorithms for unknown area is -1 in occupancy grids, that gets transformed into NaN in Grid maps and finally that gets transformed into 0 in cv::Mat. That causes cv::Mats to make no difference between occupied cells and unknown cells.

I don't think changing NaN cells to a middle value before converting to grid map is a good solution because an occupancy grid is supposed to give a probability of occupancy of each cell and is not supposed to give a value to unknown space.

juanluishortelano avatar Jun 20 '17 10:06 juanluishortelano