基于OPENCV的背景减除方法
2007-06-24 19:30
537 查看
//经实验发现,用背景减除方法也可以针对某些摄像机运动的情况,而非像大部分文章中所提到的该方法仅仅适合于摄像机静止的情况,而且效果还要较好!!
/*
*建立多高斯背景模型
*/
void CMotionAnalysisPlatformView::OnBackgdiff()
{
// TODO: Add your command handler code here
IplImage* tmp_frame = 0;
IplImage* dst = 0;
if(!m_capture)
{
MessageBox("读取视频文件失败,请重新打开真彩色(24位)视频!", "错误信息");
return;
}
tmp_frame = cvQueryFrame(m_capture);
dst = cvCreateImage(cvGetSize(tmp_frame), 8, 1);
dst->origin = 1;
if(!tmp_frame)
{
MessageBox("读取视频文件失败,请重新打开真彩色(24位)视频!", "错误信息");
return;
}
cvNamedWindow("背景图像", 1);
cvNamedWindow("前景图像", 1);
//创建多高斯模型
CvBGStatModel* bg_model = cvCreateGaussianBGModel(tmp_frame);
for( int fr = 1;tmp_frame; tmp_frame = cvQueryFrame(m_capture), fr++ )
{
//printf("frame# %d : ", fr);
//获得背景模型更新过程所花费的时间,以 CPU时钟/每微秒 为单位计数
//double t = (double)cvGetTickCount(); //模型更新之前
cvUpdateBGStatModel( tmp_frame, bg_model );
//真正的函数实现体是在: icvUpdateFGDStatModel(tmp_frame, bg_model)
// t = (double)cvGetTickCount() - t; //模型更新之后
// 以左下角为坐标原点
bg_model->foreground->origin = bg_model->background->origin = 1;
//cvErode(bg_model->background, bg_model->background);
//滤除噪声
//cvErode(bg_model->foreground, bg_model->foreground);
//滤除噪声
//printf( "%.1f毫秒n", t/(cvGetTickFrequency()*1000.) );
//最后输出结果以毫秒为单位
cvShowImage("背景图像", bg_model->background);
cvCopy(bg_model->foreground, dst);
cvSmooth(dst, dst, CV_GAUSSIAN, 5);
cvMorphologyEx( dst, dst, 0, 0, CV_MOP_CLOSE, 3);
cvMorphologyEx( dst, dst, 0, 0, CV_MOP_OPEN, 1 );
//提取轮廓 到contour序列中
//cvFindContours 仅能处理 [单通道、颜色深度为8位的图像] 的轮廓提取
cvFindContours( dst,
m_storage, &m_contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
for( ; m_contour != 0; m_contour = m_contour->h_next )
{
CvScalar color = CV_RGB( 255, 0, 255 );
CvScalar color_rect = CV_RGB( 0, 255, 255);
m_contour_rect = cvBoundingRect(m_contour, 1);
/*if(m_contour_rect.height + m_contour_rect.width > 100 && m_contour_rect.width * m_contour_rect.height > 20 && m_contour_rect.height/m_contour_rect.width > 1.5)
*/
if(fabs(cvContourArea(m_contour)) > 500.0)
{
cvRectangle(dst, cvPoint(m_contour_rect.x, m_contour_rect.y), cvPoint((m_contour_rect.x + m_contour_rect.width),(m_contour_rect.y + m_contour_rect.height)), color_rect, 2, 8, 0);
/* replace CV_FILLED with 1 to see the outlines */
cvDrawContours( dst, m_contour, color, color, -1, CV_FILLED, 8, cvPoint(0,0));
cvShowImage("前景图像", dst);
// cvWaitKey(0);
}
}
int k = cvWaitKey(1); //等待一毫秒
if( k == 'q' ) break;
}
cvReleaseBGStatModel( &bg_model );
cvReleaseCapture(&m_capture);
cvReleaseImage(&dst);
if(m_storage) cvClearMemStorage(m_storage);
cvDestroyWindow("背景图像");
cvDestroyWindow("前景图像");
return;
}
/*
*建立多高斯背景模型
*/
void CMotionAnalysisPlatformView::OnBackgdiff()
{
// TODO: Add your command handler code here
IplImage* tmp_frame = 0;
IplImage* dst = 0;
if(!m_capture)
{
MessageBox("读取视频文件失败,请重新打开真彩色(24位)视频!", "错误信息");
return;
}
tmp_frame = cvQueryFrame(m_capture);
dst = cvCreateImage(cvGetSize(tmp_frame), 8, 1);
dst->origin = 1;
if(!tmp_frame)
{
MessageBox("读取视频文件失败,请重新打开真彩色(24位)视频!", "错误信息");
return;
}
cvNamedWindow("背景图像", 1);
cvNamedWindow("前景图像", 1);
//创建多高斯模型
CvBGStatModel* bg_model = cvCreateGaussianBGModel(tmp_frame);
for( int fr = 1;tmp_frame; tmp_frame = cvQueryFrame(m_capture), fr++ )
{
//printf("frame# %d : ", fr);
//获得背景模型更新过程所花费的时间,以 CPU时钟/每微秒 为单位计数
//double t = (double)cvGetTickCount(); //模型更新之前
cvUpdateBGStatModel( tmp_frame, bg_model );
//真正的函数实现体是在: icvUpdateFGDStatModel(tmp_frame, bg_model)
// t = (double)cvGetTickCount() - t; //模型更新之后
// 以左下角为坐标原点
bg_model->foreground->origin = bg_model->background->origin = 1;
//cvErode(bg_model->background, bg_model->background);
//滤除噪声
//cvErode(bg_model->foreground, bg_model->foreground);
//滤除噪声
//printf( "%.1f毫秒n", t/(cvGetTickFrequency()*1000.) );
//最后输出结果以毫秒为单位
cvShowImage("背景图像", bg_model->background);
cvCopy(bg_model->foreground, dst);
cvSmooth(dst, dst, CV_GAUSSIAN, 5);
cvMorphologyEx( dst, dst, 0, 0, CV_MOP_CLOSE, 3);
cvMorphologyEx( dst, dst, 0, 0, CV_MOP_OPEN, 1 );
//提取轮廓 到contour序列中
//cvFindContours 仅能处理 [单通道、颜色深度为8位的图像] 的轮廓提取
cvFindContours( dst,
m_storage, &m_contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
for( ; m_contour != 0; m_contour = m_contour->h_next )
{
CvScalar color = CV_RGB( 255, 0, 255 );
CvScalar color_rect = CV_RGB( 0, 255, 255);
m_contour_rect = cvBoundingRect(m_contour, 1);
/*if(m_contour_rect.height + m_contour_rect.width > 100 && m_contour_rect.width * m_contour_rect.height > 20 && m_contour_rect.height/m_contour_rect.width > 1.5)
*/
if(fabs(cvContourArea(m_contour)) > 500.0)
{
cvRectangle(dst, cvPoint(m_contour_rect.x, m_contour_rect.y), cvPoint((m_contour_rect.x + m_contour_rect.width),(m_contour_rect.y + m_contour_rect.height)), color_rect, 2, 8, 0);
/* replace CV_FILLED with 1 to see the outlines */
cvDrawContours( dst, m_contour, color, color, -1, CV_FILLED, 8, cvPoint(0,0));
cvShowImage("前景图像", dst);
// cvWaitKey(0);
}
}
int k = cvWaitKey(1); //等待一毫秒
if( k == 'q' ) break;
}
cvReleaseBGStatModel( &bg_model );
cvReleaseCapture(&m_capture);
cvReleaseImage(&dst);
if(m_storage) cvClearMemStorage(m_storage);
cvDestroyWindow("背景图像");
cvDestroyWindow("前景图像");
return;
}
相关文章推荐
- 基于OPENCV的背景减除方法
- 【OpenCV】视频/图像背景减除方法
- 一种基于特征点匹配的图像检索方法的opencv实现
- MFC中基于OpenCV实现Picture Control控件成像方法
- 【计算机视觉】基于样本一致性的背景减除运动目标检测算法(SACON)
- OpenCV_基于自适应背景更新的运动目标检测
- 基于opencv2.0的haar算法以人脸识别为例的训练分类器xml的方法 .
- opencv:基于不变矩方法的图像特征提取
- OpenCV背景去除的几种方法
- OpenCV背景去除的几种方法
- 基于OpenCV批量处理文件夹中的图片的方法
- OpenCV背景去除的几种方法
- 基于OpenCV的程序脱离动态链接库运行方法
- 基于OpenCV的程序脱离动态链接库运行方法 .
- 【OPENCV】基于背景差法的运动目标检测
- 学习opencv——背景减除
- 基于样本一致性的背景减除运动目标检测算法(SACON)
- opencv2.4.4 背景减除算法收集
- 基于opencv的一种局部自适应快速二值化方法(积分法)
- 基于OpenCV的背景消除算法[转]