Learn opencv--particle filter (two articles on-line summary)

Source: Internet
Author: User

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?
  1. #include <cv.h>
  2. #include <cxcore.h>
  3. #include
  4. #include <cvaux.h>
  5. #include <cmath>
  6. #include <vector>
  7. #include <iostream>
  8. Using namespace std;
  9. const int winheight=600;
  10. const int winwidth=800;
  11. Cvpoint Mouseposition=cvpoint (winwidth>>1,winheight>>1);
  12. Mouse Event Callback
  13. void mouseEvent (int event,int x,int y,int flags,void *param)
  14. {
  15. if (event==cv_event_mousemove) {
  16. Mouseposition=cvpoint (x, y);
  17. }
  18. }
  19. int main (void)
  20. {
  21. //1.condensation Setup
  22. const int statenum=4;
  23. const int measurenum=2;
  24. const int samplenum=2000;
  25. cvcondensation* Condens = cvcreatecondensation (statenum,measurenum,samplenum);
  26. Cvmat* lowerbound;
  27. cvmat* Upperbound;
  28. Lowerbound = Cvcreatemat (Statenum, 1, cv_32f);
  29. Upperbound = Cvcreatemat (Statenum, 1, cv_32f);
  30. Cvmset (lowerbound,0,0,0.0);
  31. Cvmset (Upperbound,0,0,winwidth);
  32. Cvmset (lowerbound,1,0,0.0);
  33. Cvmset (Upperbound,1,0,winheight);
  34. Cvmset (lowerbound,2,0,0.0);
  35. Cvmset (upperbound,2,0,0.0);
  36. Cvmset (lowerbound,3,0,0.0);
  37. Cvmset (upperbound,3,0,0.0);
  38. float A[statenum][statenum] ={
  39. 1,0,1,0,
  40. 0,1,0,1,
  41. 0,0,1,0,
  42. 0,0,0,1
  43. };
  44. memcpy (Condens->dynammatr,a,sizeof (A));
  45. Cvcondensinitsampleset (Condens, lowerbound, upperbound);
  46. Cvrng rng_state = cvrng (0xFFFFFFFF);
  47. For (int i=0; i < samplenum; i++) {
  48. Condens->flsamples[i][0] = float (cvrandint (&rng_state)% winwidth); //width
  49. CONDENS->FLSAMPLES[I][1] = float (cvrandint (&rng_state)% winheight); Height
  50. }
  51. Cvfont font;
  52. Cvinitfont (&font,cv_font_hershey_script_complex,1,1);
  53. char* winname="condensation";
  54. Cvnamedwindow (Winname);
  55. Cvsetmousecallback (winname,mouseevent);
  56. iplimage* img=cvcreateimage (Cvsize (winwidth,winheight), 8, 3);
  57. bool ispredictonly=false; Trigger for prediction only,press SPACEBAR
  58. While (1) {
  59. //2.condensation Prediction
  60. Cvpoint predict_pt=cvpoint ((int) condens->state[0], (int) condens->state[1]);
  61. float variance[measurenum]={0};
  62. //get variance/standard Deviation of each State
  63. For (int i=0;i<measurenum;i++) {
  64. //sum
  65. float sumstate=0;
  66. For (int j=0;j<condens->samplesnum;j++) {
  67. sumstate+=condens->flsamples[i][j];
  68. }
  69. //average
  70. Sumstate/=samplenum;
  71. //variance
  72. For (int j=0;j<condens->samplesnum;j++) {
  73. variance[i]+= (condens->flsamples[i][j]-sumstate) *
  74. (condens->flsamples[i][j]-sumstate);
  75. }
  76. Variance[i]/=samplenum-1;
  77. }
  78. //3.update particals Confidence
  79. Cvpoint pt;
  80. if (ispredictonly) {
  81. Pt=predict_pt;
  82. }else{
  83. Pt=mouseposition;
  84. }
  85. For (int i=0;i<condens->samplesnum;i++) {
  86. float probx= (float) exp ( -1* (pt.x-condens->flsamples[i][0])
  87. * (Pt.x-condens->flsamples[i][0])/(2*variance[0]));
  88. float proby= (float) exp ( -1* (pt.y-condens->flsamples[i][1])
  89. * (Pt.y-condens->flsamples[i][1])/(2*variance[1]));
  90. condens->flconfidence[i]=probx*proby;
  91. }
  92. //4.update Condensation
  93. Cvcondensupdatebytime (Condens);
  94. //draw
  95. Cvset (Img,cvscalar (255,255,255,0));
  96. Cvcircle (Img,predict_pt,5,cv_rgb (0,255,0), 3); //predicted Point with green
  97. Char buf[256];
  98. sprintf_s (buf,256,"predicted position: (%3d,%3d)", PREDICT_PT.X,PREDICT_PT.Y);
  99. Cvputtext (Img,buf,cvpoint (10,30), &font,cv_rgb (0,0,0));
  100. if (!ispredictonly) {
  101. Cvcircle (Img,mouseposition,5,cv_rgb (255,0,0), 3); //current position with red
  102. sprintf_s (buf,256,"real position:(%3d,%3d)", MOUSEPOSITION.X,MOUSEPOSITION.Y);
  103. Cvputtext (Img,buf,cvpoint (10,60), &font,cv_rgb (0,0,0));
  104. }
  105. Cvshowimage (Winname, IMG);
  106. int Key=cvwaitkey (30);
  107. if (key==27) {//esc
  108. Break ;
  109. }Else if (key==') {//trigger for prediction
  110. //ispredict=!ispredict;
  111. if (ispredictonly) {
  112. Ispredictonly=false;
  113. }else{
  114. Ispredictonly=true;
  115. }
  116. }
  117. }
  118. Cvreleaseimage (&IMG);
  119. Cvreleasecondensation (&condens);
  120. return 0;
  121. }

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.