高斯建模

Emgu 版本:

代码
 private void button1_Click(object sender, EventArgs e)
        {
            Emgu.CV.Capture cap 
= new Capture("d:\\1.wmv");
            Emgu.CV.VideoSurveillance.BGStatModel
<Bgr> bg = null; ;
            Capture c 
= new Capture("d:\\1.wmv");
            Image
<Bgr, byte> img = null;
            
while ((img = c.QueryFrame()) != null)
            {
                
this.count++;
                
if (this.count == 1)
                {
                    bg 
= new Emgu.CV.VideoSurveillance.BGStatModel<Bgr>(img,
                        Emgu.CV.CvEnum.BG_STAT_TYPE.GAUSSIAN_BG_MODEL);
                }
                
else
                {
                    
//更新高斯模型
                    bg.Update(img);
                    
this.pictureBox1.Image = img.Bitmap;
                    
this.pictureBox2.Image = bg.BackgroundMask.Bitmap;
                    
this.pictureBox3.Image = bg.ForgroundMask.Bitmap;
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(
30);
                }

            }
        }


C++版本:

代码
int gaoshi(int argc, char* argv)
{
   IplImage
* pFrame = NULL;   
   IplImage
* pFrImg = NULL; 
   IplImage
* pBkImg = NULL;   
   CvCapture
* pCapture = NULL;   
   
int nFrmNum = 0;
 
   cvNamedWindow(
"video"1); 
   cvNamedWindow(
"background",1); 
   cvNamedWindow(
"foreground",1);   
   cvMoveWindow(
"video"300); 
   cvMoveWindow(
"background"3600); 
   cvMoveWindow(
"foreground"6900);
   
if( argc > 2 )   
   {     
      fprintf(stderr, 
"Usage: bkgrd [video_file_name]\n");     
      
return -1;   
   }
 
 
   
//打开视频文件 
   if(argc == 2)   
      
if!(pCapture = cvCaptureFromFile(argv)))     
      {   
         fprintf(stderr, 
"Can not open video file %s\n", argv[1]);   
         
return -2;     
      }
   
//打开摄像头 
   if (argc == 1)   
      
if!(pCapture = cvCaptureFromCAM(-1)))     
      {   
         fprintf(stderr, 
"Can not open camera.\n");   
         
return -2;     
      }   
 
                
//初始化高斯混合模型参数
   CvGaussBGModel* bg_model=NULL;
 
   
while(pFrame = cvQueryFrame( pCapture ))   
   {     
      nFrmNum
++
      
if(nFrmNum<3)
          
continue;
      
if(nFrmNum == 3)   
      {    
         pBkImg 
= cvCreateImage(cvSize(pFrame->width, pFrame->height),  IPL_DEPTH_8U,3);   
         pFrImg 
= cvCreateImage(cvSize(pFrame->width, pFrame->height),  IPL_DEPTH_8U,1);     
 
 
         
//高斯背景建模,pFrame可以是多通道图像也可以是单通道图像
          
//cvCreateGaussianBGModel函数返回值为CvBGStatModel*,
          
//需要强制转换成CvGaussBGModel*
         bg_model = (CvGaussBGModel*)cvCreateGaussianBGModel(pFrame, 0);
      }     
      
else
      {    
                 
//更新高斯模型
        cvUpdateBGStatModel(pFrame, (CvBGStatModel *)bg_model );
 
         
//pFrImg为前景图像,只能为单通道
          
//pBkImg为背景图像,可以为单通道或与pFrame通道数相同
         cvCopy(bg_model->foreground,pFrImg,0);
         cvCopy(bg_model
->background,pBkImg,0);
 
         
//把图像正过来
         pBkImg->origin=1;
         pFrImg
->origin=1;
 
         cvShowImage(
"video", pFrame);    
         cvShowImage(
"background", pBkImg);    
         cvShowImage(
"foreground", pFrImg);       
         
if( cvWaitKey(2>= 0 )      
            
break;     
      }     
 
   }
 
   
//释放高斯模型参数占用内存   
   cvReleaseBGStatModel((CvBGStatModel**)&bg_model);
   cvDestroyWindow(
"video"); 
   cvDestroyWindow(
"background"); 
   cvDestroyWindow(
"foreground");   
   cvReleaseImage(
&pFrImg); 
   cvReleaseImage(
&pBkImg);   
   cvReleaseCapture(
&pCapture);   

}


原文地址:https://www.cnblogs.com/81/p/1904566.html