本文介绍了将PXCImage转换为OpenCV Mat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用英特尔的RealSense SDK。为了信号处理的目的,我必须把它转换成OpenCV格式。



你有一个函数来做这样的转换吗?



<$ p

这是转换(有些情况下不管理,但可以随意调整)

$ p> void ConvertPXCImageToOpenCVMat(PXCImage * inImg,Mat * outImg){
int cvDataType;
int cvDataWidth;


PXCImage :: ImageData data;
inImg-> AcquireAccess(PXCImage :: ACCESS_READ,& data);
PXCImage :: ImageInfo imgInfo = inImg-> QueryInfo();

switch(data.format){
/ * STREAM_TYPE_COLOR * /
case PXCImage :: PIXEL_FORMAT_YUY2:/ * YUY2 image * /
case PXCImage :: PIXEL_FORMAT_NV12: / * NV12 image * /
throw(0); //未实现
case PXCImage :: PIXEL_FORMAT_RGB32:/ *小端机器上的BGRA布局* /
cvDataType = CV_8UC4;
cvDataWidth = 4;
break;
case PXCImage :: PIXEL_FORMAT_RGB24:/ *小端机器上的BGR布局* /
cvDataType = CV_8UC3;
cvDataWidth = 3;
break;
case PXCImage :: PIXEL_FORMAT_Y8:/ * 8位灰色图像或IR 8位* /
cvDataType = CV_8U;
cvDataWidth = 1;
break;

/ * STREAM_TYPE_DEPTH * /
case PXCImage :: PIXEL_FORMAT_DEPTH:/ * 16位无符号整数,精度为mm。 * /
case PXCImage :: PIXEL_FORMAT_DEPTH_RAW:/ *具有设备特定精度的16位无符号整数(call device-> QueryDepthUnit())* /
cvDataType = CV_16U;
cvDataWidth = 2;
break;
case PXCImage :: PIXEL_FORMAT_DEPTH_F32:/ * 32位浮点精度为mm。 * /
cvDataType = CV_32F;
cvDataWidth = 4;
break;

/ * STREAM_TYPE_IR * /
case PXCImage :: PIXEL_FORMAT_Y16:/ * 16位灰色图像* /
cvDataType = CV_16U;
cvDataWidth = 2;
break;
case PXCImage :: PIXEL_FORMAT_Y8_IR_RELATIVE:/ *相对IR映像* /
cvDataType = CV_8U;
cvDataWidth = 1
break;
}

//假设没有其他平面
if(data.planes [1]!= NULL)throw(0); //未实现
//假设没有子像素填充需要
if(data.pitches [0]%cvDataWidth!= 0)throw(0); // not implemented

outImg-> create(imgInfo.height,data.pitches [0] / cvDataWidth,cvDataType);

memcpy(outImg-> data,data.planes [0],imgInfo.height * imgInfo.width * cvDataWidth * sizeof(pxcBYTE));

inImg-> ReleaseAccess(& data);
}


I'm working on the Intel's RealSense SDK. For signal processing purposes, I have to convert it into an OpenCV format.

Do you have a function to do such a conversion?

解决方案

Here is the conversion (some cases aren't managed but feel free to adapt it):

void ConvertPXCImageToOpenCVMat(PXCImage *inImg, Mat *outImg) {
    int cvDataType;
    int cvDataWidth;


    PXCImage::ImageData data;
    inImg->AcquireAccess(PXCImage::ACCESS_READ, &data);
    PXCImage::ImageInfo imgInfo = inImg->QueryInfo();

    switch (data.format) {
        /* STREAM_TYPE_COLOR */
        case PXCImage::PIXEL_FORMAT_YUY2: /* YUY2 image  */
        case PXCImage::PIXEL_FORMAT_NV12: /* NV12 image */
            throw(0); // Not implemented
        case PXCImage::PIXEL_FORMAT_RGB32: /* BGRA layout on a little-endian machine */
            cvDataType = CV_8UC4;
            cvDataWidth = 4;
            break;
        case PXCImage::PIXEL_FORMAT_RGB24: /* BGR layout on a little-endian machine */
            cvDataType = CV_8UC3;
            cvDataWidth = 3;
            break;
        case PXCImage::PIXEL_FORMAT_Y8:  /* 8-Bit Gray Image, or IR 8-bit */
            cvDataType = CV_8U;
            cvDataWidth = 1;
            break;

        /* STREAM_TYPE_DEPTH */
        case PXCImage::PIXEL_FORMAT_DEPTH: /* 16-bit unsigned integer with precision mm. */
        case PXCImage::PIXEL_FORMAT_DEPTH_RAW: /* 16-bit unsigned integer with device specific precision (call device->QueryDepthUnit()) */
            cvDataType = CV_16U;
            cvDataWidth = 2;
            break;
        case PXCImage::PIXEL_FORMAT_DEPTH_F32: /* 32-bit float-point with precision mm. */
            cvDataType = CV_32F;
            cvDataWidth = 4;
            break;

        /* STREAM_TYPE_IR */
        case PXCImage::PIXEL_FORMAT_Y16:          /* 16-Bit Gray Image */
            cvDataType = CV_16U;
            cvDataWidth = 2;
            break;
        case PXCImage::PIXEL_FORMAT_Y8_IR_RELATIVE:    /* Relative IR Image */
            cvDataType = CV_8U;
            cvDataWidth = 1;
            break;
        }

    // suppose that no other planes
    if (data.planes[1] != NULL) throw(0); // not implemented
    // suppose that no sub pixel padding needed
    if (data.pitches[0] % cvDataWidth!=0) throw(0); // not implemented

    outImg->create(imgInfo.height, data.pitches[0] / cvDataWidth, cvDataType);

    memcpy(outImg->data, data.planes[0], imgInfo.height*imgInfo.width*cvDataWidth*sizeof(pxcBYTE));

    inImg->ReleaseAccess(&data);
    }

这篇关于将PXCImage转换为OpenCV Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 06:24