OpenNI RGB image to OpenCV BGR IplImage conversion?

Rasoul picture Rasoul · Dec 3, 2011 · Viewed 7.2k times · Source

The image which one can get from OpenNI Image Meta Data is arranged as an RGB image. I would like to convert it to OpenCV IplImage which by default assumes the data to be stored as BGR. I use the following code:

    XnUInt8 * pImage = new XnUInt8 [640*480*3]; 
    memcpy(pImage,imageMD.Data(),640*480*3*sizeof(XnUInt8));
    XnUInt8 temp;
    for(size_t row=0; row<480; row++){
        for(size_t col=0;col<3*640; col+=3){
            size_t index = row*3*640+col;
            temp = pImage[index];
            pImage[index] = pImage[index+2];
            pImage[index+2] = temp;
        }
    }
    img->imageData = (char*) pImage;

What is the best way (fastest) in C/C++ to perform this conversion such that RGB image becomes BGR (in IplImage format)?

Answer

Elias Ximenes picture Elias Ximenes · Dec 7, 2011

Is it not easy to use the color conversion function of OpenCV?

imgColor->imageData = (char*) pImage;
cvCvtColor( imgColor, imgColor, CV_BGR2RGB);