Skip to content

Commit

Permalink
Added setMaskPixelFormatAllUsers and setMaskPixelFormat to ofxOpenNI,…
Browse files Browse the repository at this point in the history
… ofxOpenNIUser, ofxOpenNIDepthThreshold
  • Loading branch information
Matthew Gingold committed Jun 11, 2012
1 parent 457ad0d commit d7f14a7
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 34 deletions.
131 changes: 97 additions & 34 deletions src/ofxOpenNI.cpp
Expand Up @@ -77,7 +77,7 @@ ofxOpenNI::ofxOpenNI(){
bUseSafeThreading = false;
bNewPixels = false;
bNewFrame = false;

width = XN_VGA_X_RES;
height = XN_VGA_Y_RES;
fps = 30;
Expand Down Expand Up @@ -1186,9 +1186,9 @@ void ofxOpenNI::update(){
if(user.getUseMaskTexture() && user.bNewPixels){
if(user.maskTexture.getWidth() != getWidth() || user.maskTexture.getHeight() != getHeight()){
ofLogVerbose(LOG_NAME) << "Allocating mask texture " << user.getXnID();
user.maskTexture.allocate(getWidth(), getHeight(), GL_RGBA);
user.maskTexture.allocate(getWidth(), getHeight(), ofGetGLTypeFromPixelFormat(user.getMaskPixelFormat()));
}
if(user.maskPixels.getPixels() != NULL) user.maskTexture.loadData(user.maskPixels.getPixels(), getWidth(), getHeight(), GL_RGBA);
if(user.maskPixels.getPixels() != NULL) user.maskTexture.loadData(user.maskPixels.getPixels(), getWidth(), getHeight(), ofGetGLTypeFromPixelFormat(user.getMaskPixelFormat()));
}
user.bNewPixels = false;
user.bNewPointCloud = false;
Expand Down Expand Up @@ -1216,9 +1216,9 @@ void ofxOpenNI::update(){
if(depthThreshold.getUseMaskPixels()){
if(depthThreshold.maskTexture.getWidth() != getWidth() || depthThreshold.maskTexture.getHeight() != getHeight()){
ofLogVerbose(LOG_NAME) << "Allocating mask texture for depthThreshold";
depthThreshold.maskTexture.allocate(getWidth(), getHeight(), GL_RGBA);
depthThreshold.maskTexture.allocate(getWidth(), getHeight(), ofGetGLTypeFromPixelFormat(depthThreshold.getMaskPixelFormat()));
}
depthThreshold.maskTexture.loadData(depthThreshold.maskPixels.getPixels(), getWidth(), getHeight(), GL_RGBA);
depthThreshold.maskTexture.loadData(depthThreshold.maskPixels.getPixels(), getWidth(), getHeight(), ofGetGLTypeFromPixelFormat(depthThreshold.getMaskPixelFormat()));
}
if(depthThreshold.getUseDepthPixels()){
if(depthThreshold.depthTexture.getWidth() != getWidth() || depthThreshold.depthTexture.getHeight() != getHeight()){
Expand Down Expand Up @@ -1551,26 +1551,52 @@ void ofxOpenNI::updatePointClouds(ofxOpenNIUser & user){
void ofxOpenNI::updateUserPixels(ofxOpenNIUser & user){
if(bIsThreaded) Poco::ScopedLock<ofMutex> lock();
if(user.maskPixels.getWidth() != getWidth() || user.maskPixels.getHeight() != getHeight()){
user.maskPixels.allocate(getWidth(), getHeight(), OF_IMAGE_COLOR_ALPHA);
user.maskPixels.allocate(getWidth(), getHeight(), user.getMaskPixelFormat());
}

int nIndex = 0;
for (int nY = 0; nY < getHeight(); nY++) {
for (int nX = 0; nX < getWidth(); nX++) {
nIndex = nY * getWidth() + nX;
if(user.userPixels[nIndex] == user.getXnID()) {
user.maskPixels[nIndex * 4 + 0] = 255;
user.maskPixels[nIndex * 4 + 1] = 255;
user.maskPixels[nIndex * 4 + 2] = 255;
user.maskPixels[nIndex * 4 + 3] = 0;
} else {
user.maskPixels[nIndex * 4 + 0] = 0;
user.maskPixels[nIndex * 4 + 1] = 0;
user.maskPixels[nIndex * 4 + 2] = 0;
user.maskPixels[nIndex * 4 + 3] = 255;
switch (user.getMaskPixelFormat()) {
case OF_PIXELS_RGBA:
{
int nIndex = 0;
for (int nY = 0; nY < getHeight(); nY++) {
for (int nX = 0; nX < getWidth(); nX++) {
nIndex = nY * getWidth() + nX;
if(user.userPixels[nIndex] == user.getXnID()) {
user.maskPixels[nIndex * 4 + 0] = 255;
user.maskPixels[nIndex * 4 + 1] = 255;
user.maskPixels[nIndex * 4 + 2] = 255;
user.maskPixels[nIndex * 4 + 3] = 0;
} else {
user.maskPixels[nIndex * 4 + 0] = 0;
user.maskPixels[nIndex * 4 + 1] = 0;
user.maskPixels[nIndex * 4 + 2] = 0;
user.maskPixels[nIndex * 4 + 3] = 255;
}
}
}
}
break;
case OF_PIXELS_MONO:
{
int nIndex = 0;
for (int nY = 0; nY < getHeight(); nY++) {
for (int nX = 0; nX < getWidth(); nX++) {
nIndex = nY * getWidth() + nX;
if(user.userPixels[nIndex] == user.getXnID()) {
user.maskPixels[nIndex] = 255;
} else {
user.maskPixels[nIndex] = 0;
}
}
}
}
break;

default:
ofLogError(LOG_NAME) << "Mask pixel type not supported: " << user.getMaskPixelFormat();
break;
}


user.bNewPixels = true;
}

Expand Down Expand Up @@ -1660,18 +1686,37 @@ void ofxOpenNI::updateDepthThresholds(const unsigned short& depth, ofColor& dept
if(depthThreshold.getUseMaskPixels()){
if(depthThreshold.maskPixels.getWidth() != getWidth() || depthThreshold.maskPixels.getHeight() != getHeight()){
ofLogVerbose(LOG_NAME) << "Allocating mask pixels for depthThreshold";
depthThreshold.maskPixels.allocate(getWidth(), getHeight(), OF_PIXELS_RGBA);
depthThreshold.maskPixels.allocate(getWidth(), getHeight(), depthThreshold.getMaskPixelFormat());
}
if(inside){
depthThreshold.maskPixels[nIndex * 4 + 0] = 255;
depthThreshold.maskPixels[nIndex * 4 + 1] = 255;
depthThreshold.maskPixels[nIndex * 4 + 2] = 255;
depthThreshold.maskPixels[nIndex * 4 + 3] = 0;
}else{
depthThreshold.maskPixels[nIndex * 4 + 0] = 0;
depthThreshold.maskPixels[nIndex * 4 + 1] = 0;
depthThreshold.maskPixels[nIndex * 4 + 2] = 0;
depthThreshold.maskPixels[nIndex * 4 + 3] = 255;
switch (depthThreshold.getMaskPixelFormat()) {
case OF_PIXELS_RGBA:
{
if(inside){
depthThreshold.maskPixels[nIndex * 4 + 0] = 255;
depthThreshold.maskPixels[nIndex * 4 + 1] = 255;
depthThreshold.maskPixels[nIndex * 4 + 2] = 255;
depthThreshold.maskPixels[nIndex * 4 + 3] = 0;
}else{
depthThreshold.maskPixels[nIndex * 4 + 0] = 0;
depthThreshold.maskPixels[nIndex * 4 + 1] = 0;
depthThreshold.maskPixels[nIndex * 4 + 2] = 0;
depthThreshold.maskPixels[nIndex * 4 + 3] = 255;
}
}
break;
case OF_PIXELS_MONO:
{
if(inside){
depthThreshold.maskPixels[nIndex] = 255;
}else{
depthThreshold.maskPixels[nIndex] = 0;
}
}
break;

default:
ofLogError(LOG_NAME) << "Mask pixel type not supported: " << depthThreshold.getMaskPixelFormat();
break;
}
depthThreshold.bNewPixels = true;
}
Expand Down Expand Up @@ -1818,6 +1863,24 @@ bool ofxOpenNI::getUseMaskPixelsAllUsers(){
return baseUser.getUseMaskPixels();
}

//--------------------------------------------------------------
void ofxOpenNI::setMaskPixelFormatAllUsers(ofPixelFormat format){
if(format == OF_PIXELS_MONO || format == OF_PIXELS_RGBA){
baseUser.setMaskPixelFormat(format);
for (int i = 0; i < getNumTrackedUsers(); i++){
currentTrackedUsers[currentTrackedUserIDs[i]].setMaskPixelFormat(format);
}
}else{
ofLogError(LOG_NAME) << "Mask pixel format not supported: " << format;
}

}

//--------------------------------------------------------------
ofPixelFormat ofxOpenNI::getMaskPixelFormatAllUsers(){
return baseUser.getMaskPixelFormat();
}

//--------------------------------------------------------------
void ofxOpenNI::setUsePointCloudsAllUsers(bool b){
baseUser.setUsePointCloud(b);
Expand Down Expand Up @@ -2207,8 +2270,8 @@ void ofxOpenNI::addDepthThreshold(ofxOpenNIDepthThreshold & depthThreshold){

//--------------------------------------------------------------
void ofxOpenNI::addDepthThreshold(int _nearThreshold, int _farThreshold, bool _bUseCloudPoint, bool _bUseMaskPixels, bool _bUseMaskTexture, bool _bUseDepthPixels, bool _bUseDepthTexture){
// ofxOpenNIDepthThreshold depthThreshold = ofxOpenNIDepthThreshold(_nearThreshold, _farThreshold, _bUseCloudPoint, _bUseMaskPixels, _bUseMaskTexture, _bUseDepthPixels, _bUseDepthTexture);
//addDepthThreshold(depthThreshold);
ofxOpenNIDepthThreshold depthThreshold = ofxOpenNIDepthThreshold(_nearThreshold, _farThreshold, _bUseCloudPoint, _bUseMaskPixels, _bUseMaskTexture, _bUseDepthPixels, _bUseDepthTexture);
addDepthThreshold(depthThreshold);
}

//--------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions src/ofxOpenNI.h
Expand Up @@ -187,6 +187,8 @@ class ofxOpenNI : public ofThread {
bool getUseMaskTextureAllUsers();
void setUseMaskPixelsAllUsers(bool b);
bool getUseMaskPixelsAllUsers();
void setMaskPixelFormatAllUsers(ofPixelFormat type);
ofPixelFormat getMaskPixelFormatAllUsers();
void setUsePointCloudsAllUsers(bool b);
bool getUsePointCloudsAllUsers();
void setPointCloudDrawSizeAllUsers(int size);
Expand Down
33 changes: 33 additions & 0 deletions src/ofxOpenNITypes.cpp
Expand Up @@ -50,6 +50,8 @@ ofxOpenNIUser::ofxOpenNIUser(){
bForceRestart = false;
bUseOrientation = false;

maskPixelFormat = OF_PIXELS_RGBA;

userPixels = NULL;

forcedResetTimeout = 1000;
Expand Down Expand Up @@ -264,6 +266,20 @@ bool ofxOpenNIUser::getUseMaskPixels(){
return (bUseMaskPixels || bUseMaskTexture);
}

//--------------------------------------------------------------
void ofxOpenNIUser::setMaskPixelFormat(ofPixelFormat format){
maskPixelFormat = format;
if(maskPixels.getImageType() != ofGetImageTypeFromGLType(maskPixelFormat)){
maskPixels.allocate(maskPixels.getWidth(), maskPixels.getHeight(), maskPixelFormat);
if(bUseMaskTexture) maskTexture.allocate(maskPixels.getWidth(), maskPixels.getHeight(), ofGetGLTypeFromPixelFormat(maskPixelFormat));
}
}

//--------------------------------------------------------------
ofPixelFormat ofxOpenNIUser::getMaskPixelFormat(){
return maskPixelFormat;
}

//--------------------------------------------------------------
void ofxOpenNIUser::setUseSkeleton(bool b){
bUseSkeleton = b;
Expand Down Expand Up @@ -463,6 +479,7 @@ ofxOpenNIDepthThreshold::ofxOpenNIDepthThreshold(){
bUseDepthTexture = false;
bNewPixels = false;
bNewPointCloud = false;
maskPixelFormat = OF_PIXELS_RGBA;
}

//--------------------------------------------------------------
Expand Down Expand Up @@ -510,6 +527,7 @@ void ofxOpenNIDepthThreshold::set(int _nearThreshold,
bUseMaskTexture = _bUseMaskTexture;
bUseDepthPixels = _bUseDepthPixels;
bUseDepthTexture = _bUseDepthTexture;
maskPixelFormat = OF_PIXELS_RGBA;
bNewPixels = false;
bNewPointCloud = false;
}
Expand All @@ -530,6 +548,7 @@ void ofxOpenNIDepthThreshold::set(ofxOpenNIROI & _roi,
bUseMaskTexture = _bUseMaskTexture;
bUseDepthPixels = _bUseDepthPixels;
bUseDepthTexture = _bUseDepthTexture;
maskPixelFormat = OF_PIXELS_RGBA;
bNewPixels = false;
bNewPointCloud = false;
}
Expand Down Expand Up @@ -653,6 +672,20 @@ bool ofxOpenNIDepthThreshold::getUseMaskPixels(){
return bUseMaskPixels;
}

//--------------------------------------------------------------
void ofxOpenNIDepthThreshold::setMaskPixelFormat(ofPixelFormat format){
maskPixelFormat = format;
if(maskPixels.getImageType() != ofGetImageTypeFromGLType(maskPixelFormat)){
maskPixels.allocate(maskPixels.getWidth(), maskPixels.getHeight(), maskPixelFormat);
if(bUseMaskTexture) maskTexture.allocate(maskPixels.getWidth(), maskPixels.getHeight(), ofGetGLTypeFromPixelFormat(maskPixelFormat));
}
}

//--------------------------------------------------------------
ofPixelFormat ofxOpenNIDepthThreshold::getMaskPixelFormat(){
return maskPixelFormat;
}

//--------------------------------------------------------------
void ofxOpenNIDepthThreshold::setUseDepthTexture(bool b){
bUseDepthTexture = b;
Expand Down
10 changes: 10 additions & 0 deletions src/ofxOpenNITypes.h
Expand Up @@ -369,6 +369,9 @@ class ofxOpenNIUser {
void setUseMaskPixels(bool b);
bool getUseMaskPixels();

void setMaskPixelFormat(ofPixelFormat format);
ofPixelFormat getMaskPixelFormat();

void setUsePointCloud(bool b);
bool getUsePointCloud();

Expand Down Expand Up @@ -459,6 +462,8 @@ class ofxOpenNIUser {
ofPixels maskPixels;
ofTexture maskTexture;

ofPixelFormat maskPixelFormat;

int XnID;

int pointCloudDrawSize;
Expand Down Expand Up @@ -769,6 +774,9 @@ class ofxOpenNIDepthThreshold {
void setUseMaskPixels(bool b);
bool getUseMaskPixels();

void setMaskPixelFormat(ofPixelFormat format);
ofPixelFormat getMaskPixelFormat();

void setUseDepthTexture(bool b);
bool getUseDepthTexture();

Expand Down Expand Up @@ -797,6 +805,8 @@ class ofxOpenNIDepthThreshold {
ofPixels maskPixels;
ofTexture maskTexture;

ofPixelFormat maskPixelFormat;

int pointCloudDrawSize;
int pointCloudResolution;

Expand Down

0 comments on commit d7f14a7

Please sign in to comment.