<span style="color: rgb(51, 153, 51);">#include <stdio.h></span>
<span style="color: rgb(51, 153, 51);">#include <cv.h></span>
<span style="color: rgb(51, 153, 51);">#include <cxcore.h></span>
<span style="color: rgb(51, 153, 51);">#include <highgui.h></span>
<span style="color: rgb(51, 153, 51);">#include <cvaux.h>//必须引此头文件</span>
<span style="color: rgb(153, 51, 51);">int</span> main<span style="color: rgb(0, 153, 0);">(</span> <span style="color: rgb(153, 51, 51);">int</span> argc<span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(153, 51, 51);">char</span><span style="color: rgb(51, 153, 51);">**</span> argv <span style="color: rgb(0, 153, 0);">)</span>
<span style="color: rgb(0, 153, 0);">{</span>
IplImage<span style="color: rgb(51, 153, 51);">*</span> pFrame <span style="color: rgb(51, 153, 51);">=</span> <span style="color: rgb(0, 0, 0);"><strong>NULL</strong></span><span style="color: rgb(51, 153, 51);">;</span>
IplImage<span style="color: rgb(51, 153, 51);">*</span> pFrImg <span style="color: rgb(51, 153, 51);">=</span> <span style="color: rgb(0, 0, 0);"><strong>NULL</strong></span><span style="color: rgb(51, 153, 51);">;</span>
IplImage<span style="color: rgb(51, 153, 51);">*</span> pBkImg <span style="color: rgb(51, 153, 51);">=</span> <span style="color: rgb(0, 0, 0);"><strong>NULL</strong></span><span style="color: rgb(51, 153, 51);">;</span>
CvCapture<span style="color: rgb(51, 153, 51);">*</span> pCapture <span style="color: rgb(51, 153, 51);">=</span> <span style="color: rgb(0, 0, 0);"><strong>NULL</strong></span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(153, 51, 51);">int</span> nFrmNum <span style="color: rgb(51, 153, 51);">=</span> <span style="color: rgb(0, 0, 221);">0</span><span style="color: rgb(51, 153, 51);">;</span>
cvNamedWindow<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(255, 0, 0);">"video"</span><span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(0, 0, 221);">1</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvNamedWindow<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(255, 0, 0);">"background"</span><span style="color: rgb(51, 153, 51);">,</span><span style="color: rgb(0, 0, 221);">1</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvNamedWindow<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(255, 0, 0);">"foreground"</span><span style="color: rgb(51, 153, 51);">,</span><span style="color: rgb(0, 0, 221);">1</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvMoveWindow<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(255, 0, 0);">"video"</span><span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(0, 0, 221);">30</span><span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(0, 0, 221);">0</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvMoveWindow<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(255, 0, 0);">"background"</span><span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(0, 0, 221);">360</span><span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(0, 0, 221);">0</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvMoveWindow<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(255, 0, 0);">"foreground"</span><span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(0, 0, 221);">690</span><span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(0, 0, 221);">0</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(0, 153, 0);">(</span> argc <span style="color: rgb(51, 153, 51);">></span> <span style="color: rgb(0, 0, 221);">2</span> <span style="color: rgb(0, 153, 0);">)</span>
<span style="color: rgb(0, 153, 0);">{</span>
fprintf<span style="color: rgb(0, 153, 0);">(</span>stderr<span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(255, 0, 0);">"Usage: bkgrd [video_file_name]<span style="color: rgb(0, 0, 153);"><strong>\n</strong></span>"</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(51, 153, 51);">-</span><span style="color: rgb(0, 0, 221);">1</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(0, 153, 0);">}</span>
<span style="color: rgb(102, 102, 102);"><em>//打开视频文件 </em></span>
<span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(0, 153, 0);">(</span>argc <span style="color: rgb(51, 153, 51);">==</span> <span style="color: rgb(0, 0, 221);">2</span><span style="color: rgb(0, 153, 0);">)</span>
<span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(0, 153, 0);">(</span> <span style="color: rgb(51, 153, 51);">!</span><span style="color: rgb(0, 153, 0);">(</span>pCapture <span style="color: rgb(51, 153, 51);">=</span> cvCaptureFromFile<span style="color: rgb(0, 153, 0);">(</span>argv<span style="color: rgb(0, 153, 0);">[</span><span style="color: rgb(0, 0, 221);">1</span><span style="color: rgb(0, 153, 0);">]</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(0, 153, 0);">)</span>
<span style="color: rgb(0, 153, 0);">{</span>
fprintf<span style="color: rgb(0, 153, 0);">(</span>stderr<span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(255, 0, 0);">"Can not open video file %s<span style="color: rgb(0, 0, 153);"><strong>\n</strong></span>"</span><span style="color: rgb(51, 153, 51);">,</span> argv<span style="color: rgb(0, 153, 0);">[</span><span style="color: rgb(0, 0, 221);">1</span><span style="color: rgb(0, 153, 0);">]</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(51, 153, 51);">-</span><span style="color: rgb(0, 0, 221);">2</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(0, 153, 0);">}</span>
<span style="color: rgb(102, 102, 102);"><em>//打开摄像头 </em></span>
<span style="color: rgb(177, 177, 0);">if</span> <span style="color: rgb(0, 153, 0);">(</span>argc <span style="color: rgb(51, 153, 51);">==</span> <span style="color: rgb(0, 0, 221);">1</span><span style="color: rgb(0, 153, 0);">)</span>
<span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(0, 153, 0);">(</span> <span style="color: rgb(51, 153, 51);">!</span><span style="color: rgb(0, 153, 0);">(</span>pCapture <span style="color: rgb(51, 153, 51);">=</span> cvCaptureFromCAM<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(51, 153, 51);">-</span><span style="color: rgb(0, 0, 221);">1</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(0, 153, 0);">)</span>
<span style="color: rgb(0, 153, 0);">{</span>
fprintf<span style="color: rgb(0, 153, 0);">(</span>stderr<span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(255, 0, 0);">"Can not open camera.<span style="color: rgb(0, 0, 153);"><strong>\n</strong></span>"</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(51, 153, 51);">-</span><span style="color: rgb(0, 0, 221);">2</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(0, 153, 0);">}</span>
<span style="color: rgb(102, 102, 102);"><em>//初始化高斯混合模型参数</em></span>
CvGaussBGModel<span style="color: rgb(51, 153, 51);">*</span> bg_model<span style="color: rgb(51, 153, 51);">=</span><span style="color: rgb(0, 0, 0);"><strong>NULL</strong></span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(177, 177, 0);">while</span><span style="color: rgb(0, 153, 0);">(</span>pFrame <span style="color: rgb(51, 153, 51);">=</span> cvQueryFrame<span style="color: rgb(0, 153, 0);">(</span> pCapture <span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(0, 153, 0);">)</span>
<span style="color: rgb(0, 153, 0);">{</span>
nFrmNum<span style="color: rgb(51, 153, 51);">++;</span>
<span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(0, 153, 0);">(</span>nFrmNum <span style="color: rgb(51, 153, 51);">==</span> <span style="color: rgb(0, 0, 221);">1</span><span style="color: rgb(0, 153, 0);">)</span>
<span style="color: rgb(0, 153, 0);">{</span>
pBkImg <span style="color: rgb(51, 153, 51);">=</span> cvCreateImage<span style="color: rgb(0, 153, 0);">(</span>cvSize<span style="color: rgb(0, 153, 0);">(</span>pFrame<span style="color: rgb(51, 153, 51);">-></span>width<span style="color: rgb(51, 153, 51);">,</span> pFrame<span style="color: rgb(51, 153, 51);">-></span>height<span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">,</span> IPL_DEPTH_8U<span style="color: rgb(51, 153, 51);">,</span><span style="color: rgb(0, 0, 221);">3</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
pFrImg <span style="color: rgb(51, 153, 51);">=</span> cvCreateImage<span style="color: rgb(0, 153, 0);">(</span>cvSize<span style="color: rgb(0, 153, 0);">(</span>pFrame<span style="color: rgb(51, 153, 51);">-></span>width<span style="color: rgb(51, 153, 51);">,</span> pFrame<span style="color: rgb(51, 153, 51);">-></span>height<span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">,</span> IPL_DEPTH_8U<span style="color: rgb(51, 153, 51);">,</span><span style="color: rgb(0, 0, 221);">1</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(102, 102, 102);"><em>//高斯背景建模,pFrame可以是多通道图像也可以是单通道图像</em></span>
<span style="color: rgb(102, 102, 102);"><em>//cvCreateGaussianBGModel函数返回值为CvBGStatModel*,</em></span>
<span style="color: rgb(102, 102, 102);"><em>//需要强制转换成CvGaussBGModel*</em></span>
bg_model <span style="color: rgb(51, 153, 51);">=</span> <span style="color: rgb(0, 153, 0);">(</span>CvGaussBGModel<span style="color: rgb(51, 153, 51);">*</span><span style="color: rgb(0, 153, 0);">)</span>cvCreateGaussianBGModel<span style="color: rgb(0, 153, 0);">(</span>pFrame<span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(0, 0, 221);">0</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(0, 153, 0);">}</span>
<span style="color: rgb(177, 177, 0);">else</span>
<span style="color: rgb(0, 153, 0);">{</span>
<span style="color: rgb(102, 102, 102);"><em>//更新高斯模型</em></span>
cvUpdateBGStatModel<span style="color: rgb(0, 153, 0);">(</span>pFrame<span style="color: rgb(51, 153, 51);">,</span> <span style="color: rgb(0, 153, 0);">(</span>CvBGStatModel <span style="color: rgb(51, 153, 51);">*</span><span style="color: rgb(0, 153, 0);">)</span>bg_model <span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(102, 102, 102);"><em>//pFrImg为前景图像,只能为单通道</em></span>
<span style="color: rgb(102, 102, 102);"><em>//pBkImg为背景图像,可以为单通道或与pFrame通道数相同</em></span>
cvCopy<span style="color: rgb(0, 153, 0);">(</span>bg_model<span style="color: rgb(51, 153, 51);">-></span>foreground<span style="color: rgb(51, 153, 51);">,</span>pFrImg<span style="color: rgb(51, 153, 51);">,</span><span style="color: rgb(0, 0, 221);">0</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvCopy<span style="color: rgb(0, 153, 0);">(</span>bg_model<span style="color: rgb(51, 153, 51);">-></span>background<span style="color: rgb(51, 153, 51);">,</span>pBkImg<span style="color: rgb(51, 153, 51);">,</span><span style="color: rgb(0, 0, 221);">0</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(102, 102, 102);"><em>//把图像正过来</em></span>
pBkImg<span style="color: rgb(51, 153, 51);">-></span>origin<span style="color: rgb(51, 153, 51);">=</span><span style="color: rgb(0, 0, 221);">1</span><span style="color: rgb(51, 153, 51);">;</span>
pFrImg<span style="color: rgb(51, 153, 51);">-></span>origin<span style="color: rgb(51, 153, 51);">=</span><span style="color: rgb(0, 0, 221);">1</span><span style="color: rgb(51, 153, 51);">;</span>
cvShowImage<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(255, 0, 0);">"video"</span><span style="color: rgb(51, 153, 51);">,</span> pFrame<span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvShowImage<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(255, 0, 0);">"background"</span><span style="color: rgb(51, 153, 51);">,</span> pBkImg<span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvShowImage<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(255, 0, 0);">"foreground"</span><span style="color: rgb(51, 153, 51);">,</span> pFrImg<span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(0, 153, 0);">(</span> cvWaitKey<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(0, 0, 221);">2</span><span style="color: rgb(0, 153, 0);">)</span> <span style="color: rgb(51, 153, 51);">>=</span> <span style="color: rgb(0, 0, 221);">0</span> <span style="color: rgb(0, 153, 0);">)</span>
<span style="color: rgb(0, 0, 0);"><strong>break</strong></span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(0, 153, 0);">}</span>
<span style="color: rgb(0, 153, 0);">}</span>
<span style="color: rgb(102, 102, 102);"><em>//释放高斯模型参数占用内存 </em></span>
cvReleaseBGStatModel<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(0, 153, 0);">(</span>CvBGStatModel<span style="color: rgb(51, 153, 51);">**</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">&</span>bg_model<span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvDestroyWindow<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(255, 0, 0);">"video"</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvDestroyWindow<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(255, 0, 0);">"background"</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvDestroyWindow<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(255, 0, 0);">"foreground"</span><span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvReleaseImage<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(51, 153, 51);">&</span>pFrImg<span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvReleaseImage<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(51, 153, 51);">&</span>pBkImg<span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
cvReleaseCapture<span style="color: rgb(0, 153, 0);">(</span><span style="color: rgb(51, 153, 51);">&</span>pCapture<span style="color: rgb(0, 153, 0);">)</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 221);">0</span><span style="color: rgb(51, 153, 51);">;</span>
<span style="color: rgb(0, 153, 0);">}</span>
我们可以看到在这个网站上的代码因为上传时不小心而导致我们copy之后出现了大量的<格式>的内容,想要运行这段代码就不得不把这些类似的代码给去掉?那么这种问题如何解决呢?
我们可以通过word中的替换来高效解决这个问题;我们首先把代码复制到一个新word中,然后通过点击Ctrl+H,唤醒替换窗口,在查找内容中我们输入[<]*[>],然后在搜索选项中选择使用通配符,之后就可以高效的替换所有的<格式>了;另外关于其他的通配符选项可以参考通配符链接
在这个网页上,我们可以看到很多有效的通配符,这样结合word就可以帮助我们避免这种机械的劳动了。