opencv平均背景法详解

  

本文实例为大家分享了opencv平均背景法的具体代码,供大家参考,具体内容如下

        # include   # include   # include   使用名称空间性病;   使用名称空间的简历;   * IprevF IplImage * IavgF, * IdiffF, * IhiF, * IlowF;   * Iscratch3 IplImage * Iscratch;   * Igray3 IplImage * Igray1, * Igray2;   * Ilow3 IplImage * Ilow1, * Ilow2;   * Ihi3 IplImage * Ihi1, * Ihi2;   IplImage * Imaskt;   浮动Icount;   空白AllocateImages (IplImage *我){   CvSize深圳=cvGetSize(我);   IavgF=cvCreateImage(深圳IPL_DEPTH_32F 3);   IdiffF=cvCreateImage(深圳IPL_DEPTH_32F 3);   IprevF=cvCreateImage(深圳IPL_DEPTH_32F 3);   IhiF=cvCreateImage(深圳IPL_DEPTH_32F 3);   IlowF=cvCreateImage(深圳IPL_DEPTH_32F 3);   Ilow1=cvCreateImage(深圳IPL_DEPTH_32F 1);   Ilow2=cvCreateImage(深圳IPL_DEPTH_32F 1);   Ilow3=cvCreateImage(深圳IPL_DEPTH_32F 1);   Ihi1=cvCreateImage(深圳IPL_DEPTH_32F 1);   Ihi2=cvCreateImage(深圳IPL_DEPTH_32F 1);   Ihi3=cvCreateImage(深圳IPL_DEPTH_32F 1);   cvZero (IavgF);   cvZero (IdiffF);   cvZero (IprevF);   cvZero (IhiF);   cvZero (IlowF);   Icount=0.00001;   Iscratch=cvCreateImage(深圳IPL_DEPTH_32F 3);   Iscratch3=cvCreateImage(深圳IPL_DEPTH_32F 3);   Igray1=cvCreateImage(深圳IPL_DEPTH_32F 1);   Igray2=cvCreateImage(深圳IPL_DEPTH_32F 1);   Igray3=cvCreateImage(深圳IPL_DEPTH_32F 1);   Imaskt=cvCreateImage(深圳IPL_DEPTH_8U 1);   cvZero (Iscratch);   cvZero (Iscratch3);   }   空白accumulateBackground (IplImage *我){   静态int第一=1;   cvCvtScale(我Iscratch 1,0);   如果(!){   cvAcc (Iscratch IavgF);   cvAbsDiff (Iscratch IprevF Iscratch3);   cvAcc (Iscratch3 IdiffF);   Icount +=1.0;   }   第一次=0;   cvCopy (Iscratch IprevF);   }   空白setHighThreshold(浮动范围){   cvConvertScale (IdiffF、Iscratch、规模);   cvAdd (Iscratch IavgF IhiF);   cvSplit (IhiF Ihi1、Ihi2 Ihi3, 0);   }   空白setLowThreshold(浮动范围){   cvConvertScale (IdiffF、Iscratch、规模);   cvAdd (IavgF Iscratch IlowF);   cvSplit (IlowF Ilow1、Ilow2 Ilow3, 0);   }      空白createModelsfromStats () {   cvConvertScale (IavgF IavgF(双)(1.0/Icount));   cvConvertScale (IdiffF IdiffF(双)(1.0/Icount));   cvAddS (IdiffF cvScalar (1.0, 1.0, 1.0), IdiffF);   setHighThreshold (10.0);   setLowThreshold (4.0);   }   空白backgroundDiff (IplImage *我,IplImage * Imask) {   cvCvtScale(我Iscratch 1,0);   cvSplit (Iscratch Igray1、Igray2 Igray3, 0);   cvInRange (Igray1 Ilow1、Ihi1 Imask);   cvInRange (Igray2 Ilow2、Ihi2 Imaskt);   cvOr (Imask Imaskt Imask);   cvInRange (Igray3 Ilow3、Ihi3 Imaskt);   cvOr (Imask Imaskt Imask);   cvSubRS (Imask标量(255),Imask);   }   空白DeallocateImages () {   cvReleaseImage(及IavgF);   cvReleaseImage(及IdiffF);   cvReleaseImage(及IprevF);   cvReleaseImage(及IhiF);   cvReleaseImage(及IlowF);   cvReleaseImage(及Ilow1);   cvReleaseImage(及Ilow2);   cvReleaseImage(及Ilow3);   cvReleaseImage(及Ihi1);   cvReleaseImage(及Ihi2);   cvReleaseImage(及Ihi3);   cvReleaseImage(及Iscratch);   cvReleaseImage(及Iscratch3);   cvReleaseImage(及Igray1);   cvReleaseImage(及Igray2);   cvReleaseImage(及Igray3);   cvReleaseImage(及Imaskt);   }      char文件名[100];   char newcontour [100];   void main ()   {   TickMeter tm;   tm.start ();//多一个   IplImage * src=https://www.yisu.com/zixun/cvLoadImage(“待处理背面图\ \ 55124. bmp”);   AllocateImages (src);   for (int i=55124;我& lt;=56460;我+ +)   {   sprintf(文件名,“待处理背面图\ \ % d。bmp”,我);   sprintf (newcontour,“分割前景\ \ % d。bmp”,我);   IplImage * src_ipl=cvLoadImage(文件名);   accumulateBackground (src_ipl);   createModelsfromStats ();   CvSize深圳=cvGetSize (src_ipl);   IplImage * myImask=cvCreateImage(深圳IPL_DEPTH_8U 1);;   backgroundDiff (src_ipl myImask);   cvSaveImage (newcontour myImask);   }   DeallocateImages ();   tm.stop ();   cout & lt; & lt;"数=" & lt; & lt;tm.getCounter () & lt; & lt;”,过程时间=" & lt; & lt;tm.getTimeMilli () & lt; & lt;endl;   }      

然而对我的图还是不适合分割出轮廓:

  

 opencv平均背景法详解

  

opencv平均背景法详解