The theory of particle filtering is so wonderful that it approximates complex probability density functions with a set of random states of different weights. It has excellent properties in the nonlinear and non-Gaussian systems. OpenCV gives an implementation, but does not give an example, the learning process found on the network can not find. Learning OpenCV is described in a book, but it is still a distance away from direct use. After some bumpy, finally can use, hope to help you.
The example given in this article is one of the same applications as my other blog post, which is to smooth and predict two-dimensional coordinates.
How to use:
1. Create and initialize
const INT statenum=4;//State number
const int measurenum=2;//measurement variable number
const int samplenum=2000;//number of particles
cvcondensation* Condens = cvcreatecondensation (statenum,measurenum,samplenum);
The larger the number of particles, the more stable the system performance without compromising performance
Please refer to learning OPENCV for other initialization contents.
2. Forecasting
3. Update the example confidence, which is the weight. In this example, the Update method differs from learning OpenCV, and you want to see the code
4. Update Cvcondensation
Code:
[CPP]View PlainCopyprint?
- #include <cv.h>
- #include <cxcore.h>
- #include
- #include <cvaux.h>
- #include <cmath>
- #include <vector>
- #include <iostream>
- Using namespace std;
- const int winheight=600;
- const int winwidth=800;
- Cvpoint Mouseposition=cvpoint (winwidth>>1,winheight>>1);
- Mouse Event Callback
- void mouseEvent (int event,int x,int y,int flags,void *param)
- {
- if (event==cv_event_mousemove) {
- Mouseposition=cvpoint (x, y);
- }
- }
- int main (void)
- {
- //1.condensation Setup
- const int statenum=4;
- const int measurenum=2;
- const int samplenum=2000;
- cvcondensation* Condens = cvcreatecondensation (statenum,measurenum,samplenum);
- Cvmat* lowerbound;
- cvmat* Upperbound;
- Lowerbound = Cvcreatemat (Statenum, 1, cv_32f);
- Upperbound = Cvcreatemat (Statenum, 1, cv_32f);
- Cvmset (lowerbound,0,0,0.0);
- Cvmset (Upperbound,0,0,winwidth);
- Cvmset (lowerbound,1,0,0.0);
- Cvmset (Upperbound,1,0,winheight);
- Cvmset (lowerbound,2,0,0.0);
- Cvmset (upperbound,2,0,0.0);
- Cvmset (lowerbound,3,0,0.0);
- Cvmset (upperbound,3,0,0.0);
- float A[statenum][statenum] ={
- 1,0,1,0,
- 0,1,0,1,
- 0,0,1,0,
- 0,0,0,1
- };
- memcpy (Condens->dynammatr,a,sizeof (A));
- Cvcondensinitsampleset (Condens, lowerbound, upperbound);
- Cvrng rng_state = cvrng (0xFFFFFFFF);
- For (int i=0; i < samplenum; i++) {
- Condens->flsamples[i][0] = float (cvrandint (&rng_state)% winwidth); //width
- CONDENS->FLSAMPLES[I][1] = float (cvrandint (&rng_state)% winheight); Height
- }
- Cvfont font;
- Cvinitfont (&font,cv_font_hershey_script_complex,1,1);
- char* winname="condensation";
- Cvnamedwindow (Winname);
- Cvsetmousecallback (winname,mouseevent);
- iplimage* img=cvcreateimage (Cvsize (winwidth,winheight), 8, 3);
- bool ispredictonly=false; Trigger for prediction only,press SPACEBAR
- While (1) {
- //2.condensation Prediction
- Cvpoint predict_pt=cvpoint ((int) condens->state[0], (int) condens->state[1]);
- float variance[measurenum]={0};
- //get variance/standard Deviation of each State
- For (int i=0;i<measurenum;i++) {
- //sum
- float sumstate=0;
- For (int j=0;j<condens->samplesnum;j++) {
- sumstate+=condens->flsamples[i][j];
- }
- //average
- Sumstate/=samplenum;
- //variance
- For (int j=0;j<condens->samplesnum;j++) {
- variance[i]+= (condens->flsamples[i][j]-sumstate) *
- (condens->flsamples[i][j]-sumstate);
- }
- Variance[i]/=samplenum-1;
- }
- //3.update particals Confidence
- Cvpoint pt;
- if (ispredictonly) {
- Pt=predict_pt;
- }else{
- Pt=mouseposition;
- }
- For (int i=0;i<condens->samplesnum;i++) {
- float probx= (float) exp ( -1* (pt.x-condens->flsamples[i][0])
- * (Pt.x-condens->flsamples[i][0])/(2*variance[0]));
- float proby= (float) exp ( -1* (pt.y-condens->flsamples[i][1])
- * (Pt.y-condens->flsamples[i][1])/(2*variance[1]));
- condens->flconfidence[i]=probx*proby;
- }
- //4.update Condensation
- Cvcondensupdatebytime (Condens);
- //draw
- Cvset (Img,cvscalar (255,255,255,0));
- Cvcircle (Img,predict_pt,5,cv_rgb (0,255,0), 3); //predicted Point with green
- Char buf[256];
- sprintf_s (buf,256,"predicted position: (%3d,%3d)", PREDICT_PT.X,PREDICT_PT.Y);
- Cvputtext (Img,buf,cvpoint (10,30), &font,cv_rgb (0,0,0));
- if (!ispredictonly) {
- Cvcircle (Img,mouseposition,5,cv_rgb (255,0,0), 3); //current position with red
- sprintf_s (buf,256,"real position:(%3d,%3d)", MOUSEPOSITION.X,MOUSEPOSITION.Y);
- Cvputtext (Img,buf,cvpoint (10,60), &font,cv_rgb (0,0,0));
- }
- Cvshowimage (Winname, IMG);
- int Key=cvwaitkey (30);
- if (key==27) {//esc
- Break ;
- }Else if (key==') {//trigger for prediction
- //ispredict=!ispredict;
- if (ispredictonly) {
- Ispredictonly=false;
- }else{
- Ispredictonly=true;
- }
- }
- }
- Cvreleaseimage (&IMG);
- Cvreleasecondensation (&condens);
- return 0;
- }
Kalman Filter Video Demo:
The number of particles in the demo is 100,200,2000
Please observe the effect carefully
Http://v.youku.com/v_show/id_XMjU4MzE0ODgw.html
Demo Snapshot:
the above article is a demonstration point tracking, the original http://blog.csdn.net/onezeros/article/details/6319180
This one is handed in to a Buddy demo window Tracker! (with code) http://www.cnblogs.com/yangyangcv/archive/2010/05/23/1742263.html from:http://blog.csdn.net/yangtrees/ article/details/7616483
Learn opencv--particle filter (two articles on-line summary)