75 #include <opencv2/highgui/highgui.hpp>
76 #include <opencv2/core/core_c.h>
78 #include <yarp/os/Log.h>
79 #include <yarp/os/LogStream.h>
80 #include <yarp/os/ResourceFinder.h>
81 #include <yarp/cv/Cv.h>
83 #include <iCub/pf3dTracker.hpp>
86 using namespace yarp::os;
87 using namespace yarp::sig;
88 using namespace yarp::cv;
90 void printMat(CvMat* A);
93 PF3DTracker::PF3DTracker()
98 PF3DTracker::~PF3DTracker()
103 bool PF3DTracker::configure(ResourceFinder &rf)
105 _doneInitializing=
false;
109 string trackedObjectColorTemplate;
111 string trackedObjectShapeTemplate;
112 string motionModelMatrix;
115 double widthRatio, heightRatio;
118 _saveImagesWithOpencv=
false;
120 _lut =
new Lut[256*256*256];
126 srand((
unsigned int)time(0));
127 rngState = cvRNG(rand());
132 int sizes[3]={YBins,UBins,VBins};
133 _modelHistogramMat=cvCreateMatND(dimensions, sizes, CV_32FC1);
134 if(_modelHistogramMat==0)
136 yWarning(
"PF3DTracker::open - I wasn\'t able to allocate memory for _modelHistogramMat.");
139 _innerHistogramMat=cvCreateMatND(dimensions, sizes, CV_32FC1);
140 if(_innerHistogramMat==0)
142 yWarning(
"PF3DTracker::open - I wasn\'t able to allocate memory for _innerHistogramMat.");
145 _outerHistogramMat=cvCreateMatND(dimensions, sizes, CV_32FC1);
146 if(_outerHistogramMat==0)
148 yWarning(
"PF3DTracker::open - I wasn\'t able to allocate memory for _outerHistogramMat.");
152 _model3dPointsMat=cvCreateMat(3, 2*nPixels, CV_32FC1);
153 if(_model3dPointsMat==0)
155 yWarning(
"PF3DTracker::open - I wasn\'t able to allocate memory for _model3dPointsMat.");
158 _points2Mat=cvCreateMat(3, 2*nPixels, CV_32FC1);
161 yWarning(
"PF3DTracker::open - I wasn\'t able to allocate memory for _points2Mat.");
164 _tempMat=cvCreateMat(3, 2*nPixels, CV_32FC1);
167 yWarning(
"PF3DTracker::open - I wasn\'t able to allocate memory for _tempMat.");
174 string initializationFile = rf.check(
"from",Value(
"pf3dTracker.ini"),
"Initialization file (string)").asString();
175 string context = rf.check(
"context",Value(
"pf3dTracker"),
"Context (string)").asString();
186 Bottle botConfig(rf.toString());
190 _inputVideoPortName = botConfig.check(
"inputVideoPort",
191 Value(
"/pf3dTracker/video:i"),
192 "Input video port (string)").asString();
193 _inputVideoPort.open(_inputVideoPortName);
195 _outputVideoPortName = botConfig.check(
"outputVideoPort",
196 Value(
"/pf3dTracker/video:o"),
197 "Output video port (string)").asString();
198 _outputVideoPort.open(_outputVideoPortName);
200 _outputDataPortName = botConfig.check(
"outputDataPort",
201 Value(
"/pf3dTracker/data:o"),
202 "Output data port (string)").asString();
203 _outputDataPort.open(_outputDataPortName);
205 _inputParticlePortName = botConfig.check(
"inputParticlePort",
206 Value(
"/pf3dTracker/particles:i"),
207 "Input particle port (string)").asString();
208 _inputParticlePort.open(_inputParticlePortName);
210 _outputParticlePortName = botConfig.check(
"outputParticlePort",
211 Value(
"/pf3dTracker/particles:o"),
212 "Output particle port (string)").asString();
213 _outputParticlePort.open(_outputParticlePortName);
215 _outputAttentionPortName = botConfig.check(
"outputAttentionPort",
216 Value(
"/pf3dTracker/attention:o"),
217 "Output attention port (string)").asString();
218 _outputAttentionPort.open(_outputAttentionPortName);
220 _likelihoodThreshold = (float)botConfig.check(
"likelihoodThreshold",
222 "Likelihood threshold value (double)").asFloat64();
224 _attentionOutputMax = botConfig.check(
"attentionOutputMax",
226 "attentionOutputMax (double)").asFloat64();
227 _attentionOutputDecrease = botConfig.check(
"attentionOutputDecrease",
229 "attentionOutputDecrease (double)").asFloat64();
231 if (botConfig.check(
"outputUVDataPort"))
234 _outputUVDataPortName = botConfig.check(
"outputUVDataPort",
235 Value(
"/PF3DTracker/dataUVOut"),
236 "Image plane output data port (string)").asString();
237 _outputUVDataPort.open(_outputUVDataPortName);
241 yWarning(
"No (u,v) data will be supplied");
242 supplyUVdata =
false;
245 _nParticles = botConfig.check(
"nParticles",
247 "Number of particles used in the tracker (int)").asInt32();
249 _colorTransfPolicy = botConfig.check(
"colorTransfPolicy",
251 "Color transformation policy (int)").asInt32();
252 if(_colorTransfPolicy!=0 && _colorTransfPolicy!=1)
254 yWarning() <<
"Color trasformation policy "<<_colorTransfPolicy<<
" is not yet implemented.";
258 _inside_outside_difference_weight = (float)botConfig.check(
"insideOutsideDiffWeight",
260 "Inside-outside difference weight in the likelihood function (double)").asFloat64();
262 _projectionModel = botConfig.check(
"projectionModel",
263 Value(
"perspective"),
264 "Projection model (string)").asString();
266 if(_projectionModel==
"perspective")
269 if (botConfig.check(
"cameraContext") && botConfig.check(
"cameraFile") && botConfig.check(
"cameraGroup"))
271 ResourceFinder camera_rf;
272 camera_rf.setDefaultContext(botConfig.find(
"cameraContext").asString().c_str());
273 camera_rf.setDefaultConfigFile(botConfig.find(
"cameraFile").asString().c_str());
274 camera_rf.configure(0,NULL);
275 Bottle ¶ms=camera_rf.findGroup(botConfig.find(
"cameraGroup").asString());
276 if (!params.isNull())
278 _calibrationImageWidth =params.check(
"w",Value(320)).asInt32();
279 _calibrationImageHeight=params.check(
"h",Value(240)).asInt32();
280 _perspectiveFx =(float)params.check(
"fx",Value(257.34)).asFloat64();
281 _perspectiveFy =(float)params.check(
"fy",Value(257.34)).asFloat64();
282 _perspectiveCx =(float)params.check(
"cx",Value(160.0)).asFloat64();
283 _perspectiveCy =(float)params.check(
"cy",Value(120.0)).asFloat64();
290 _calibrationImageWidth =botConfig.check(
"w",Value(320)).asInt32();
291 _calibrationImageHeight=botConfig.check(
"h",Value(240)).asInt32();
292 _perspectiveFx =(float)botConfig.check(
"perspectiveFx",Value(257.34)).asFloat64();
293 _perspectiveFy =(float)botConfig.check(
"perspectiveFy",Value(257.34)).asFloat64();
294 _perspectiveCx =(float)botConfig.check(
"perspectiveCx",Value(160.0)).asFloat64();
295 _perspectiveCy =(float)botConfig.check(
"perspectiveCy",Value(120.0)).asFloat64();
298 cout<<
"w ="<<_calibrationImageWidth<<endl;
299 cout<<
"h ="<<_calibrationImageHeight<<endl;
300 cout<<
"fx="<<_perspectiveFx<<endl;
301 cout<<
"fy="<<_perspectiveFy<<endl;
302 cout<<
"cx="<<_perspectiveCx<<endl;
303 cout<<
"cy="<<_perspectiveCy<<endl;
307 if(_projectionModel==
"equidistance" || _projectionModel==
"unified")
309 yWarning() <<
"Projection model "<<_projectionModel<<
" is not yet implemented.";
314 yWarning() <<
"The projection model you specified ("<<_projectionModel<<
") is not supported.";
319 _initializationMethod = botConfig.check(
"initializationMethod",
321 "Initialization method (string)").asString();
323 if(_initializationMethod==
"3dEstimate")
325 _initialX = botConfig.check(
"initialX",
327 "Estimated initial X position [m] (double)").asFloat64()*1000;
328 _initialY = botConfig.check(
"initialY",
330 "Estimated initial Y position [m] (double)").asFloat64()*1000;
331 _initialZ = botConfig.check(
"initialZ",
333 "Estimated initial Z position [m] (double)").asFloat64()*1000;
337 if(_initializationMethod==
"2dEstimate" || _initializationMethod==
"search")
339 yWarning() <<
"Initialization method "<<_initializationMethod<<
" is not yet implemented.";
344 yWarning() <<
"The initialization method you specified ("<<_initializationMethod<<
") is not supported.";
349 _trackedObjectType = botConfig.check(
"trackedObjectType",
351 "Tracked object type (string)").asString();
352 if(_trackedObjectType==
"sphere")
356 _circleVisualizationMode = botConfig.check(
"circleVisualizationMode",
358 "Visualization mode for the sphere (int)").asInt32();
362 if(_trackedObjectType==
"parallelogram")
364 yWarning() <<
"Tracked object type "<<_trackedObjectType<<
" is not yet implemented.";
369 yWarning() <<
"The tracked object type you specified ("<<_trackedObjectType<<
") is not supported.";
378 trackedObjectColorTemplate = rf.findFile(
"trackedObjectColorTemplate");
379 dataFileName = rf.findFile(
"trackedObjectTemp");
382 failure=computeTemplateHistogram(trackedObjectColorTemplate,dataFileName);
385 yWarning(
"I had troubles computing the template histogram.");
389 failure=readModelHistogram(_modelHistogramMat,dataFileName.c_str());
392 yWarning(
"I had troubles reading the template histogram.");
399 trackedObjectShapeTemplate = rf.findFile(
"trackedObjectShapeTemplate");
400 failure=readInitialmodel3dPoints(_model3dPointsMat,trackedObjectShapeTemplate);
403 yWarning(
"I had troubles reading the model 3D points.");
407 if((_trackedObjectType==
"sphere") && (_circleVisualizationMode==1))
410 _visualization3dPointsMat=cvCreateMat( 3, 2*nPixels, CV_32FC1 );
412 cvSet(_visualization3dPointsMat,(cvScalar(1)));
413 for(row=0;row<3;row++)
415 for(column=0;column<nPixels;column++)
417 ((
float*)(_visualization3dPointsMat->data.ptr + _visualization3dPointsMat->step*row))[column]=(((
float*)(_model3dPointsMat->data.ptr + _model3dPointsMat->step*row))[column]+((
float*)(_model3dPointsMat->data.ptr + _model3dPointsMat->step*row))[column+nPixels])/2;
426 _accelStDev = (float)botConfig.check(
"accelStDev",
428 "StDev of acceleration noise (double)").asFloat64();
430 motionModelMatrix = rf.findFile(
"motionModelMatrix");
433 _A=cvCreateMat(7,7,CV_32FC1);
434 failure=readMotionModelMatrix(_A, motionModelMatrix);
437 yWarning(
"I had troubles reading the motion model matrix.");
442 _particles=cvCreateMat(7,_nParticles,CV_32FC1);
444 cvSetZero(_particles);
447 _particles1 = cvCreateMatHeader( 1,_nParticles, CV_32FC1);
448 cvInitMatHeader( _particles1, 1, _nParticles, CV_32FC1, _particles->data.ptr, _particles->step );
449 _particles2 = cvCreateMatHeader( 1,_nParticles, CV_32FC1);
450 cvInitMatHeader( _particles2, 1, _nParticles, CV_32FC1, _particles->data.ptr + _particles->step*1, _particles->step );
451 _particles3 = cvCreateMatHeader( 1,_nParticles, CV_32FC1);
452 cvInitMatHeader( _particles3, 1, _nParticles, CV_32FC1, _particles->data.ptr + _particles->step*2, _particles->step );
453 _particles4 = cvCreateMatHeader( 1,_nParticles, CV_32FC1);
454 cvInitMatHeader( _particles4, 1, _nParticles, CV_32FC1, _particles->data.ptr + _particles->step*3, _particles->step );
455 _particles5 = cvCreateMatHeader( 1,_nParticles, CV_32FC1);
456 cvInitMatHeader( _particles5, 1, _nParticles, CV_32FC1, _particles->data.ptr + _particles->step*4, _particles->step );
457 _particles6 = cvCreateMatHeader( 1,_nParticles, CV_32FC1);
458 cvInitMatHeader( _particles6, 1, _nParticles, CV_32FC1, _particles->data.ptr + _particles->step*5, _particles->step );
459 _particles7 = cvCreateMatHeader( 1,_nParticles, CV_32FC1);
460 cvInitMatHeader( _particles7, 1, _nParticles, CV_32FC1, _particles->data.ptr + _particles->step*6, _particles->step );
461 _particles1to6 = cvCreateMatHeader( 6,_nParticles, CV_32FC1);
462 cvInitMatHeader( _particles1to6, 6, _nParticles, CV_32FC1, _particles->data.ptr, _particles->step );
465 _newParticles=cvCreateMat(7,_nParticles,CV_32FC1);
466 _newParticles1to6 = cvCreateMatHeader( 6,_nParticles, CV_32FC1);
467 cvInitMatHeader( _newParticles1to6, 6, _nParticles, CV_32FC1, _newParticles->data.ptr, _newParticles->step );
470 _noise=cvCreateMat(6,_nParticles,CV_32FC1);
472 _noise1 = cvCreateMatHeader( 3,_nParticles, CV_32FC1);
473 cvInitMatHeader( _noise1, 3, _nParticles, CV_32FC1, _noise->data.ptr, _noise->step );
475 _noise2 = cvCreateMatHeader( 3,_nParticles, CV_32FC1);
476 cvInitMatHeader( _noise2, 3, _nParticles, CV_32FC1, _noise->data.ptr + _noise->step*3, _noise->step );
480 _nChildren = cvCreateMat(1,_nParticles,CV_32FC1);
481 _label = cvCreateMat(1,_nParticles,CV_32FC1);
482 _ramp = cvCreateMat(1,_nParticles,CV_32FC1);
483 _u = cvCreateMat(1,_nParticles,CV_32FC1);
485 _cumWeight =cvCreateMat(1,_nParticles+1,CV_32FC1);
488 for(count=0;count<_nParticles;count++)
490 ((
float*)(_ramp->data.ptr))[count]=(
float)count+1.0F;
493 temp = botConfig.check(
"saveImagesWithOpencv",
495 "Save elaborated images with OpenCV? (string)").asString();
498 _saveImagesWithOpencv=
true;
501 if(_saveImagesWithOpencv)
503 _saveImagesWithOpencvDir = botConfig.check(
"saveImagesWithOpencvDir",
505 "Directory where to save the elaborated images (string)").asString();
508 if(_initializationMethod==
"3dEstimate")
515 float mean,velocityStDev;
519 mean=(float)_initialX;
520 cvRandArr( &rngState, _particles1, CV_RAND_NORMAL, cvScalar(mean), cvScalar(_accelStDev));
522 mean=(float)_initialY;
523 cvRandArr( &rngState, _particles2, CV_RAND_NORMAL, cvScalar(mean), cvScalar(_accelStDev));
525 mean=(float)_initialZ;
526 cvRandArr( &rngState, _particles3, CV_RAND_NORMAL, cvScalar(mean), cvScalar(_accelStDev));
529 cvRandArr( &rngState, _particles4, CV_RAND_NORMAL, cvScalar(mean), cvScalar(velocityStDev));
531 cvRandArr( &rngState, _particles5, CV_RAND_NORMAL, cvScalar(mean), cvScalar(velocityStDev));
533 cvRandArr( &rngState, _particles6, CV_RAND_NORMAL, cvScalar(mean), cvScalar(velocityStDev));
540 _rzMat = cvCreateMat(3, 3, CV_32FC1);
541 _ryMat = cvCreateMat(3, 3, CV_32FC1);
542 _uv = cvCreateMat(2,2*nPixels, CV_32FC1);
544 _tempMat1 = cvCreateMatHeader( 1,2*nPixels, CV_32FC1);
545 cvInitMatHeader( _tempMat1, 1, 2*nPixels, CV_32FC1,_tempMat->data.ptr, _tempMat->step );
547 _tempMat2 = cvCreateMatHeader( 1,2*nPixels, CV_32FC1);
548 cvInitMatHeader( _tempMat2, 1, 2*nPixels, CV_32FC1, _tempMat->data.ptr+_tempMat->step*1, _tempMat->step );
550 _tempMat3 = cvCreateMatHeader( 1,2*nPixels, CV_32FC1);
551 cvInitMatHeader( _tempMat3, 1, 2*nPixels, CV_32FC1, _tempMat->data.ptr+_tempMat->step*2, _tempMat->step );
553 _p2Mat1 = cvCreateMatHeader( 1,2*nPixels, CV_32FC1);
554 cvInitMatHeader( _p2Mat1, 1, 2*nPixels, CV_32FC1, _points2Mat->data.ptr );
555 _p2Mat3 = cvCreateMatHeader( 1,2*nPixels, CV_32FC1);
556 cvInitMatHeader( _p2Mat3, 1, 2*nPixels, CV_32FC1, _points2Mat->data.ptr+_points2Mat->step*2, _points2Mat->step );
558 _drawingMat=cvCreateMat(3, 2*nPixels, CV_32FC1);
559 _projectionMat=cvCreateMat(2, 3, CV_32FC1);
561 _xyzMat1 = cvCreateMatHeader(1,2*nPixels,CV_32FC1);
562 _xyzMat2 = cvCreateMatHeader(1,2*nPixels,CV_32FC1);
563 _xyzMat3 = cvCreateMatHeader(1,2*nPixels,CV_32FC1);
594 _yarpImage = _inputVideoPort.read();
595 _inputVideoPort.getEnvelope(_yarpTimestamp);
597 if (_yarpImage != NULL)
599 widthRatio=(double)_yarpImage->width()/(double)_calibrationImageWidth;
600 heightRatio=(double)_yarpImage->height()/(double)_calibrationImageHeight;
601 _perspectiveFx=_perspectiveFx*(float)widthRatio;
602 _perspectiveFy=_perspectiveFy*(float)heightRatio;
603 _perspectiveCx=_perspectiveCx*(float)widthRatio;
604 _perspectiveCy=_perspectiveCy*(float)heightRatio;
606 _rawImage = cvCreateImage(cvSize(_yarpImage->width(),_yarpImage->height()),IPL_DEPTH_8U, 3);
607 _transformedImage = cvCreateImage(cvSize(_yarpImage->width(),_yarpImage->height()),IPL_DEPTH_8U, 3);
608 toCvMat(*_yarpImage).copyTo(cv::cvarrToMat(_rawImage));
610 rgbToYuvBinImageLut(_rawImage,_transformedImage,_lut);
613 _transformedImage = cvCreateImage(cvSize(_yarpImage->width(),_yarpImage->height()),IPL_DEPTH_8U,3);
615 _framesNotTracking=0;
619 _lastU=_yarpImage->width()/2.0F;
620 _lastV=_yarpImage->height()/2.0F;
629 yWarning(
"There were problems initializing the object: the execution was interrupted.");
634 _doneInitializing=
true;
640 bool PF3DTracker::close()
642 _inputVideoPort.close();
643 _outputVideoPort.close();
644 _outputDataPort.close();
645 _outputUVDataPort.close();
646 _inputParticlePort.close();
647 _outputParticlePort.close();
648 _outputAttentionPort.close();
653 if (_particles != NULL)
654 cvReleaseMat(&_particles);
656 if (_newParticles != NULL)
657 cvReleaseMat(&_newParticles);
663 bool PF3DTracker::interruptModule()
665 _inputVideoPort.interrupt();
666 _outputVideoPort.interrupt();
667 _outputDataPort.interrupt();
668 _outputUVDataPort.interrupt();
669 _inputParticlePort.interrupt();
670 _outputParticlePort.interrupt();
671 _outputAttentionPort.interrupt();
679 bool PF3DTracker::updateModule()
681 if(_doneInitializing)
685 float likelihood, mean, maxX, maxY, maxZ;
686 float weightedMeanX, weightedMeanY, weightedMeanZ;
690 string outputFileName;
695 _finalTime=yarp::os::Time::now();
696 wholeCycle=(float)(_finalTime-_initialTime);
697 _initialTime=yarp::os::Time::now();
702 float sumLikelihood=0.0;
703 float maxLikelihood=0.0;
705 for(count=0;count< _nParticles;count++)
707 if(_colorTransfPolicy==0)
709 evaluateHypothesisPerspective(_model3dPointsMat,(
float)cvmGet(_particles,0,count),(
float)cvmGet(_particles,1,count),(
float)cvmGet(_particles,2,count),_modelHistogramMat,_transformedImage,_perspectiveFx,_perspectiveFy, _perspectiveCx,_perspectiveCy,_inside_outside_difference_weight,likelihood);
713 if(_colorTransfPolicy==1)
715 evaluateHypothesisPerspectiveFromRgbImage(_model3dPointsMat,(
float)cvmGet(_particles,0,count),(
float)cvmGet(_particles,1,count),(
float)cvmGet(_particles,2,count),_modelHistogramMat,_rawImage,_perspectiveFx,_perspectiveFy, _perspectiveCx,_perspectiveCy,_inside_outside_difference_weight,likelihood);
719 yWarning() <<
"Wrong ID for color transformation policy:"<<_colorTransfPolicy<<
". Quitting.";
724 cvmSet(_particles,6,count,likelihood);
725 sumLikelihood+=likelihood;
726 if(likelihood>maxLikelihood)
728 maxLikelihood=likelihood;
736 maxX=(float)cvmGet(_particles,0,maxIndex);
737 maxY=(float)cvmGet(_particles,1,maxIndex);
738 maxZ=(float)cvmGet(_particles,2,maxIndex);
747 if(maxLikelihood/exp((
float)20.0)>_likelihoodThreshold)
750 _framesNotTracking=0;
751 _attentionOutput=_attentionOutputMax;
755 _attentionOutput=_attentionOutput*_attentionOutputDecrease;
757 _framesNotTracking+=1;
784 if(_framesNotTracking==5 || sumLikelihood==0.0)
786 cout<<
"**********************************************************************Reset\n";
787 float mean,velocityStDev;
790 mean=(float)_initialX;
791 cvRandArr( &rngState, _particles1, CV_RAND_NORMAL, cvScalar(mean), cvScalar(_accelStDev));
793 mean=(float)_initialY;
794 cvRandArr( &rngState, _particles2, CV_RAND_NORMAL, cvScalar(mean), cvScalar(_accelStDev));
796 mean=(float)_initialZ;
797 cvRandArr( &rngState, _particles3, CV_RAND_NORMAL, cvScalar(mean), cvScalar(_accelStDev));
800 cvRandArr( &rngState, _particles4, CV_RAND_NORMAL, cvScalar(mean), cvScalar(velocityStDev));
802 cvRandArr( &rngState, _particles5, CV_RAND_NORMAL, cvScalar(mean), cvScalar(velocityStDev));
804 cvRandArr( &rngState, _particles6, CV_RAND_NORMAL, cvScalar(mean), cvScalar(velocityStDev));
806 _framesNotTracking=0;
808 weightedMeanX=weightedMeanY=weightedMeanZ=0.0;
809 for(count=0;count<_nParticles;count++)
811 weightedMeanX+=(float)cvmGet(_particles,0,count);
812 weightedMeanY+=(float)cvmGet(_particles,1,count);
813 weightedMeanZ+=(float)cvmGet(_particles,2,count);
815 weightedMeanX/=_nParticles;
816 weightedMeanY/=_nParticles;
817 weightedMeanZ/=_nParticles;
824 cout<<setw(8)<<_frameCounter;
825 cout<<setiosflags(ios::fixed)<<setprecision(3)<<setw(8)<<weightedMeanX/1000;
826 cout<<
" "<<setw(8)<<weightedMeanY/1000;
827 cout<<
" "<<setiosflags(ios::fixed)<<setprecision(3)<<setw(8)<<weightedMeanZ/1000;
828 cout<<
" "<<setiosflags(ios::fixed)<<setprecision(5)<<setw(8)<<maxLikelihood/exp((
float)20.0);
829 cout<<
" "<<setw(5)<<_seeingObject;
839 for(count=0;count<_nParticles;count++)
841 cvmSet(_particles,6,count,(cvmGet(_particles,6,count)/sumLikelihood));
842 weightedMeanX+=(float)(cvmGet(_particles,0,count)*cvmGet(_particles,6,count));
843 weightedMeanY+=(float)(cvmGet(_particles,1,count)*cvmGet(_particles,6,count));
844 weightedMeanZ+=(float)(cvmGet(_particles,2,count)*cvmGet(_particles,6,count));
857 cout<<setw(8)<<_frameCounter;
858 cout<<setiosflags(ios::fixed)<<setprecision(3)<<setw(8)<<weightedMeanX/1000;
859 cout<<
" "<<setw(8)<<weightedMeanY/1000;
860 cout<<
" "<<setiosflags(ios::fixed)<<setprecision(3)<<setw(8)<<weightedMeanZ/1000;
861 cout<<
" "<<setiosflags(ios::fixed)<<setprecision(5)<<setw(8)<<maxLikelihood/exp((
float)20.0);
862 cout<<
" "<<setw(5)<<_seeingObject;
865 Bottle *particleInput = _inputParticlePort.read(
false);
866 if (particleInput==NULL)
867 _numParticlesReceived=0;
869 _numParticlesReceived=(particleInput->get(0)).asInt32();
870 if(_numParticlesReceived > _nParticles)
872 _numParticlesReceived=0;
873 yWarning(
"PROBLEM: Input particles are more than nParticles.");
880 int minimum_likelihood=10;
882 if(maxLikelihood>minimum_likelihood)
886 systematic_resampling(_particles1to6,_particles7,_newParticles,_cumWeight);
893 cvCopy(_particles,_newParticles);
900 cvMatMul(_A,_newParticles,_particles);
909 cvRandArr( &rngState, _noise1, CV_RAND_NORMAL, cvScalar(mean), cvScalar(_accelStDev));
912 cvCopy(_noise1,_noise2);
913 cvConvertScale( _noise1, _noise1, 0.5, 0 );
919 cvAdd(_particles1to6,_noise,_particles1to6);
924 if(_numParticlesReceived > 0){
925 int topdownParticles = _nParticles - _numParticlesReceived;
926 for(count=0 ; count<_numParticlesReceived ; count++){
927 cvmSet(_particles,0,topdownParticles+count, (particleInput->get(1+count*3+0)).asFloat64());
928 cvmSet(_particles,1,topdownParticles+count, (particleInput->get(1+count*3+1)).asFloat64());
929 cvmSet(_particles,2,topdownParticles+count, (particleInput->get(1+count*3+2)).asFloat64());
930 cvmSet(_particles,3,topdownParticles+count, 0);
931 cvmSet(_particles,4,topdownParticles+count, 0);
932 cvmSet(_particles,5,topdownParticles+count, 0);
933 cvmSet(_particles,6,topdownParticles+count, 0.8);
944 if(_circleVisualizationMode==0)
946 drawSampledLinesPerspectiveYARP(_model3dPointsMat, weightedMeanX,weightedMeanY,weightedMeanZ, _yarpImage,_perspectiveFx, _perspectiveFy, _perspectiveCx, _perspectiveCy, 255, 255, 255, meanU, meanV);
948 if(_circleVisualizationMode==1)
951 drawContourPerspectiveYARP(_visualization3dPointsMat, weightedMeanX,weightedMeanY,weightedMeanZ, _yarpImage,_perspectiveFx, _perspectiveFy, _perspectiveCx, _perspectiveCy, 0, 255, 0, meanU, meanV);
953 drawContourPerspectiveYARP(_visualization3dPointsMat, weightedMeanX,weightedMeanY,weightedMeanZ, _yarpImage,_perspectiveFx, _perspectiveFy, _perspectiveCx, _perspectiveCy, 255,255, 0, meanU, meanV);
971 cout<<setw(8)<<(int)meanU;
972 cout<<setw(5)<<(int)meanV;
973 if(_firstFrame==
false)
975 cout<<setw(5)<<setw(8)<<setiosflags(ios::fixed)<<setprecision(3)<<wholeCycle<<endl;
979 cout<<
" -----"<<endl;
983 Bottle& output=_outputDataPort.prepare();
985 output.addFloat64(weightedMeanX/1000);
986 output.addFloat64(weightedMeanY/1000);
987 output.addFloat64(weightedMeanZ/1000);
988 output.addFloat64(maxLikelihood/exp((
float)20.0));
989 output.addFloat64(meanU);
990 output.addFloat64(meanV);
991 output.addFloat64(_seeingObject);
994 _outputDataPort.setEnvelope(_yarpTimestamp);
996 _outputDataPort.write();
998 if (_seeingObject && supplyUVdata)
1000 Bottle& outputUV=_outputUVDataPort.prepare();
1003 outputUV.addFloat64(meanU);
1004 outputUV.addFloat64(meanV);
1007 _outputUVDataPort.setEnvelope(_yarpTimestamp);
1008 _outputUVDataPort.write();
1011 Vector& tempVector=_outputAttentionPort.prepare();
1012 tempVector.resize(5);
1013 if(maxLikelihood>_likelihoodThreshold)
1015 tempVector(0) = (meanU-_rawImage->width/2)/(_rawImage->width/2)/1.5;
1016 tempVector(1) = (meanV-_rawImage->height/2)/(_rawImage->height/2)/1.5;
1022 tempVector(0) = ((_lastU-_rawImage->width/2)/(_rawImage->width/2));
1023 tempVector(1) = ((_lastV-_rawImage->height/2)/(_rawImage->height/2));
1028 tempVector(4) = _attentionOutput;
1031 _outputAttentionPort.setEnvelope(_yarpTimestamp);
1032 _outputAttentionPort.write();
1039 if(_saveImagesWithOpencv)
1041 if(_frameCounter<1000) out << 0;
1042 if(_frameCounter<100) out << 0;
1043 if(_frameCounter<10) out << 0;
1044 out << _frameCounter;
1045 outputFileName=_saveImagesWithOpencvDir+out.str()+
".jpeg";
1046 toCvMat(*_yarpImage).copyTo(cv::cvarrToMat(_rawImage));
1047 imwrite(outputFileName, cv::cvarrToMat(_rawImage));
1051 cv::Mat tmpMat=toCvMat(*_yarpImage);
1052 cvtColor(tmpMat,tmpMat,CV_BGR2RGB);
1053 _outputVideoPort.prepare() = fromCvMat<PixelRgb>(tmpMat);
1056 _outputVideoPort.setEnvelope(_yarpTimestamp);
1057 _outputVideoPort.write();
1064 _yarpImage = _inputVideoPort.read();
1065 _inputVideoPort.getEnvelope(_yarpTimestamp);
1067 toCvMat(*_yarpImage).copyTo(cv::cvarrToMat(_rawImage));
1072 if(_colorTransfPolicy==0)
1074 rgbToYuvBinImageLut(_rawImage,_transformedImage,_lut);
1083 double PF3DTracker::getPeriod()
1088 void PF3DTracker::drawSampledLinesPerspectiveYARP(CvMat* model3dPointsMat,
float x,
float y,
float z, ImageOf<PixelRgb> *image,
float _perspectiveFx,
float _perspectiveFy ,
float _perspectiveCx,
float _perspectiveCy ,
int R,
int G,
int B,
float &meanU,
float &meanV)
1095 cvCopy(model3dPointsMat,_drawingMat);
1100 failure=place3dPointsPerspective(_drawingMat,x,y,z);
1107 failure= perspective_projection(_drawingMat, _perspectiveFx, _perspectiveFy, _perspectiveCx, _perspectiveCy, _uv)!=0;
1110 yWarning(
"I had troubles projecting the points.");
1114 int conta,uPosition,vPosition;
1117 for(conta=0;conta<nPixels;conta++)
1119 meanU=meanU+((
float*)(_uv->data.ptr + _uv->step*0))[conta];
1120 meanV=meanV+((
float*)(_uv->data.ptr + _uv->step*1))[conta];
1122 vPosition= (int)((
float*)(_uv->data.ptr + _uv->step*1))[conta];
1123 uPosition= (int)((
float*)(_uv->data.ptr + _uv->step*0))[conta];
1124 if((uPosition<_rawImage->width)&&(uPosition>=0)&&(vPosition<_rawImage->height)&&(vPosition>=0))
1126 image->pixel(uPosition,vPosition)= PixelRgb(B,G,R);
1128 vPosition= (int)((
float*)(_uv->data.ptr + _uv->step*1))[conta+nPixels];
1129 uPosition= (int)((
float*)(_uv->data.ptr + _uv->step*0))[conta+nPixels];
1130 if((uPosition<_rawImage->width)&&(uPosition>=0)&&(vPosition<_rawImage->height)&&(vPosition>=0))
1132 image->pixel(uPosition,vPosition)= PixelRgb(B,G,R);
1137 meanU=floor(meanU/nPixels);
1138 meanV=floor(meanV/nPixels);
1139 if((meanU<_rawImage->width)&&(meanU>=0)&&(meanV<_rawImage->height)&&(meanV>=0))
1141 image->pixel((
int)meanU,(
int)meanV)= PixelRgb(B,G,R);
1146 void PF3DTracker::drawContourPerspectiveYARP(CvMat* model3dPointsMat,
float x,
float y,
float z, ImageOf<PixelRgb> *image,
float _perspectiveFx,
float _perspectiveFy ,
float _perspectiveCx,
float _perspectiveCy ,
int R,
int G,
int B,
float &meanU,
float &meanV)
1153 cvCopy(model3dPointsMat,_drawingMat);
1158 failure=place3dPointsPerspective(_drawingMat,x,y,z);
1165 failure= perspective_projection(_drawingMat, _perspectiveFx, _perspectiveFy, _perspectiveCx, _perspectiveCy, _uv)!=0;
1168 yWarning(
"I had troubles projecting the points.");
1174 int conta,cippa,lippa,uPosition,vPosition;
1177 for(conta=0;conta<nPixels;conta++)
1179 meanV=meanV+((
float*)(_uv->data.ptr + _uv->step*1))[conta];
1180 meanU=meanU+((
float*)(_uv->data.ptr + _uv->step*0))[conta];
1182 for(lippa=-2;lippa<3;lippa++)
1183 for(cippa=-2;cippa<3;cippa++)
1185 vPosition= (int)(((
float*)(_uv->data.ptr + _uv->step*1))[conta])+lippa-1;
1186 uPosition= (int)(((
float*)(_uv->data.ptr + _uv->step*0))[conta])+cippa-1;
1188 if((uPosition<_rawImage->width)&&(uPosition>=0)&&(vPosition<_rawImage->height)&&(vPosition>=0))
1190 image->pixel(uPosition,vPosition)= PixelRgb(B,G,R);
1196 meanU=floor(meanU/nPixels);
1197 meanV=floor(meanV/nPixels);
1198 if((meanU<_rawImage->width)&&(meanU>=0)&&(meanV<_rawImage->height)&&(meanV>=0))
1200 image->pixel((
int)meanU,(
int)meanV)= PixelRgb(B,G,R);
1205 bool PF3DTracker::computeTemplateHistogram(
string imageFileName,
string dataFileName)
1214 int sizes[3]={YBins,UBins,VBins};
1216 CvMatND* histogram=cvCreateMatND(dimensions, sizes, CV_32FC1);
1219 yWarning(
"computeTemplateHistogram: I wasn\'t able to allocate memory for histogram.");
1223 cvSetZero(histogram);
1225 auto rawImage = cv::imread(imageFileName);
1226 if( ! rawImage.data)
1228 yWarning(
"I wasn't able to open the image file!");
1231 cv::Mat transformedImage(rawImage.rows, rawImage.cols, CV_8UC3);
1237 rgbToYuvBinMatLut(rawImage,transformedImage,_lut);
1240 for(v=0;v<rawImage.rows;v++)
1241 for(u=0;u<rawImage.cols;u++)
1246 (((uchar*)(rawImage.data + rawImage.step*v))[u*3+0])==255 && (((uchar*)(rawImage.data + rawImage.step*v))[u*3+1])==255 && (((uchar*)(rawImage.data + rawImage.step*v))[u*3+2])==255)
1251 a=(((uchar*)(transformedImage.data + transformedImage.step*v))[u*3+0]);
1252 b=(((uchar*)(transformedImage.data + transformedImage.step*v))[u*3+1]);
1253 c=(((uchar*)(transformedImage.data + transformedImage.step*v))[u*3+2]);
1258 *((
float*)(histogram->data.ptr + a*histogram->dim[0].step + b*histogram->dim[1].step + c*histogram->dim[2].step)) +=1;
1270 cvConvertScale( histogram, histogram, 1/usedPoints, 0 );
1274 ofstream fout(dataFileName.c_str());
1277 yWarning(
"computeTemplateHistogram: unable to open the csv file to store the histogram.");
1282 for(a=0;a<YBins;a++)
1284 for(b=0;b<UBins;b++)
1286 for(c=0;c<VBins;c++)
1288 fout<<*((
float*)(histogram->data.ptr + a*histogram->dim[0].step + b*histogram->dim[1].step + c*histogram->dim[2].step))<<endl;
1296 if (histogram != NULL)
1297 cvReleaseMatND(&histogram);
1303 bool PF3DTracker::readModelHistogram(CvMatND* histogram,
const char fileName[])
1308 ifstream fin(fileName);
1311 yWarning(
"unable to open the csv histogram file.");
1316 for(c1=0;c1<YBins;c1++)
1317 for(c2=0;c2<UBins;c2++)
1318 for(c3=0;c3<VBins;c3++)
1320 fin.getline(line, 14);
1321 *((
float*)(histogram->data.ptr + c1*histogram->dim[0].step + c2*histogram->dim[1].step + c3*histogram->dim[2].step))=(
float)atof(line);
1327 bool PF3DTracker::readInitialmodel3dPoints(CvMat* points,
string fileName)
1332 ifstream fin(fileName.c_str());
1335 yWarning(
"unable to open the the 3D model file.");
1341 for(c2=0;c2<2*nPixels;c2++)
1343 fin.getline(line, 14);
1344 ((
float*)(points->data.ptr + points->step*c1))[c2]=(
float)atof(line);
1350 bool PF3DTracker::readMotionModelMatrix(CvMat* points,
string fileName)
1355 ifstream fin(fileName.c_str());
1358 yWarning(
"unable to open the motion model file.");
1366 fin.getline(line, 14);
1367 cvmSet(points,c1,c2,atof(line));
1373 bool PF3DTracker::evaluateHypothesisPerspective(CvMat* model3dPointsMat,
float x,
float y,
float z, CvMatND* modelHistogramMat, IplImage* transformedImage,
float fx,
float fy,
float u0,
float v0,
float inside_outside,
float &likelihood)
1377 float usedOuterPoints, usedInnerPoints;
1380 cvCopy(model3dPointsMat,_drawingMat);
1385 failure=place3dPointsPerspective(_drawingMat,x,y,z);
1390 failure= perspective_projection(_drawingMat, _perspectiveFx, _perspectiveFy, _perspectiveCx, _perspectiveCy, _uv)!=0;
1393 yWarning(
"I had troubles projecting the points.");
1396 computeHistogram(_uv, transformedImage, _innerHistogramMat, usedInnerPoints, _outerHistogramMat, usedOuterPoints);
1400 failure=calculateLikelihood(_modelHistogramMat, _innerHistogramMat,_outerHistogramMat, inside_outside,likelihood);
1402 likelihood=exp(20*likelihood);
1405 likelihood=likelihood*((float)usedInnerPoints/nPixels)*((float)usedInnerPoints/nPixels)*((float)usedOuterPoints/nPixels)*((float)usedOuterPoints/nPixels);
1410 bool PF3DTracker::evaluateHypothesisPerspectiveFromRgbImage(CvMat* model3dPointsMat,
float x,
float y,
float z, CvMatND* modelHistogramMat, IplImage *image,
float fx,
float fy,
float u0,
float v0,
float inside_outside,
float &likelihood)
1415 float usedOuterPoints, usedInnerPoints;
1418 cvCopy(model3dPointsMat,_drawingMat);
1423 failure=place3dPointsPerspective(_drawingMat,x,y,z);
1428 failure= perspective_projection(_drawingMat, _perspectiveFx, _perspectiveFy, _perspectiveCx, _perspectiveCy, _uv)!=0;
1431 yWarning(
"I had troubles projecting the points.");
1434 computeHistogramFromRgbImage(_uv, image, _innerHistogramMat, usedInnerPoints, _outerHistogramMat, usedOuterPoints);
1438 failure=calculateLikelihood(_modelHistogramMat, _innerHistogramMat, _outerHistogramMat, inside_outside,likelihood);
1440 likelihood=exp(20*likelihood);
1443 likelihood=likelihood*((float)usedInnerPoints/nPixels)*((float)usedInnerPoints/nPixels)*((float)usedOuterPoints/nPixels)*((float)usedOuterPoints/nPixels);
1448 bool PF3DTracker::systematic_resampling(CvMat* oldParticlesState, CvMat* oldParticlesWeights, CvMat* newParticlesState, CvMat* cumWeight)
1458 int numParticlesToGenerate = _nParticles - _numParticlesReceived;
1467 for(c1=0;c1<_nParticles;c1++)
1469 sum+=((
float*)(oldParticlesWeights->data.ptr + oldParticlesWeights->step*0))[c1];
1471 for(c1=0;c1<_nParticles;c1++)
1473 ((
float*)(oldParticlesWeights->data.ptr + oldParticlesWeights->step*0))[c1] = (((
float*)(oldParticlesWeights->data.ptr + oldParticlesWeights->step*0))[c1])/(
float)sum;
1478 u=1/(double)numParticlesToGenerate*((
double)rand()/(double)RAND_MAX);
1487 ((
float*)(cumWeight->data.ptr))[0]=0;
1488 for(c1=0;c1<_nParticles;c1++)
1492 ((
float*)(cumWeight->data.ptr))[c1+1]=((
float*)(cumWeight->data.ptr))[c1]+((
float*)(oldParticlesWeights->data.ptr + oldParticlesWeights->step*0))[c1];
1498 if(((
float*)(cumWeight->data.ptr))[_nParticles]!=1)
1502 ((
float*)(cumWeight->data.ptr))[_nParticles]=1;
1503 if( ((
float*)(cumWeight->data.ptr))[_nParticles]!=1)
1521 while(npIndex < numParticlesToGenerate)
1524 if(((
float*)(cumWeight->data.ptr))[cIndex]>=(double)rIndex/(
double)numParticlesToGenerate+u)
1529 ((
float*)(newParticlesState->data.ptr + newParticlesState->step*0))[npIndex]=((
float*)(oldParticlesState->data.ptr + oldParticlesState->step*0))[cIndex-1];
1530 ((
float*)(newParticlesState->data.ptr + newParticlesState->step*1))[npIndex]=((
float*)(oldParticlesState->data.ptr + oldParticlesState->step*1))[cIndex-1];
1531 ((
float*)(newParticlesState->data.ptr + newParticlesState->step*2))[npIndex]=((
float*)(oldParticlesState->data.ptr + oldParticlesState->step*2))[cIndex-1];
1532 ((
float*)(newParticlesState->data.ptr + newParticlesState->step*3))[npIndex]=((
float*)(oldParticlesState->data.ptr + oldParticlesState->step*3))[cIndex-1];
1533 ((
float*)(newParticlesState->data.ptr + newParticlesState->step*4))[npIndex]=((
float*)(oldParticlesState->data.ptr + oldParticlesState->step*4))[cIndex-1];
1534 ((
float*)(newParticlesState->data.ptr + newParticlesState->step*5))[npIndex]=((
float*)(oldParticlesState->data.ptr + oldParticlesState->step*5))[cIndex-1];
1535 ((
float*)(newParticlesState->data.ptr + newParticlesState->step*6))[npIndex]=0;
1549 bool PF3DTracker::systematicR(CvMat* inState, CvMat* weights, CvMat* outState)
1551 float N=(float)_nParticles;
1556 cvCopy(_ramp,_label);
1563 T=s*((float)rand()/(float)RAND_MAX);
1569 cvRandArr( &rngState, _u, CV_RAND_UNI, cvScalar(0), cvScalar(1));
1571 while((T<1) && (i<_nParticles))
1574 if((Q>T) && (i<_nParticles))
1579 ((
float*)(_nChildren->data.ptr))[li-1]+=1;
1583 i=(int)(floor((N-j+1)*(((
float*)(_u->data.ptr))[j-1]))+j);
1585 auxw=((
float*)(weights->data.ptr))[i-1];
1587 li=(int)((
float*)(_label->data.ptr))[i-1];
1590 ((
float*)(weights->data.ptr))[i-1]=((
float*)(weights->data.ptr))[j-1];
1591 ((
float*)(_label->data.ptr))[i-1] = ((
float*)(_label->data.ptr))[j-1];
1600 if(((
float*)(_nChildren->data.ptr))[i]>0)
1602 for(j=index;(j<index+((
float*)(_nChildren->data.ptr))[i])&&(j<_nParticles+1);j++)
1604 ((
float*)(outState->data.ptr + outState->step*0))[j-1]=((
float*)(inState->data.ptr + inState->step*0))[i];
1605 ((
float*)(outState->data.ptr + outState->step*1))[j-1]=((
float*)(inState->data.ptr + inState->step*1))[i];
1606 ((
float*)(outState->data.ptr + outState->step*2))[j-1]=((
float*)(inState->data.ptr + inState->step*2))[i];
1607 ((
float*)(outState->data.ptr + outState->step*3))[j-1]=((
float*)(inState->data.ptr + inState->step*3))[i];
1608 ((
float*)(outState->data.ptr + outState->step*4))[j-1]=((
float*)(inState->data.ptr + inState->step*4))[i];
1609 ((
float*)(outState->data.ptr + outState->step*5))[j-1]=((
float*)(inState->data.ptr + inState->step*5))[i];
1612 index=index+(int)((
float*)(_nChildren->data.ptr))[i];
1618 bool PF3DTracker::place3dPointsPerspective(CvMat* points,
float x,
float y,
float z)
1623 float floorDistance=sqrt(x*x+y*y);
1624 float distance=sqrt(x*x+y*y+z*z);
1626 float cosAlpha=floorDistance/distance;
1627 float sinAlpha=-z/distance;
1628 float cosBeta=x/floorDistance;
1629 float sinBeta=y/floorDistance;
1632 ((
float*)(_rzMat->data.ptr + _rzMat->step*0))[0]= cosBeta;
1633 ((
float*)(_rzMat->data.ptr + _rzMat->step*0))[1]= -sinBeta;
1634 ((
float*)(_rzMat->data.ptr + _rzMat->step*0))[2]= 0;
1635 ((
float*)(_rzMat->data.ptr + _rzMat->step*1))[0]= sinBeta;
1636 ((
float*)(_rzMat->data.ptr + _rzMat->step*1))[1]= cosBeta;
1637 ((
float*)(_rzMat->data.ptr + _rzMat->step*1))[2]= 0;
1638 ((
float*)(_rzMat->data.ptr + _rzMat->step*2))[0]= 0;
1639 ((
float*)(_rzMat->data.ptr + _rzMat->step*2))[1]= 0;
1640 ((
float*)(_rzMat->data.ptr + _rzMat->step*2))[2]= 1;
1643 ((
float*)(_ryMat->data.ptr + _ryMat->step*0))[0]= cosAlpha;
1644 ((
float*)(_ryMat->data.ptr + _ryMat->step*0))[1]= 0;
1645 ((
float*)(_ryMat->data.ptr + _ryMat->step*0))[2]= sinAlpha;
1646 ((
float*)(_ryMat->data.ptr + _ryMat->step*1))[0]= 0;
1647 ((
float*)(_ryMat->data.ptr + _ryMat->step*1))[1]= 1;
1648 ((
float*)(_ryMat->data.ptr + _ryMat->step*1))[2]= 0;
1649 ((
float*)(_ryMat->data.ptr + _ryMat->step*2))[0]= -sinAlpha;
1650 ((
float*)(_ryMat->data.ptr + _ryMat->step*2))[1]= 0;
1651 ((
float*)(_ryMat->data.ptr + _ryMat->step*2))[2]= cosAlpha;
1658 cvMatMul(_ryMat,points,_points2Mat);
1664 cvSet(_tempMat1,cvScalar(floorDistance));
1665 cvAdd(_p2Mat1,_tempMat1,_p2Mat1);
1668 cvSet(_tempMat3,cvScalar(z));
1669 cvAdd(_p2Mat3,_tempMat3,_p2Mat3);
1676 cvMatMul(_rzMat,_points2Mat,points);
1682 int PF3DTracker::perspective_projection(CvMat* xyz,
float fx,
float fy,
float cx,
float cy, CvMat* uv)
1685 ((
float*)(_projectionMat->data.ptr + _projectionMat->step*0))[0]= fx;
1686 ((
float*)(_projectionMat->data.ptr + _projectionMat->step*0))[1]= 0;
1687 ((
float*)(_projectionMat->data.ptr + _projectionMat->step*0))[2]= cx;
1688 ((
float*)(_projectionMat->data.ptr + _projectionMat->step*1))[0]= 0;
1689 ((
float*)(_projectionMat->data.ptr + _projectionMat->step*1))[1]= fy;
1690 ((
float*)(_projectionMat->data.ptr + _projectionMat->step*1))[2]= cy;
1720 cvInitMatHeader( _xyzMat1, 1, 2*nPixels, CV_32FC1, xyz->data.ptr );
1721 cvInitMatHeader( _xyzMat2, 1, 2*nPixels, CV_32FC1, xyz->data.ptr + xyz->step*1);
1722 cvInitMatHeader( _xyzMat3, 1, 2*nPixels, CV_32FC1, xyz->data.ptr + xyz->step*2);
1725 cvDiv( _xyzMat1, _xyzMat3, _xyzMat1, 1 );
1739 cvDiv( _xyzMat2, _xyzMat3, _xyzMat2, 1 );
1753 cvSet(_xyzMat3,(cvScalar(1)));
1769 cvMatMul(_projectionMat,xyz,uv);
1785 bool PF3DTracker::computeHistogram(CvMat* uv, IplImage* transformedImage, CvMatND* innerHistogramMat,
float &usedInnerPoints, CvMatND* outerHistogramMat,
float &usedOuterPoints)
1791 cvSetZero(innerHistogramMat);
1793 for(count=0;count<nPixels;count++)
1795 u=(int)((
float*)(uv->data.ptr + uv->step*0))[count];
1796 v=(int)((
float*)(uv->data.ptr + uv->step*1))[count];
1797 if((v<transformedImage->height)&&(v>=0)&&(u<transformedImage->width)&&(u>=0))
1799 a=(((uchar*)(transformedImage->imageData + transformedImage->widthStep*v))[u*3+0]);
1800 b=(((uchar*)(transformedImage->imageData + transformedImage->widthStep*v))[u*3+1]);
1801 c=(((uchar*)(transformedImage->imageData + transformedImage->widthStep*v))[u*3+2]);
1802 *((
float*)(innerHistogramMat->data.ptr + a*innerHistogramMat->dim[0].step + b*innerHistogramMat->dim[1].step + c*innerHistogramMat->dim[2].step)) +=1;
1807 if(usedInnerPoints>0)
1809 cvConvertScale(innerHistogramMat, innerHistogramMat, 1/usedInnerPoints, 0 );
1813 cvSetZero(outerHistogramMat);
1814 for(count=nPixels;count<2*nPixels;count++)
1816 u=(int)((
float*)(uv->data.ptr + uv->step*0))[count];
1817 v=(int)((
float*)(uv->data.ptr + uv->step*1))[count];
1818 if((v<transformedImage->height)&&(v>=0)&&(u<transformedImage->width)&&(u>=0))
1820 a=(((uchar*)(transformedImage->imageData + transformedImage->widthStep*v))[u*3+0]);
1821 b=(((uchar*)(transformedImage->imageData + transformedImage->widthStep*v))[u*3+1]);
1822 c=(((uchar*)(transformedImage->imageData + transformedImage->widthStep*v))[u*3+2]);
1823 *((
float*)(outerHistogramMat->data.ptr + a*outerHistogramMat->dim[0].step + b*outerHistogramMat->dim[1].step + c*outerHistogramMat->dim[2].step)) +=1;
1827 if(usedOuterPoints>0)
1829 cvConvertScale(outerHistogramMat, outerHistogramMat, 1/usedOuterPoints, 0 );
1835 bool PF3DTracker::computeHistogramFromRgbImage(CvMat* uv, IplImage *image, CvMatND* innerHistogramMat,
float &usedInnerPoints, CvMatND* outerHistogramMat,
float &usedOuterPoints)
1846 cvZero(innerHistogramMat);
1847 for(count=0;count<nPixels;count++)
1849 u=(int)((
float*)(uv->data.ptr + uv->step*0))[count];
1850 v=(int)((
float*)(uv->data.ptr + uv->step*1))[count];
1851 if((v<image->height)&&(v>=0)&&(u<image->width)&&(u>=0))
1854 r=(((uchar*)(image->imageData + image->widthStep*v))[u*3+0]);
1855 g=(((uchar*)(image->imageData + image->widthStep*v))[u*3+1]);
1856 b=(((uchar*)(image->imageData + image->widthStep*v))[u*3+2]);
1857 index=r*65536+g*256+b;
1859 *((
float*)(innerHistogramMat->data.ptr + _lut[index].y*innerHistogramMat->dim[0].step + _lut[index].u*innerHistogramMat->dim[1].step + _lut[index].v*innerHistogramMat->dim[2].step)) +=1;
1865 if(usedInnerPoints>0)
1867 cvConvertScale( innerHistogramMat, innerHistogramMat, 1/usedInnerPoints, 0 );
1874 cvZero(outerHistogramMat);
1875 for(count=nPixels;count<2*nPixels;count++)
1877 u=(int)((
float*)(uv->data.ptr + uv->step*0))[count];
1878 v=(int)((
float*)(uv->data.ptr + uv->step*1))[count];
1879 if((v<image->height)&&(v>=0)&&(u<image->width)&&(u>=0))
1882 r=(((uchar*)(image->imageData + image->widthStep*v))[u*3+0]);
1883 g=(((uchar*)(image->imageData + image->widthStep*v))[u*3+1]);
1884 b=(((uchar*)(image->imageData + image->widthStep*v))[u*3+2]);
1885 index=r*65536+g*256+b;
1887 *((
float*)(outerHistogramMat->data.ptr + _lut[index].y*outerHistogramMat->dim[0].step + _lut[index].u*outerHistogramMat->dim[1].step + _lut[index].v*outerHistogramMat->dim[2].step)) +=1;
1891 if(usedOuterPoints>0)
1893 cvConvertScale( outerHistogramMat, outerHistogramMat, 1/usedOuterPoints, 0 );
1899 bool PF3DTracker::calculateLikelihood(CvMatND* templateHistogramMat, CvMatND* innerHistogramMat, CvMatND* outerHistogramMat,
float inside_outside,
float &likelihood)
1903 for(a=0;a<YBins;a++)
1904 for(b=0;b<UBins;b++)
1905 for(c=0;c<VBins;c++)
1907 likelihood=likelihood + sqrt(
1908 *((
float*)(innerHistogramMat->data.ptr + a*innerHistogramMat->dim[0].step + b*innerHistogramMat->dim[1].step + c*innerHistogramMat->dim[2].step)) *
1909 *((
float*)(templateHistogramMat->data.ptr + a*templateHistogramMat->dim[0].step + b*templateHistogramMat->dim[1].step + c*templateHistogramMat->dim[2].step)))
1910 - _inside_outside_difference_weight*sqrt(
1911 *((
float*)(outerHistogramMat->data.ptr + a*outerHistogramMat->dim[0].step + b*outerHistogramMat->dim[1].step + c*outerHistogramMat->dim[2].step)) *
1912 *((
float*)(innerHistogramMat->data.ptr + a*innerHistogramMat->dim[0].step + b*innerHistogramMat->dim[1].step + c*innerHistogramMat->dim[2].step)));
1915 likelihood=(likelihood+_inside_outside_difference_weight)/(1+_inside_outside_difference_weight);
1917 yWarning(
"LIKELIHOOD<0!!!");
1921 bool PF3DTracker::testOpenCv()
1928 points = cvCreateMat( 3, 2*nPixels, type );
1929 readInitialmodel3dPoints(points,
"models/initial_ball_points_46mm_30percent.csv");
1931 failure = place3dPointsPerspective(points,100,200,1000);
1933 failure = perspective_projection(points, _perspectiveFx, _perspectiveFy, _perspectiveCx, _perspectiveCy, _uv)!=0;
1939 void printMat(CvMat* A)
1942 for(a=0;a<A->rows;a++)
1944 for(b=0;b<A->cols;b++)
1946 cout<<((
float*)(A->data.ptr + A->step*a))[b]<<
",";
Copyright: (C) 2009 RobotCub Consortium Authors: Matteo Taiana, Ugo Pattacini CopyPolicy: Released un...