1. 程式人生 > >運動物體目標檢測實現—基於OpenCV

運動物體目標檢測實現—基於OpenCV

#include     
#include     
#include     
#include     
int main( int argc, char** argv )    
    
  //宣告IplImage指標    
  IplImage* pFrame = NULL;    
 IplImage* pFrImg = NULL;    
 IplImage* pBkImg = NULL;    
 CvMat* pFrameMat = NULL;    
  CvMat* pFrMat = NULL;    
 CvMat* pBkMat = NULL;    
    
  CvCapture* pCapture = NULL;    
     
  int nFrmNum = 0;    
  //建立視窗     
 cvNamedWindow("video", 1);    
 cvNamedWindow("background",1);    
 cvNamedWindow("foreground",1);    
  //使視窗有序排列    
 cvMoveWindow("video", 30, 0);    
 cvMoveWindow("background", 360, 0);    
  cvMoveWindow("foreground", 690, 0);    
  argc = 1;  
    
  if( argc > 2 )    
    {    
      fprintf(stderr, "Usage: bkgrd [video_file_name]\n");    
      return -1;    
    }    
  //開啟攝像頭    
  if (argc ==1)    
    if( !(pCapture = cvCaptureFromCAM(-1)))    
      {    
    fprintf(stderr, "Can not open camera.\n");    
    return -2;    
      }    
  //開啟視訊檔案    
  if(argc == 2)    
    if( !(pCapture = cvCaptureFromFile(argv[1])))    
      {    
    fprintf(stderr, "Can not open video file %s\n", argv[1]);    
    return -2;    
      }    
     
  //逐幀讀取視訊    
  while(pFrame = cvQueryFrame( pCapture ))    
    {    
      nFrmNum++;    
         
      //如果是第一幀,需要申請記憶體,並初始化    
      if(nFrmNum == 1)    
    {    
      pBkImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),  IPL_DEPTH_8U,1);    
      pFrImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),  IPL_DEPTH_8U,1);    
      pBkMat    = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);    
      pFrMat    = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);    
      pFrameMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);    
      //轉化成單通道影象再處理    
      cvCvtColor(pFrame, pBkImg, CV_BGR2GRAY);    
      cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);    
      cvConvert(pFrImg, pFrameMat);    
      cvConvert(pFrImg, pFrMat);    
      cvConvert(pFrImg, pBkMat);    
    }    
      else    
    {    
      cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);    
      cvConvert(pFrImg, pFrameMat);    
      //先高斯濾波,以平滑影象    
      //cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0);    
         
      //當前幀跟背景圖相減    
      cvAbsDiff(pFrameMat, pBkMat, pFrMat);    
      //二值化前景圖    
      cvThreshold(pFrMat, pFrImg, 60, 255.0, CV_THRESH_BINARY);    
      //進行形態學濾波,去掉噪音     
      //cvErode(pFrImg, pFrImg, 0, 1);    
      //cvDilate(pFrImg, pFrImg, 0, 1);    
      //更新背景    
      cvRunningAvg(pFrameMat, pBkMat, 0.003, 0);    
      //將背景轉化為影象格式,用以顯示    
      cvConvert(pBkMat, pBkImg);    
      //顯示影象    
      cvShowImage("video", pFrame);    
      cvShowImage("background", pBkImg);    
      cvShowImage("foreground", pFrImg);    
      //如果有按鍵事件,則跳出迴圈    
      //此等待也為cvShowImage函式提供時間完成顯示     
      //等待時間可以根據CPU速度調整    
      if( cvWaitKey(2) >= 0 )    
        break;    
    
    }    
    }    
       
  //銷燬視窗    
  cvDestroyWindow("video");    
  cvDestroyWindow("background");    
  cvDestroyWindow("foreground");    
  //釋放影象和矩陣    
  cvReleaseImage(&pFrImg);    
  cvReleaseImage(&pBkImg);    
  cvReleaseMat(&pFrameMat);    
  cvReleaseMat(&pFrMat);    
  cvReleaseMat(&pBkMat);    
  cvReleaseCapture(&pCapture);    
  return 0;    
}