How to use Opencv 3 particle filter api

asked 2016-05-26 03:33:44 -0500

DWei gravatar image

updated 2016-06-19 13:09:00 -0500

I have built the opencv_contirb for opencv 3.1, and I got the tracking module.

The document :

The 3.0 doc also introduce the method :

I've tried the TrackerSampler::addTrackerSamplerAlgorithm and rewrote to the particle filter version myself below:

#include <opencv2\core.hpp>
#include <opencv2\video.hpp>
#include <opencv2\highgui.hpp>
#include <opencv2\tracking.hpp>
#include <opencv2\tracking\tracker.hpp>
#include <opencv2\tracking\feature.hpp>

using namespace std ;
using namespace cv ;
Mat src ; // video frame
Rect boundingBox ; // boundingBox the target Rect
Mat roi = Mat(src ,boundingBox) ;
vector<Mat> sampleImg ;
Ptr<TrackerSamplerAlgorithm> PFSampler  = new TrackerSamplerPF( roi, TrackerSamplerPF::Params() ) ;
Ptr<TrackerSampler> sampler = new TrackerSampler() ;

if( sampler->addTrackerSamplerAlgorithm( PFSampler ) ) {
  cout << PFSampler->getClassName() << endl ; // check the 'pf' mode ;
  PFSampler->sampling( src, boundingBox, sampleImg ) ;  // <---------- it occured error here
} // if

The error seemed something empty but I tracked the function work found these:

The opencv source from trackerSamplerAlgorithm.cpp



The error message:

OpenCV Error: Assertion failed (0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows) in cv::Mat::Mat, file C:\OpenCV\source\modules\core\src\matrix.cpp, line 477

bool TrackerSamplerPF::samplingImpl( const Mat& image, Rect boundingBox, std::vector<Mat>& sample ){
    Ptr<TrackerTargetState> ptr;
    Mat_<double> _last_guess=(Mat_<double>(1,4)<<(double)boundingBox.x,(double)boundingBox.y,
    PFSolver* promoted_solver=dynamic_cast<PFSolver*>(static_cast<MinProblemSolver*>(_solver));

    while(promoted_solver->iteration() <= promoted_solver->getTermCriteria().maxCount);

    Rect res=Rect(Point_<int>((int)_last_guess(0,0),(int)_last_guess(0,1)),Point_<int>((int)_last_guess(0,2),(int)_last_guess(0,3)));
    return true;

The src and roi, boundingBox isnt empty or null ; The only empty input is the (vector<mat>) sampleImg, however it looked like an output data..

Does anyone try this method successful ?

edit retag flag offensive close merge delete


a Rect is (x,y,w,h), yours is (x,y,x+w,y+h), i guess, you're out ofbounds in the last line

berak gravatar imageberak ( 2016-05-26 04:01:59 -0500 )edit

To berak :

Thanks for your friendly reminder. I've checked this again and it is not the reason to the error.

I thought the, width and height is not out of the src image size.

The rect(boundingBox) method reference here:

DWei gravatar imageDWei ( 2016-05-26 07:54:33 -0500 )edit

sidenote: your example is from a 3 years old, outdated fork...

berak gravatar imageberak ( 2016-05-26 08:26:28 -0500 )edit

I only use the mouse event to get the rect in this example.

DWei gravatar imageDWei ( 2016-05-26 09:44:55 -0500 )edit


 Mat_<double> _last_guess=(Mat_<double>(1,4)<<(double)boundingBox.x, (double)boundingBox.y,
(double)boundingBox.width, (double)boundingBox.height);
berak gravatar imageberak ( 2016-05-27 03:18:10 -0500 )edit

I tried your suggest, and the implement is from opencv api...

So I think that isnt the reason.

The implement source I built from here at last function

DWei gravatar imageDWei ( 2016-05-27 04:36:57 -0500 )edit

oook. at least, we got a proper link now ;)

berak gravatar imageberak ( 2016-05-27 04:42:26 -0500 )edit

Maybe Rect res is Rect(0,0,0,0) as _last_guess might have converged to 0,0,0,0. You should probably check that to confirm.

Prasanna gravatar imagePrasanna ( 2016-05-30 06:39:57 -0500 )edit

To Prasanna:

All right, I tried your suggest too.

Then I track the api error occured at line 405 in trackerSamplerAlgorithm.cpp

Finally, I find the condition use PFSolver::iteration()

The error occured at line 109 in PFSolver.hpp

The variable "max_element" counted itself, so it seems there is some bugs in the implement case.

I did not continue to check the variables because I just wanna know if I input the incorrect paras or the steps doing particle filter.

If it is the bugs in opencv api, I think I should find some others implement even in 2.X

(It maybe more readable than this case...><)

DWei gravatar imageDWei ( 2016-05-30 12:32:05 -0500 )edit

Excuse me, Do you figure out this problem? Cause i have gotten in the same trouble, Thank you.

Vince Li gravatar imageVince Li ( 2017-09-21 04:55:30 -0500 )edit