I have generated a map named map_1.pgm using map_server package. I want to display it in an OpenCV window. How can I achieve this?

I am currently trying this -

std::string file_name = "map_1.pgm"; 
cv::Mat m = cv::imread(file_name.c_str(), 0);

cv::imshow("map_one_window", m);


This is my map_1.yaml

image: map_1.pgm
resolution: 0.050000
origin: [-100.000000, -100.000000, 0.000000]
negate: 0
occupied_thresh: 0.65
free_thresh: 0.196

However, on doing the above steps, I get a big OpenCV window which is completely grey.

How do I solve this problem?

I am attaching the results of xdg-open map_1.pgm. enter image description here


1 Answer 1


I am not familiar with ROS maps, but the threshold values in the yaml seem to indicate your map values are floats in the [0,1] range. I think OpenCV will not properly visualize these.

Check the minimum/maximum values in your matrix.

Try to generate a uint8 image with values in the [0,255] range for display, by just multiplying your map values with 255 (you may even have to make it a 3-channel image [with 3 identical channels] for OpenCV to handle it properly).

  • $\begingroup$ I didn't get the last paragraph of your answer - 'Try to generate .......... 255'. The dimensions of m are 4000 * 4000. I also checked the maximum and minimum value of elements in m. The maximum value is 254 and minimum value is 0. $\endgroup$
    – skpro19
    Commented Mar 8, 2021 at 9:43
  • $\begingroup$ imshow works for me for a 1-channel uint8 matrix with values in [0,255] range. grasping at straws.. check the type of m? stackoverflow.com/questions/10167534/… other than that, if the actual matrix content is correct, I'm out of ideas. $\endgroup$ Commented Mar 8, 2021 at 10:27
  • $\begingroup$ related to the "content is correct".. I trust you checked the map image in a standard image viewer, right? $\endgroup$ Commented Mar 8, 2021 at 10:32
  • $\begingroup$ I am updating the post with the results of xdg-open map_1.pgm. The type of m is '0' or 'CV_8U'. $\endgroup$
    – skpro19
    Commented Mar 8, 2021 at 14:56
  • $\begingroup$ When I resize both the dimensions of 'm' using the cv::resize function, I am able to see the white map surrounded by the grey space. I think, the image was always being displayed correctly. However, due to the large grey space surrounding the white map, I was not able to see the map. Reducing the size of m helped me to see the image. $\endgroup$
    – skpro19
    Commented Mar 8, 2021 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.