基於OpenCV的背景消除演算法[轉]
/*
*建立多高斯背景模型
*/
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;
}