Ejemplo n.º 1
0
void CvParticleFilter::observe()
{
  if (!initialized()) {return;}
  
  // draw new particles
  cvParticleTransition( m_particle );

  // measurement
// CV_TIMER_START();
  if ( !m_observer.measure(m_particle) ) {
    fprintf(stderr, "ERROR: particle measurement error!\n");
    return;
  }
// CV_TIMER_SHOW();

  int maxp_id = cvParticleGetMax( m_particle );
  CvBox2D box = cvParticleStateGet( m_particle, maxp_id );
  // fprintf(stderr, "center: %.2f,%.2f\n", box.center.x, box.center.y);
  m_window = box;

  // show best fit particle
  {
    float warp_p_data[3]={
      cos(box.angle/180.*CV_PI),
      box.center.x-box.size.width/2.,
      box.center.y-box.size.height/2.
    };
    CvMat warp_p = cvMat(3,1,CV_32F,warp_p_data);
    // m_observer.learn(&warp_p);

    // CvMat * imgYpatch=cvCreateMat(box.size.height,box.size.width,CV_8U);
    // icvWarp(m_imgY, imgYpatch, &warp_p);
    // CV_SHOW(imgYpatch);
    // cvReleaseMat(&imgYpatch);
  }

  // normalize
  cvParticleNormalize( m_particle);

  // resampling
  cvParticleResample( m_particle );
}
Ejemplo n.º 2
0
/**************************** Main ********************************/
int main( int argc, char** argv )
{
    IplImage *frame;
    CvCapture* video;
    int frame_num = 0;
    int i;

    arg_parse( argc, argv );

    // initialization
    cvParticleObserveInitialize( featsize );

    // read a video
    if( !vid_file || (isdigit(vid_file[0]) && vid_file[1] == '\0') )
        video = cvCaptureFromCAM( !vid_file ? 0 : vid_file[0] - '0' );
    else
        video = cvCaptureFromAVI( vid_file ); 
    if( (frame = cvQueryFrame( video )) == NULL )
    {
        fprintf( stderr, "Video %s is not loadable.\n", vid_file );
        usage();
        exit(1);
    }

    // allows user to select initial region
    CvRect region;
    icvGetRegion( frame, &region );

    // configure particle filter
    bool logprob = true;
    CvParticle *particle = cvCreateParticle( num_states, num_particles, logprob );
    CvParticleState std = cvParticleState (
        std_x,
        std_y,
        std_w,
        std_h,
        std_r
    );
    cvParticleStateConfig( particle, cvGetSize(frame), std );

    // initialize particle filter
    CvParticleState s;
    CvParticle *init_particle;
    init_particle = cvCreateParticle( num_states, 1 );
    CvRect32f region32f = cvRect32fFromRect( region );
    CvBox32f box = cvBox32fFromRect32f( region32f ); // centerize
    s = cvParticleState( box.cx, box.cy, box.width, box.height, 0.0 );
    cvParticleStateSet( init_particle, 0, s );
    cvParticleInit( particle, init_particle );
    cvReleaseParticle( &init_particle );

    // template
    IplImage* reference = cvCreateImage( featsize, frame->depth, frame->nChannels );
    IplImage* tmp = cvCreateImage( cvSize(region.width,region.height), frame->depth, frame->nChannels );
    cvCropImageROI( frame, tmp, region32f );
    cvResize( tmp, reference );
    cvReleaseImage( &tmp );

    while( ( frame = cvQueryFrame( video ) ) != NULL )
    {
        // Draw new particles
        cvParticleTransition( particle );
        // Measurements
        cvParticleObserveMeasure( particle, frame, reference );

        // Draw all particles
        for( i = 0; i < particle->num_particles; i++ )
        {
            CvParticleState s = cvParticleStateGet( particle, i );
            cvParticleStateDisplay( s, frame, CV_RGB(0,0,255) );
        }
        // Draw most probable particle
        //printf( "Most probable particle's state\n" );
        int maxp_id = cvParticleGetMax( particle );
        CvParticleState maxs = cvParticleStateGet( particle, maxp_id );
        cvParticleStateDisplay( maxs, frame, CV_RGB(255,0,0) );
        ///cvParticleStatePrint( maxs );
        
        // Save pictures
        if( arg_export ) {
            sprintf( export_filename, export_format, vid_file, frame_num );
            printf( "Export: %s\n", export_filename ); fflush( stdout );
            cvSaveImage( export_filename, frame );
        }
        cvShowImage( "Select an initial region > SPACE > ESC", frame );

        // Normalize
        cvParticleNormalize( particle);
        // Resampling
        cvParticleResample( particle );

        char c = cvWaitKey( 1000 );
        if(c == '\x1b')
            break;
    }

    cvParticleObserveFinalize();
    cvDestroyWindow( "Select an initial region > SPACE > ESC" );
    cvReleaseImage( &reference );
    cvReleaseParticle( &particle );
    cvReleaseCapture( &video );
    return 0;
}