RGB和深度图像获取最终版

此处在原博客基础上进行了改正,调整了深度图像的归一化处理方法,最终显示减少了空洞

#include <opencv2\opencv.hpp> 

#include<iostream>
#include <Windows.h>
#include "NuiApi.h"
#include<cv.h>
#include <d3d11.h>


using namespace std;
using namespace cv;
   
  
//最远距离(mm)
const int MAX_DISTANCE = 3500;
//最近距离(mm)
const int MIN_DISTANCE = 200;
  
const LONG m_depthWidth = 640;
const LONG m_depthHeight = 480;
const LONG m_colorWidth = 640;
const LONG m_colorHeight = 480;
const LONG cBytesPerPixel = 4;   //像素位数
//计算内存大小
  
int main()
{
  //彩色图像
  Mat image_rgb;
  //深度图像
  Mat image_depth;
  
  //创建一个MAT
  image_rgb.create(480,640,CV_8UC3);
  image_depth.create(480,640,CV_8UC1);
  
  //一个KINECT实例指针
  INuiSensor* m_pNuiSensor = NULL;
  
  if (m_pNuiSensor != NULL)
  {
    return 0;
  }
  
  //记录当前连接KINECT的数量(为多连接做准备)
  int iSensorCount;
  //获得当前KINECT的数量
  HRESULT hr = NuiGetSensorCount(&iSensorCount);
  
  
  //按照序列初始化KINETC实例,这里就连接了一个KINECT,所以没有用到循环
  hr = NuiCreateSensorByIndex(iSensorCount - 1, &m_pNuiSensor);
  //初始化,让其可以接收彩色和深度数据流
  hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH);
  
  //判断是否出错
  if (FAILED(hr))
  {
    cout<<"NuiInitialize failed"<<endl;
    return hr;
  }
  
  //彩色图像获取下一帧事件
  HANDLE nextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  //彩色图像事件句柄
  HANDLE colorStreamHandle = NULL;
  //深度图像获取下一帧事件
  HANDLE nextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  //深度图像事件句柄
  HANDLE depthStreamHandle = NULL;
  
  //实例打开数据流,这里NUI_IMAGE_TYPE_COLOR表示彩色图像
  hr = m_pNuiSensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0,2,nextColorFrameEvent,&colorStreamHandle);
  
  if( FAILED( hr ) )//判断是否提取正确
  {
    cout<<"Could not open color image stream video"<<endl;
    m_pNuiSensor->NuiShutdown();
    return hr;
  }
  
  //实例打开数据流,这里NUI_IMAGE_TYPE_DEPTH表示深度图像
  hr = m_pNuiSensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480, 0,2, nextDepthFrameEvent, &depthStreamHandle);
  
  if( FAILED( hr ) )//判断是否提取正确
  {
    cout<<"Could not open color image stream video"<<endl;
    m_pNuiSensor->NuiShutdown();
    return hr;
  }
  
  
  
  cv::namedWindow("depth",1);
  moveWindow("depth",300,600);
  cv::namedWindow("colorImage",1);
  moveWindow("colorImage",0,200);
  
  while (1)
  {
    NUI_IMAGE_FRAME pImageFrame_rgb;
    NUI_IMAGE_FRAME pImageFrame_depth;
  
    //无限等待新的彩色数据,等到后返回
    if (WaitForSingleObject(nextColorFrameEvent, 0) == 0)
    {
      //从刚才打开数据流的流句柄中得到该帧数据,读取到的数据地址存于pImageFrame
      hr = m_pNuiSensor->NuiImageStreamGetNextFrame(colorStreamHandle, 0, &pImageFrame_rgb);
      if (FAILED(hr))
      {
        cout<<"Could not get color image"<<endl;
        m_pNuiSensor->NuiShutdown();
        return -1;
      }
  
  INuiFrameTexture *pTexture = pImageFrame_rgb.pFrameTexture;
  NUI_LOCKED_RECT lockedRect;
  
  //提取数据帧到LockedRect,它包括两个数据对象:pitch每行字节数,pBits第一个字节地址
  //并锁定数据,这样当我们读数据的时候,kinect就不会去修改它
  
  
  pTexture->LockRect(0, &lockedRect, NULL, 0);
      //确认获得的数据是否有效
    if (lockedRect.Pitch != 0)
    {
     //将数据转换为OpenCV的Mat格式
    for (int i = 0; i < image_rgb.rows; i++)
    {
          //第i行的指针
      uchar *prt = image_rgb.ptr(i);
  
          //每个字节代表一个颜色信息,直接使用uchar
      uchar *pBuffer = (uchar*)(lockedRect.pBits) + i * lockedRect.Pitch;
  
      for (int j = 0; j < image_rgb.cols; j++)
       {  
            prt[3 * j] = pBuffer[4 * j];//内部数据是4个字节,0-1-2是BGR,第4个现在未使用
            prt[3 * j + 1] = pBuffer[4 * j + 1];
            prt[3 * j + 2] = pBuffer[4 * j + 2];
       }
    }
       // Canny(image_rgb,image_rgb,0,30,3);
        imshow("colorImage",image_rgb);
        //解除锁定
        pTexture->UnlockRect(0);
        //释放帧
        m_pNuiSensor->NuiImageStreamReleaseFrame(colorStreamHandle, &pImageFrame_rgb );
      }
      else
      {
        cout<<"Buffer length of received texture is bogus\r\n"<<endl;
      }
  
      BOOL nearMode;
      INuiFrameTexture* pColorToDepthTexture; 
  
  
      //深度图像的处理
      if (WaitForSingleObject(nextDepthFrameEvent, INFINITE) == 0)
      {
  
        hr = m_pNuiSensor->NuiImageStreamGetNextFrame(depthStreamHandle, 0 , &pImageFrame_depth);
  
        if (FAILED(hr))
        {
          cout<<"Could not get depth image"<<endl;
          NuiShutdown();
          return -1;
        }
  
    hr = m_pNuiSensor->NuiImageFrameGetDepthImagePixelFrameTexture( depthStreamHandle, &pImageFrame_depth, &nearMode, &pColorToDepthTexture); 
    INuiFrameTexture *pTexture = pImageFrame_depth.pFrameTexture;
    NUI_LOCKED_RECT lockedRect;
    NUI_LOCKED_RECT ColorToDepthLockRect; 
  
    pTexture->LockRect(0, &lockedRect, NULL, 0);
    pColorToDepthTexture->LockRect(0,&ColorToDepthLockRect,NULL,0); 
  
        //归一化
    for (int i = 0; i < image_depth.rows; i++)
    {
       uchar *prt = image_depth.ptr<uchar>(i);
  
       uchar* pBuffer = (uchar*)(lockedRect.pBits) + i * lockedRect.Pitch;
          //这里需要转换,因为每个深度数据是2个字节,应将BYTE转成USHORT
       USHORT *pBufferRun = (USHORT*)pBuffer;
  
       for (int j = 0; j < image_depth.cols; j++)
       {
         //先向,将数据归一化处理,对深度距离在300mm-3500mm范围内的像素,映射到【0—255】内,
         //超出范围的,都去做是边缘像素
        prt[j] = 255 - (BYTE)(256*pBufferRun[j]/0x0fff);//直接将数据归一化处理
       }
    }
   Mat out;


bilateralFilter(image_depth,out,25,25*2,25/2);

        imshow("depth", image_depth);
  
  
  
        //接下来是对齐部分,将前景抠出来
  
        //存放深度点的参数
        NUI_DEPTH_IMAGE_POINT* depthPoints = new NUI_DEPTH_IMAGE_POINT[640 * 480];
        if (ColorToDepthLockRect.Pitch != 0) 
        { 
          HRESULT hrState = S_OK; 
            
          //一个能在不同空间坐标转变的类(包括:深度,彩色,骨骼)
          INuiCoordinateMapper* pMapper; 
  
          //设置KINECT实例的空间坐标系
          hrState = m_pNuiSensor->NuiGetCoordinateMapper(&pMapper); 
  
          if (FAILED(hrState)) 
          { 
            return hrState; 
          } 
  
          //重要的一步:从颜色空间映射到深度空间。参数说明:
          //【参数1】:彩色图像的类型
          //【参数2】:彩色图像的分辨率
          //【参数3】:深度图像的分辨率
          //【参数4】:深度图像的个数
          //【参数5】:深度像素点数
          //【参数6】:取内存的大小,个数。类型为NUI_DEPTH_IMAGE_PIXEL
          //【参数7】:存放映射结果点的参数
          hrState = pMapper->MapColorFrameToDepthFrame(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, NUI_IMAGE_RESOLUTION_640x480, 
            640 * 480, (NUI_DEPTH_IMAGE_PIXEL*)ColorToDepthLockRect.pBits,640 * 480, depthPoints); 
  
          if (FAILED(hrState)) 
          { 
            return hrState; 
          } 
  
  
          //显示的图像
          Mat show;
          show.create(480,640,CV_8UC3);
          show = 0;
  
          for (int i = 0; i < image_rgb.rows; i++)
          {
            for (int j = 0; j < image_rgb.cols; j++)
            {
              uchar *prt_rgb = image_rgb.ptr(i);
              uchar *prt_show = show.ptr(i);
              //在内存中偏移量
              long index = i * 640 + j; 
              //从保存了映射坐标的数组中获取点
              NUI_DEPTH_IMAGE_POINT depthPointAtIndex = depthPoints[index]; 
  
              //边界判断
              if (depthPointAtIndex.x >= 0 && depthPointAtIndex.x < image_depth.cols &&
                depthPointAtIndex.y >=0 && depthPointAtIndex.y < image_depth.rows)
              {
                //深度判断,在MIN_DISTANCE与MAX_DISTANCE之间的当成前景,显示出来
                //这个使用也很重要,当使用真正的深度像素点再在深度图像中获取深度值来判断的时候,会出错
                if (depthPointAtIndex.depth >= MIN_DISTANCE && depthPointAtIndex.depth <= MAX_DISTANCE)
                {
                  prt_show[3 * j]   = prt_rgb[j * 3];
                  prt_show[3 * j + 1] = prt_rgb[j * 3 + 1];
                  prt_show[3 * j + 2] = prt_rgb[j * 3 + 2];
                }
              }
            }
          }
          imshow("show", show);
        }
  
        delete []depthPoints;
          
        pTexture->UnlockRect(0);
        m_pNuiSensor->NuiImageStreamReleaseFrame(depthStreamHandle, &pImageFrame_depth);
      }
  
      else
      {
        cout<<"Buffer length of received texture is bogus\r\n"<<endl;
      }
    }
  
    if (waitKey(20) == 27)
      break;
  }
  return 0;
}
在ROS(Robot Operating System)中,获取相机的RGB图像深度图像通常涉及以下几个步骤: 1. **选择合适的包**:对于大多数现代机器人平台,像`camera_calibration`、`image_transport`、`message_filters`等都是处理图像数据的基础包。`depth_image_proc`包可以用于处理深度图像。 2. **设置相机节点**:每个相机需要有自己的ROS节点(如`cv_bridge`或`image_transport`),用来订阅相机发布的原始数据。你可以通过`rosrun`命令启动特定相机驱动程序(如`usb_cam`、`realsense_node`等)。 3. **发布图像话题**:相机节点会发布包含RGB图像深度图的`sensor_msgs/Image`或`sensor_msgs/DepthImage`消息。RGB图像通常是BGR格式(而不是常见的RGB),而深度图则是表示距离的数据,常以米为单位。 4. **数据转换**:为了方便在不同节点间传递,`cv_bridge`工具可以将`sensor_msgs/Image`转换成OpenCV兼容的格式。如果你想同时获取RGB深度信息,可以使用`message_filters`创建一个联合主题,使得两个数据流同步。 5. **订阅和处理图像**:在另一个节点中,使用`image_subscriber`订阅这两个话题,然后通过处理函数接收并操作RGB深度图像。 ```python import rospy from sensor_msgs.msg import Image, DepthImage from cv_bridge import CvBridge # 初始化节点和桥接器 rospy.init_node('image_listener') bridge = CvBridge() def rgb_depth_callback(rgb_msg, depth_msg): # 将深度图像转换为numpy数组 depth_array = bridge.imgmsg_to_numpy(depth_msg, "passthrough") # 将RGB图像转换为OpenCV格式 rgb_image = bridge.imgmsg_to_cv2(rgb_msg) # 进行进一步的处理,例如显示或保存图片 ... # 创建订阅者 rgb_topic = 'camera/rgb/image_color' depth_topic = 'camera/depth_registered/image_rect_depth' subscriber_rgb = rospy.Subscriber(rgb_topic, Image, rgb_depth_callback) subscriber_depth = rospy.Subscriber(depth_topic, DepthImage, rgb_depth_callback) # 等待节点退出 rospy.spin() ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值