How to use Opencv 3 particle filter api

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

DWei gravatar image

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

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

The document :

http://docs.opencv.org/trunk/dc/d75/c...

The 3.0 doc also introduce the method :

http://docs.opencv.org/3.0-beta/modul...

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

PFSolver.hpp

TrackingFunctionPF.hpp

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,
    (double)boundingBox.x+boundingBox.width,(double)boundingBox.y+boundingBox.height);
    PFSolver* promoted_solver=dynamic_cast<PFSolver*>(static_cast<MinProblemSolver*>(_solver));

    promoted_solver->setParamsSTD(params.std);
    promoted_solver->minimize(_last_guess);
    dynamic_cast<TrackingFunctionPF*>(static_cast<MinProblemSolver::Function*>(promoted_solver->getFunction()))->update(image);
    while(promoted_solver->iteration() <= promoted_solver->getTermCriteria().maxCount);
    promoted_solver->getOptParam(_last_guess);

    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)));
    sample.clear();
    sample.push_back(image(res));
    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

Comments

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 -0600 )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 boundingBox.tl(), width and height is not out of the src image size.

The rect(boundingBox) method reference here: github.com/lenlen/opencv

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

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

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

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

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

try:

 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 -0600 )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 -0600 )edit
1

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

berak gravatar imageberak ( 2016-05-27 04:42:26 -0600 )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 -0600 )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 -0600 )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 -0600 )edit