ofxOpenCv and ofxKinectForWindows2


#1

Hi everybody,
I’m studing C++ code to develop an algorithm for human and object detection (static and dinamic object).
I have acquired the depth and RGBA image, but I can not copy the RGBA array to an array OpenCV.
I do this in order to use the OpenCV functions to edit images (in order to use gaussianBlur, region growing algorithm …). can you help me?

my code:

 void ofApp::setup(){
if (FAILED(GetDefaultKinectSensor(&sensor))) {
	ofLogError() << "Could not acquire IR!";
}
if (sensor) {
	sensor->Open();
	IDepthFrameSource* framesourceDept = NULL;
	sensor->get_DepthFrameSource(&framesourceDept);
	framesourceDept->OpenReader(&readerDept);
	if (framesourceDept) {
		framesourceDept->Release();
		framesourceDept = NULL;
	}
	IColorFrameSource* framesourceColor = NULL;
	sensor->get_ColorFrameSource(&framesourceColor);
	framesourceColor->OpenReader(&readerColor);
	if (framesourceColor) {
		framesourceColor->Release();
		framesourceColor = NULL;
	}
}

colorImage.allocate(COLOR_WIDTH, COLOR_HEIGHT, OF_IMAGE_COLOR_ALPHA);
grayImage.allocate(DEPTH_WIDTH, DEPTH_HEIGHT);



 }
 void ofApp::update(){

IDepthFrame* frame = NULL;
if (SUCCEEDED(readerDept->AcquireLatestFrame(&frame))) {
	unsigned int sz;
	unsigned short* buf;
	frame->AccessUnderlyingBuffer(&sz, &buf);

	const unsigned short* curr = (const unsigned short*)buf;
	


	for (int i = 0; i < DEPTH_SIZE; i++)
	{
		pixel[i] = (*curr++)*(255.0 / 2000.0);
	}

	grayImage.setFromPixels(pixel, DEPTH_WIDTH, DEPTH_HEIGHT);  // grayImage is an object of 'ofxCvImage' class
}
if (frame) frame->Release();

IColorFrame* frameC = NULL;
if (SUCCEEDED(readerColor->AcquireLatestFrame(&frameC))) {
	frameC->CopyConvertedFrameDataToArray(COLOR_WIDTH*COLOR_HEIGHT * 4, data, ColorImageFormat_Rgba);
	colorImage.setFromPixels(data, COLOR_WIDTH, COLOR_HEIGHT, OF_IMAGE_COLOR_ALPHA, true); // colorImag is an object of 'ofImage' class
	//mat = cvMemStorageAlloc(CvMemStorage, COLOR_WIDTH*COLOR_HEIGHT * 4);
}
if (frameC) frameC->Release();

}

void ofApp::draw(){
grayImage.draw(0,0);
colorImage.draw(512, 0);

}

thanks a lot!


#2

try this:

int kinectWidth = 512;
	int kinectHeight = 424;
	ofShortPixels kinPix;
	ofPixels grayPix;
kinPix = kinect.getDepthSource()->getPixels();
		//cout << " kinPix[200*500]" << int(kinPix[200 * 500]) << endl;
		for (int i = 0; i < grayPix.size(); i++) {
			grayPix[i] = 0;
			if (kinPix[i] < farThreshold &&  kinPix[i] != 0) {
				grayPix[i] = 255;
			}

		}

		grayImage.setFromPixels(grayPix);