ROS各消息类型的data理解

1.sensor_msgs::Image

  • data按rgbrgb…的方式保存图像
  • 将保存在txt中的data用opencv显示
#include 
#include 
#include 

using namespace std;

int data[3 * 640 * 480];

void array2mat(const std::vector data, int width, int height)
{
  cv::Mat src = cv::Mat::zeros(height, width, CV_8UC3);
  for (int i = 0; i < height; ++i) 
  {
    for (int j = 0; j < width; j++)
    {
      src.at(i, j)[0] = data[(width * i + j)*3+2];
      src.at(i, j)[1] = data[(width * i + j)*3+1];
      src.at(i, j)[2] = data[(width * i + j)*3];
      //cout << (int)src.at(i, j)[0] << endl;
      //cout << (int)src.at(i, j)[1] << endl;
      //cout << (int)src.at(i, j)[2] << endl;
    }
  }
  cv::imshow("img", src);
  cv::waitKey(10);
}

void CallBack(const sensor_msgs::Image::ConstPtr& img) {
  printf("%d  %d\n", img->height, img->width);
  array2mat(img->data, img->width, img->height);
}

你可能感兴趣的:(updating)