Kinect as a webcam

Hello everyone.

I’ve got a problem with this statement:

grayImage.setFromPixels(kinect.getPixels(), kinect.width, kinect.height);

Because when I try to draw grayImage, which is an ofxCvGrayScaleImage, on the canvas:

kinect.draw(20, 10, 400, 300);
grayImage.draw(420, 10, 400, 300);

(here I put another image for comparison)

what I get is this:

I tried to change the dimmensions of all the functions (setFromPixels() and draw()).

I’d like to know how to make grayImage to have the same pixels information as kinect.draw(), can anyone help me?

Thank you.

hi there,

you’re getting an RGB array from ofxKinect when you call getPixels();
see in the source of ofxKinect.cpp:

  
//---------------------------------------------------------------------------  
unsigned char * ofxKinect::getPixels(){  
	return rgbPixels;  
}  
  

and in ofxKinect::init(bool setUseTexture):

  
int length = width*height;  
rgbPixels = new unsigned char[length*3];  

This tells you that the rgbPixels array has size = width * height * 3 (RGB values)
your greyimage has only the size = width * height * 1 (greyscale value)

That’s why you get to see this weird scaling effect, the RGB values are spread across the greyscale array.

for example this is how you would manually create a greyscale array from the rgbPixels you get: (but I’m sure there’s a faster cleaner way to do this)

  
unsigned char greyPixels[w * h];  
for(int i = 0; i < w*h; i++){  
	greyPixels[i] = rgbPixels[3*i]; // using the red value, use rgbPixels[3*i+1] for green, etc..  
}  
greyImage.setFromPixels(greyPixels, w, h);  
  

hope that helps,
tim.