OpenCV学习——物体跟踪的粒子滤波算法实现之粒子集重新采样

/*
Re-samples a set of particles according to their weights to produce a
new set of unweighted particles

@param particles an old set of weighted particles whose weights have been
normalized with normalize_weights()
@param n the number of particles in \a particles

@return Returns a new set of unweighted particles sampled from \a particles
*/
particle* resample( particle* particles, int n )
{
particle* new_particles;
int i, j, np, k = 0;

qsort( particles, n, sizeof( particle ), &particle_cmp );
new_particles = malloc( n * sizeof( particle ) );
for( i = 0; i < n; i++ )
{
np = cvRound( particles[i].w * n );
for( j = 0; j < np; j++ )
{
new_particles[k++] = particles[i];
if( k == n )
goto exit;
}
}
while( k < n )
new_particles[k++] = particles[0];

exit:
return new_particles;
}

程序先使用C标准库中的qsort排序函数,按照权重,由大到小将原粒子集排序。然后将权重大的在新的粒子集中分配的多一点。

你可能感兴趣的:(OpenCV学习——物体跟踪的粒子滤波算法实现之粒子集重新采样)