統計均值前景檢測演算法(Mean-variation)

來源:互聯網
上載者:User

這是很早以前寫的一個演算法(貌似先前是參照某本書來寫的),現貼出來。 

轉帖,請註明出處:http://cvchina.net/blog-423-182.html                                   http://hi.baidu.com/belial/item/1cc308ed947d293c86d9dee9這是該演算法的.h檔案:
class StatAverageModel  {public: StatAverageModel(); StatAverageModel(CvCapture* pCapture,int Iteration); void Detect(IplImage* frame,float highSclae,float lowScale);// default value high=7.0,low=6.0 IplImage* getForeground(){return pForeImg;}; virtual ~StatAverageModel();private: void setHighThreshold(float scale); void setLowThreshold(float scale); IplImage* pFrame; IplImage* pavgF,* pdiffF,*pprevf,*pHif,*plowF; IplImage* pscratch,*pscratch2; IplImage* pgray1,*pgray2,*pgray3; IplImage* plow1,*plow2,*plow3; IplImage* phi1,*phi2,*phi3; IplImage* pmaskt; IplImage* pForeImg;  int gIteration; int gHeight; int gWidth; int tmpNum;};

下面是該演算法的.cpp檔案:

/*if you use the mean and variation to model the background ,you can do as follows:  1. calculate the sum of image sequences, you can apply the function: cvAcc();  2. also have another choice: cvRunningAvg();for the variation of each pixel, we can compute the accumulate squre image with the function cvSquareAcc();then the variation cam be computed with : simga^2= 1/N(x1^2+x2^2+...+xn^2)-(1/N(x1+x2+...+xn)^2)  統計平均法的基本思想:  計算每個像素的平均值和標準差(或者相似的,但計算速度更快的平均差值)作為背景模型。*/StatAverageModel::StatAverageModel(){}StatAverageModel::StatAverageModel(CvCapture* pCapture,int Iteration){ pFrame=cvQueryFrame(pCapture); pFrame=cvQueryFrame(pCapture); gIteration=Iteration; gHeight=pFrame->height; gWidth=pFrame->width; CvSize size=cvGetSize(pFrame); pavgF=cvCreateImage(size,IPL_DEPTH_32F,3); pdiffF=cvCreateImage(size,IPL_DEPTH_32F,3); pprevf=cvCreateImage(size,IPL_DEPTH_32F,3); plowF=cvCreateImage(size,IPL_DEPTH_32F,3); pHif=cvCreateImage(size,IPL_DEPTH_32F,3);  pscratch=cvCreateImage(size,IPL_DEPTH_32F,3); pscratch2=cvCreateImage(size,IPL_DEPTH_32F,3); plow1=cvCreateImage(size,IPL_DEPTH_32F,1); plow2=cvCreateImage(size,IPL_DEPTH_32F,1); plow3=cvCreateImage(size,IPL_DEPTH_32F,1);  phi1=cvCreateImage(size,IPL_DEPTH_32F,1); phi2=cvCreateImage(size,IPL_DEPTH_32F,1); phi3=cvCreateImage(size,IPL_DEPTH_32F,1); pgray1=cvCreateImage(size,IPL_DEPTH_32F,1); pgray2=cvCreateImage(size,IPL_DEPTH_32F,1); pgray3=cvCreateImage(size,IPL_DEPTH_32F,1); pmaskt=cvCreateImage(size,IPL_DEPTH_8U,1); pForeImg=cvCreateImage(size,IPL_DEPTH_8U,1); tmpNum=1; cvConvertScale(pFrame,pscratch,1.0,0); cvCopy(pscratch,pprevf);}void StatAverageModel::setHighThreshold(float scale){ cvConvertScale(pdiffF,pscratch,scale); cvAdd(pscratch,pavgF,pHif); cvSplit(pHif,phi1,phi2,phi3,NULL);}void StatAverageModel::setLowThreshold(float scale){ cvConvertScale(pdiffF,pscratch,scale); cvAdd(pscratch,pavgF,plowF); cvSplit(plowF,plow1,plow2,plow3,NULL);}void StatAverageModel::Detect(IplImage* frame,float highScale,float lowScale){ cvZero(pscratch); cvConvertScale(frame,pscratch,1.0,0); if (tmpNum<=gIteration) {  cvAcc(pscratch,pavgF);  cvAbsDiff(pscratch,pprevf,pscratch2);  cvAcc(pscratch2,pdiffF);  tmpNum++;  if (tmpNum==gIteration)  {   cvConvertScale(pavgF,pavgF,(double)(1.0/gIteration));//均值   cvConvertScale(pdiffF,pdiffF,(double)(1.0/gIteration));//累積差值的均值代替標準差   cvAddS(pdiffF,cvScalar(1.0,1.0,1.0),pdiffF);     cvCopy(pscratch,pprevf);   setHighThreshold(highScale);   setLowThreshold(lowScale);  }  cvCopy(pscratch,pprevf);   } else {  cvSplit(pscratch,pgray1,pgray2,pgray3,NULL);  cvInRange(pgray1,plow1,phi1,pForeImg);  cvInRange(pgray2,plow2,phi2,pmaskt);  cvOr(pmaskt,pForeImg,pForeImg);  cvZero(pmaskt);  cvInRange(pgray3,plow3,phi3,pmaskt);  cvOr(pmaskt,pForeImg,pForeImg);  cvSubRS(pForeImg,cvScalar(255,0,0),pForeImg); }}StatAverageModel::~StatAverageModel(){ cvReleaseImage(&pFrame); cvReleaseImage(&pavgF); cvReleaseImage(&pdiffF); cvReleaseImage(&pprevf); cvReleaseImage(&pHif); cvReleaseImage(&plowF); cvReleaseImage(&pscratch); cvReleaseImage(&pscratch2); cvReleaseImage(&pgray1); cvReleaseImage(&pgray2); cvReleaseImage(&pgray3); cvReleaseImage(&plow1); cvReleaseImage(&plow2); cvReleaseImage(&plow3); cvReleaseImage(&phi1); cvReleaseImage(&phi2); cvReleaseImage(&phi3); cvReleaseImage(&pmaskt); cvReleaseImage(&pForeImg);}/* 調用例子: // default value high=7.0,low=6.0 StatAverageModel* pInstance=new StatAverageModel(pCapture,100); while(pFrame=cvQueryFrame(pCapture)) {   FrmNum++;  pInstance->Detect(pFrame,7.0,6.0);  pForeImg=pInstance->getForeground();  pForeImg->origin=1;  cvShowImage("frame",pFrame);  cvShowImage("fore",pForeImg);    cvWaitKey(5);  }*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.