How to get user mask in ofxNI2

#1

I’m using https://github.com/pierrep/ofxNI2 which works well in Mac ofv0.10.1
I am trying to find a way to extract the user pixels or shape to make a clean contourFinder with opencv.
After reading the forum, some of the code has been used before is based in another library of openni.
OpenNI || Kinect GetUserPixels

I need some guidance here.
How to modify the following code to match the new ofxNi2 version?

userGenerator.GetUserPixels(0, sceneMetaData);  
depthPixels = (unsigned short*)sceneMetaData.Data();  
  
for (int i =0 ; i < 640 * 480; i++) {  
		if (depthPixels[i] != 0) {  
                      maskPixels[i] = 0;  
		} else {  
		      maskPixels[i] = 255;  
		}    
  }  
maskImage.setFromPixels(maskPixels, 640, 480, OF_IMAGE_GRAYSCALE);  

Best
David

#2

OK it is resolved.
What was missing is the following line in the UserTracker class, ofxNite2.h

inline nite::UserMap getUserMap() {return user_map;}

then in ofApp.h

ofxNiTE2::UserTracker   tracker;
ofImage                 userMask;

in ofApp.cpp

setup()

 tracker.setup(device);
 userMask.allocate(640, 480, OF_IMAGE_GRAYSCALE);

update()

    if(tracker.getNumUser() > 0){
        const short int *userPixels = tracker.getUserMap().getPixels();
        long total = tracker.getUserMap().getWidth() * tracker.getUserMap().getHeight();
        unsigned char maskPixels[total];
        
        for (int i=0; i<total; i++) {
            if (userPixels) {
                if (*userPixels != 0) {
                    maskPixels[i] = 255;
                } else {
                    maskPixels[i] = 0;
                }
            }
            userPixels++;
        }
        userMask.setFromPixels(maskPixels,tracker.getUserMap().getWidth(),tracker.getUserMap().getHeight(),OF_IMAGE_GRAYSCALE);
        userMask.update();
    }

draw()

userMask.draw();

It still can be improved to generate user colours or grey scale depth masks.

#3

I am not sure if you are using ofxOpenCv or ofxCv, but I recommend using ofxCv over ofxOpenCv, if you want better contour/depth accuracy.

With just a few lines of code you will get accurate grey scale depth masks, and with some finicking around with the thresholding, you will get far better contours compared to ofxOpenCv (coming from my personal experiences).