#include"ros/ros.h" #include "nav_msgs/OccupancyGrid.h" #include "std_msgs/String.h" #include #include #include void callback(const nav_msgs::OccupancyGrid::ConstPtr& mapmsg) { int height,width,k=0; cv::Mat map(2000,2000,CV_8UC1); height = mapmsg->info.height; width = mapmsg->info.width; std::vector compression_params; compression_params.push_back(cv::IMWRITE_PNG_COMPRESSION); compression_params.push_back(0); for (int i = 0; i < 2000; i++) { for(int j = 0; j < 2000; j++) { map.at(i,j) = mapmsg->data[k]; k++; } } cv::imshow("map",map); if (cv::waitKey(1)==27) { cv::imwrite("map.png", map, compression_params); exit(0); } } int main(int argc, char *argv[]) { //init_keyboard(); setlocale(LC_ALL,""); ros::init(argc,argv,"listener_map"); ros::NodeHandle nh; ros::Subscriber map_sub = nh.subscribe("map",1,callback); ros::spin(); return 0; }