Exemplo n.º 1
0
bool VideoSourceKinect::getNextFrame(int* pnStatus_){

	Status nRetVal = STATUS_OK;
	unsigned int time_stamp_c;
	{
		openni::VideoStream* streams[] = { &_depth, &_color };

		int changedIndex = -1;
		//_colorFrame.release();

		while (nRetVal == STATUS_OK /*|| !_colorFrame.isValid() || !_depthFrame.isValid()*/) //if any one of the frames are not loaded properly, then loop to try to load them
		{
			nRetVal = openni::OpenNI::waitForAnyStream(streams, 2, &changedIndex, 0);
			if (nRetVal == openni::STATUS_OK)
			{
				switch (changedIndex)
				{
				case 0:
					_depth.readFrame(&_depthFrame); break;
				case 1:
					_color.getCameraSettings()->setExposure(_exposure);
					_color.readFrame(&_colorFrame);
					time_stamp_c = _colorFrame.getTimestamp();
					cout << " time stamp: " << time_stamp_c - time_stamp << endl;
					time_stamp = time_stamp_c;
					break;
				default:
					printf("Error in wait\n");
				}
			}
		}
	}

	//load color image to 
	if (!_colorFrame.isValid() || !_depthFrame.isValid()) return false;

	cv::Mat cvmRGB(__aRGBH[_uResolution], __aRGBW[_uResolution], CV_8UC3, (unsigned char*)_colorFrame._getFrame()->data);
	cv::Mat cvmDep(__aDepthH[_uResolution], __aDepthW[_uResolution], CV_16UC1, (unsigned short*)_depthFrame._getFrame()->data);
	cvmRGB.copyTo(_cvmRGB);
	int avg_n = 30;
	if (_nTotalFrameToAccumulate < avg_n){
		Mat RGB;
		_cvmRGB.convertTo(RGB, CV_32SC3);
		Mat cvmBGR; cv::cvtColor(_cvmRGB, cvmBGR, CV_RGB2BGR);

		ostringstream Convert;
		Convert << "..//" << _serial_number << "//" << _color.getCameraSettings()->getExposure() << "." << _nTotalFrameToAccumulate << ".png";
		if (!imwrite(Convert.str(), cvmBGR))return false;
		{
			ostringstream Convert;
			Convert << "..//" << _serial_number << "//depth//" << _color.getCameraSettings()->getExposure() << ".depth." << _nTotalFrameToAccumulate << ".png";
			if (!imwrite(Convert.str(), cvmDep))return false;
		}
		_nTotalFrameToAccumulate++;
	}
	else if (_nTotalFrameToAccumulate == avg_n)
	{
		cout << _color.getCameraSettings()->getExposure() << endl;
		_nTotalFrameToAccumulate = 0;
		if (_exposure == 0){
			_exposure = 3;
		} else {
			_exposure <<= 1;
		}
	}
	
    return true;
}
void CKinectCapturer::getNextFrameRGB(cv::Mat& cvmRGB_){
	const XnRGB24Pixel* _pRGBMap = _cRGBGen.GetRGB24ImageMap();
	cv::Mat cvmRGB(480,640,CV_8UC3,(uchar*)_pRGBMap);
	cv::cvtColor(cvmRGB,cvmRGB_, CV_BGR2RGB);
}