sensor_msgs::Image转换成深度图,或直接获取距离

1.使用cv_bridge

#include <cv_bridge/cv_bridge.h>
void CB(const sensor_msgs::ImageConstPtr& depth) {
  cv_bridge::CvImageConstPtr depth_ptr;
  depth_ptr =
      cv_bridge::toCvShare(depth, sensor_msgs::image_encodings::TYPE_32FC1);
  cv::imshow("res", depth_ptr->image);
  cv::waitKey(1);
}
  • 显示的图像不对,试了好几个image_encodings都不行

2.直接获取距离

  • 属性
    • height: 480
    • width: 640
    • encoding: “16UC1”
    • is_bigendian: 0
typedef union USHORT_UNION {
  ushort num;
  char c[2];
} ushort_union;

ushort Little(char c0, char c1) {
  ushort_union tmp;
  tmp.c[0] = c0;
  tmp.c[1] = c1;
  return tmp.num;
}

void cb(const sensor_msgs::Image::ConstPtr& depth) {
  int x = 320, y = 240;
  int index = y * depth->width + x;
  ushort tmp = Little(depth->data[index*2], depth->data[index*2+1]);//单位mm
  // 也可以转换成深度图
  cv::Mat dep_im = cv::Mat::zeros(depth->height, depth->width, CV_8UC3);
  for (int i = 0; i < depth->height; ++i) {
    for (int j = 0; j < depth->width; j++) {
      int index = i * depth->width + j;
      ushort tmp = Little(depth->data[index * 2], depth->data[index * 2 + 1]);
      if (tmp > 4000)
        tmp = 4000;
      dep_im.at<cv::Vec3b>(i, j)[0] = tmp * 255 / 4000;
      dep_im.at<cv::Vec3b>(i, j)[1] = tmp * 255 / 4000;
      dep_im.at<cv::Vec3b>(i, j)[2] = tmp * 255 / 4000;
    }
  }
}
  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刀么克瑟拉莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值