How to use Opencv 3 particle filter api
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
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 ?
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
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
sidenote: your example is from a 3 years old, outdated fork...
I only use the mouse event to get the rect in this example.
try:
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
oook. at least, we got a proper link now ;)
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.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...><)
Excuse me, Do you figure out this problem? Cause i have gotten in the same trouble, Thank you.