KinectV2 with ofxOpenCv and ofxKinectV2

errortest

Hi. I’m trying to use ofxOpenCv and ofxKinectV2 together but I can’t even set the pixel information from the kinect to an ofxCvColorImage properly.

My code is super simple:

void ofApp::setup(){
kinect.open();
color.allocate(960, 540);
}

//--------------------------------------------------------------
void ofApp::update(){
kinect.update();
if (kinect.isFrameNew()) {
color.setFromPixels(kinect.getPixels());
}

}

//--------------------------------------------------------------
void ofApp::draw(){
color.draw(0, 0, 960, 540);
}

I should a result like an rgb from the camera, which is what I get when I use ofImage or ofTexture, but ofxCvColorImage results in this instead.

Hi,

I think the color stream is 1920 x 1080.
You could try changing that in ofApp::setup or add these lines before color.setFromPixels :

    //new code
    auto pix = kinect.getPixels();
    if( color.getWidth() != pix.getWidth() || color.getHeight() != pix.getHeight() ){
        color.allocate(pix.getWidth(), pix.getHeight());
    }
    //end new code
    color.setFromPixels(pix);

Hope that fixes your issue!
Theo

No, I still get the same result, either changing it to 1920 x 1080 or using your code.

Ah yes, I can reproduce. I think this is because the native format of the pixels is BGRA so you have 4 channels going to 3.

This works for me:

void ofApp::setup(){
    kinect.open();
}

//--------------------------------------------------------------
void ofApp::update(){
    kinect.update();
    if (kinect.isFrameNew()) {
        auto pix = kinect.getPixels();
                
        pix.setNumChannels(3);
        pix.swapRgb();
        
        color.setFromPixels(pix);
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    color.draw(0, 0, 960, 540);
}

Worked like a charm! Thanks!