⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 track1.c

📁 不错的particle filter的程序
💻 C
字号:
/*  Perform single object tracking with particle filtering  @author Rob Hess  @version 1.0.0-20060306*/#include "track.h"/************************** Function Definitions *****************************//*  Allows the user to interactively select object regions.  @param frame the frame of video in which objects are to be selected  @param regions a pointer to an array to be filled with rectangles    defining object regions  @return Returns the number of objects selected by the user*/int get_regions( IplImage* frame, CvRect** regions ){  char* win_name = "First frame";  params p;  CvRect* r;  int i, x1, y1, x2, y2, w, h;    /* use mouse callback to allow user to define object regions */  p.win_name = win_name;  p.orig_img = (IplImage *)cvClone( frame );  p.cur_img = NULL;  p.n = 0;  cvNamedWindow( win_name, 1 );  cvShowImage( win_name, frame );  cvSetMouseCallback( win_name, &mouse, &p );  cvWaitKey( 0 );  cvDestroyWindow( win_name );  cvReleaseImage( &(p.orig_img) );  if( p.cur_img )    cvReleaseImage( &(p.cur_img) );  /* extract regions defined by user; store as an array of rectangles */  if( p.n == 0 )    {      *regions = NULL;      return 0;    }  r = (CvRect *)malloc( p.n * sizeof( CvRect ) );  for( i = 0; i < p.n; i++ )	//for each rectangle round the object    {      x1 = MIN( p.loc1[i].x, p.loc2[i].x );      x2 = MAX( p.loc1[i].x, p.loc2[i].x );      y1 = MIN( p.loc1[i].y, p.loc2[i].y );      y2 = MAX( p.loc1[i].y, p.loc2[i].y );      w = x2 - x1;      h = y2 - y1;      /* ensure odd width and height */      w = ( w % 2 )? w : w+1;      h = ( h % 2 )? h : h+1;      r[i] = cvRect( x1, y1, w, h );	//define one of the rects    }  *regions = r;  return p.n;}/*  Mouse callback function that allows user to specify the initial object  regions.  Parameters are as specified in OpenCV documentation.*/void mouse( int event, int x, int y, int flags, void* param ){  params* p = (params*)param;  CvPoint* loc;  int n;  IplImage* tmp;  static int pressed = FALSE;  int height=p->orig_img->height;    /* on left button press, remember first corner of rectangle around object */  if( event == CV_EVENT_LBUTTONDOWN )    {      n = p->n;      if( n == MAX_OBJECTS )	return;      loc = p->loc1;      loc[n].x = x;      loc[n].y = height-y;      pressed = TRUE;    }  /* on left button up, finalize the rectangle and draw it in black */  else if( event == CV_EVENT_LBUTTONUP )    {      n = p->n;      if( n == MAX_OBJECTS )	return;      loc = p->loc2;      loc[n].x = x;      loc[n].y = height-y;      cvReleaseImage( &(p->cur_img) );      p->cur_img = NULL;      cvRectangle( p->orig_img, p->loc1[n], loc[n], CV_RGB(0,0,0), 1, 8, 0 );      cvShowImage( p->win_name, p->orig_img );      pressed = FALSE;      p->n++;    }  /* on mouse move with left button down, draw rectangle as defined in white */  else if( event == CV_EVENT_MOUSEMOVE  &&  flags & CV_EVENT_FLAG_LBUTTON )    {      n = p->n;      if( n == MAX_OBJECTS )	return;      tmp = (IplImage *)cvClone( p->orig_img );      loc = p->loc1;      cvRectangle( tmp, loc[n], cvPoint(x, height-y), CV_RGB(255,255,255), 1, 8, 0 );      cvShowImage( p->win_name, tmp );      if( p->cur_img )	cvReleaseImage( &(p->cur_img) );      p->cur_img = tmp;    }}/*  Computes a reference histogram for each of the object regions defined by  the user  @param frame video frame in which to compute histograms; should have been    converted to hsv using bgr2hsv in observation.h  @param regions regions of \a frame over which histograms should be computed  @param n number of regions in \a regions  @param export if TRUE, object region images are exported  @return Returns an \a n element array of normalized histograms corresponding    to regions of \a frame specified in \a regions.*/histogram** compute_ref_histos( IplImage* frame, CvRect* regions, int n ){  histogram** histos =(histogram **)malloc( n * sizeof( histogram* ) );  IplImage* tmp;  int i;  /* extract each region from frame and compute its histogram */  for( i = 0; i < n; i++ )    {      cvSetImageROI( frame, regions[i] );	//set the area specified by rect as the ROI of the frame      tmp = cvCreateImage( cvGetSize( frame ), IPL_DEPTH_32F, 3 );      cvCopy( frame, tmp, NULL );	//tmp=the ROI of the frame;      cvResetImageROI( frame );      histos[i] = calc_histogram( &tmp, 1 );      normalize_histogram( histos[i] );      cvReleaseImage( &tmp );    }  return histos;}/*  Exports reference histograms to file  @param ref_histos array of reference histograms  @param n number of histograms  @return Returns 1 on success or 0 on failure*/int export_ref_histos( histogram** ref_histos, int n ){  char name[32];  char num[3];//  FILE* file;  int i;    for( i = 0; i < n; i++ )    {      sprintf( num, "%02d", i );	//snprintf( num, 3, "%02d", i );      strcpy( name, "hist_" );      strcat( name, num );      strcat( name, ".dat" );      if( ! export_histogram( ref_histos[i], name ) )	return 0;    }  return 1;}/*  Exports a frame whose name and format are determined by EXPORT_BASE and  EXPORT_EXTN, defined above.  @param frame frame to be exported  @param i frame number*/int export_frame( IplImage* frame, int i ){  char name[EXPORT_NAME_LENGTH];	//[ strlen(EXPORT_BASE) + strlen(EXPORT_EXTN) + 4 ];  char num[5];  sprintf( num, "%04d", i );	//snprintf( num, 5, "%04d", i );  strcpy( name, EXPORT_BASE );  strcat( name, num );  strcat( name, EXPORT_EXTN );  return cvSaveImage( name, frame );}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -