资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
/* 比平均背景法性能更加良好的方法,codeBook 模型实现背景减除 核心代码详细解析和实现 by zcube */ /*/ /* A few more thoughts on codebook models In general, the codebook method works quite well across a wide number of conditions, and it is relatively quick to train and to run. It doesnt deal well with varying patterns of light such as morning, noon, and evening sunshine or with someone turning lights on or off indoors. This type of global variability can be taken into account by using several different codebook models, one for each condition, and then allowing the condition to control which model is active. */ /*/ #include “stdafx.h“ #include #include #include #define CHANNELS 3 / 设置处理的图像通道数,要求小于等于图像本身的通道数 / / 下面为码本码元的数据结构 / 处理图像时每个像素对应一个码本,每个码本中可有若干个码元 / 当涉及一个新领域,通常会遇到一些奇怪的名词,不要被这些名词吓坏,其实思路都是简单的 typedef struct ce uchar learnHighCHANNELS; / High side threshold for learning / 此码元各通道的阀值上限(学习界限) uchar learnLowCHANNELS; / Low side threshold for learning / 此码元各通道的阀值下限 / 学习过程中如果一个新像素各通道值 xi,均有 learnLowit_last_update = c.t; / 更新该码元时间为当前时间 / adjust this codeword for the first channel for (n=0; nmaxn maxn = *(p+n); else if (c.cbi-minn *(p+n) c.cbi-minn = *(p+n); break; / ENTER A NEW CODE WORD IF NEEDED if(i = c.numEntries) / No existing code word found, make a new one / p 像素不满足此码本中任何一个码元,下面创建一个新码元 code_element *foo = new code_element* c.numEntries+1; / 申请 c.numEntries+1 个指向码元的指针 for(int ii=0; iilearnLown = lown; c.cbc.numEntries-maxn = *(p+n); c.cbc.numEntries-minn = *(p+n); c.cbc.numEntries-t_last_update = c.t; c.cbc.numEntries-stale = 0; c.numEntries += 1; / OVERHEAD TO TRACK POTENTIAL STALE ENTRIES for(int s=0; st_last_update; / 计算该码元的不更新时间 if(c.cbs-stale stale = negRun; / SLOWLY ADJUST LEARNING BOUNDS for(n=0; nlearnHighn learnHighn += 1; if(c.cbi-learnLown lown) c.cbi-learnLown -= 1; return(i); / / / uchar cvbackgroundDiff(uchar *p, codeBook /SEE IF THIS FITS AN EXISTING CODEWORD int i; for (i=0; i staleThresh) / 如码元中的不更新时间大于设定的刷新时间,则标记为删除 keepi = 0; /Mark for destruction else keepi = 1; /Mark to keep keepCnt += 1; / KEEP ONLY THE GOOD c.t = 0; /Full reset on stale tracking / 码本时间清零 code_element *foo = new code_element* keepCnt; / 申请大小为 keepCnt 的码元指针数组 int k=0; for(int ii=0; iistale = 0; /We have to refresh these entries for next clearStale fook-t_last_update = 0; k+; /CLEAN UP delete keep; delete c.cb; c.cb = foo; / 把 foo 头指针地址赋给 c.cb int numCleared = c.numEntries - keepCnt; / 被清理的码元个数 c.numEntries = keepCnt; / 剩余的码元地址 return(numCleared); int main() / / 需要使用的变量 CvCapture* capture; IplImage* rawImage; IplImage* yuvImage; IplImage* ImaskCodeBook; codeBook* cB; unsigned cbBoundsCHANNELS; uchar* pColor; /YUV pointer int imageLen; int nChannels = CHANNELS; int minModCHANNELS; int maxModCHANNELS; / / 初始化各变量 cvNamedWindow(“Raw“); cvNamedWindow(“CodeBook“); capture = cvCreateFileCapture(“tree.avi“); if (!capture) printf(“Couldnt open the capture!“); return -1; rawImage = cvQueryFrame(capture); yuvImage = cvCreateImage(cvGetSize(rawImage), 8, 3); / 给 yuvImage 分配一个和 rawImage 尺寸相同,8 位 3 通道图像 ImaskCodeBook = cvCreateImage(cvGetSize(rawImage), IPL_DEPTH_8U, 1); / 为 ImaskCodeBook 分配一个和 rawImage 尺寸相同,8 位单通道图像 cvSet(ImaskCodeBook, cvScalar(255); / 设置单通道数组所有元素为 255,即初始化为白色图像 imageLen = rawImage-width * rawImage-height; cB = new codeBookimageLen; / 得到与图像像素数目长度一样的一组码本,以便对每个像素进行处理 for (int i=0; iimageData); /1 channel image / 指向 ImaskCodeBook 通道数据序列的首元素 for(int c=0; cimageLen; c+) maskPixelCodeBook = cvbackgroundDiff(pColor, cBc, nChannels, minMod, maxMod); / 我看到这儿时豁然开朗,开始理解了 codeBook 呵呵 *pMask+ = maskPixelCodeBook; pColor += 3; / pColor 指向的是 3 通道图像 if (!(rawImage = cvQueryFrame(capture) break; cvShowImage(“Raw“, rawImage); cvShowImage(“CodeBook“, ImaskCodeBook); if (cvWaitKey(30) = 27) break; if (i = 56 | i = 63) cvWaitKey(); cvReleaseCapture( if (yuvImage) cvReleaseImage( if(ImaskCodeBook) cvReleaseImage( cvDestroyAllWindows(); delete cB; return 0;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号