ofxMultiKinect and ofxOpenCV or ofxCv

// Sorry.I am Japanese programmer.So, my English is not good enough.

Hello!I’m trying to use XBOX One Kinect with ofxMultiKinectV2.I can get the IR picture sent from Kinect.But I set it to ofxOpenCV Images or ofxCv Images.

There is my code.This one is using ofxOpenCV.

ofApp.h

    class ofApp : public ofBaseApp{

        public:
        ・・・

        ofxMultiKinectV2 kinect;
        ofTexture texture1;
        ofTexture texture2;
        ofxPanel gui;
        ofxTurboJpeg turbo;
        ofShader irShader;
        ofxCvGrayscaleImage irImage;
        ofFbo frameBuffer;
        ofPixels irPixels;

        int threshold;
        int width;
        int height;
    };

    static string irFragmentShader =
    STRINGIFY(
              uniform sampler2DRect tex;
              void main()
              {
                  vec4 col = texture2DRect(tex, gl_TexCoord[0].xy);
                  float value = col.r / 65535.0;
                  gl_FragColor = vec4(vec3(value), 1.0);
              }
              );

ofApp.cpp

void ofApp::setup(){
    ofBackground(255, 255, 255);
    ofSetVerticalSync(true);
    ofSetFrameRate(60);
    kinect.open(true, true, 0, 2);
    kinect.start();
    irShader.setupShaderFromSource(GL_FRAGMENT_SHADER, irFragmentShader);
    irShader.linkProgram();
    width = 2585;
    height = 1081;

    irImage.allocate(width, height);
    frameBuffer.allocate(width, height);
}

//--------------------------------------------------------------
void ofApp::update(){
    kinect.update();
    if(kinect.isFrameNew()){
        texture2.loadData(kinect.getIrPixelsRef());
        if (texture2.isAllocated()) {
            frameBuffer.begin();
            irShader.begin();
            texture2.draw(0, 0, width, height);
            irShader.end();
            frameBuffer.end();
            frameBuffer.readToPixels(irPixels);

            unsigned char pixs2[width*height*3];
            for(int i = 0; i < width*height; i++)
            {
                pixs2[i*3]   = irPixels[i*4];
                pixs2[i*3+1] = irPixels[i*4];
                pixs2[i*3+2] = irPixels[i*4];
            }
            ofPixels pixels;
            pixels.setFromPixels(pixs2, width, height, GL_RGB);
            irImage.setFromPixels(pixs2, width, height);
        }
    }

}

//--------------------------------------------------------------
void ofApp::draw(){
    irImage.draw(0.0, 0.0, 960, 540);
    ofDrawBitmapStringHighlight(ofToString(ofGetFrameRate()), 10, 20);
    ofDrawBitmapStringHighlight("Device Count : " + ofToString(ofxMultiKinectV2::getDeviceCount()), 10, 40);
}

Can anybody help me?

If I understand the question right. The problem is making the FBO into a ofxOpenCv image? Here is how I have done that in the past:

in .cpp:

ofxCvColorImage myCvImage;
ofFbo myFbo;
ofPixels tempPix;

in setup:

myFbo.allocate(2000, 2000, GL_RGB);
tempPix.allocate(2000, 2000,OF_PIXELS_RGB);
myCvImage.allocate( 2000, 2000);

in Draw:

myFbo.begin();
// draw shader kinect stuff
myFbo.end();
myFbo.readToPixels(tempPix);
myCvImage.setFromPixels(tempPix);
1 Like

I solved this problem.

THANK YOU :heart_eyes: !!

1 Like