| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 545 人关注过本帖
标题:《基于帧间差分与背景差分下的运动目标检测》 求大神注释尽量详细点
取消只看楼主 加入收藏
lxhaisiwhb
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-5-26
结帖率:0
收藏
已结贴  问题点数:10 回复次数:0 
《基于帧间差分与背景差分下的运动目标检测》 求大神注释尽量详细点
#include <stdio.h>
#include <tchar.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <cvaux.h>
#include <cvcam.h>

#define max(a, b) ((a)>(b)?(a):(b))

//调整矩形框B,使其在A的范围
CvRect my_ChangeRect(CvRect A,CvRect B)
{
   if(B.x<A.x)
      B.x=A.x;
   if(B.x+B.width>A.width)
      B.width=A.width-B.x;
   if(B.y<A.y)
      B.y=A.y;
   if(B.y+B.height>A.height)
      B.height=A.height-B.y;
   return(B);
}

int main( int argc, char** argv )
{
   
    //声明IplImage指针
    IplImage* pFrame = NULL;//图像帧
    IplImage* pFrImg = NULL;//前景图像
    IplImage* pBkImg = NULL;//背景图像
    IplImage* pBackproject = NULL;//反向投影图像
    IplImage* pImage = NULL;//视频帧图像
    IplImage* pTraje = NULL;//轨迹图
    IplImage* hsv = NULL;
    IplImage* hue = NULL;
    IplImage* mask = NULL;
   
    //声明点集变量
    CvPoint lastpoint;//上一帧的中心坐标
    CvPoint predictpoint;//预测坐标
    CvPoint measurepoint;//当前帧的坐标
    CvPoint origin;
    CvPoint point_text;

    //声明矩形框变量
    CvRect origin_box;
    CvRect track_window;
    CvRect search_window;
    CvBox2D track_box;//跟踪返回的区域box,带有角度
    CvConnectedComp track_comp;
   
    //声明文本变量
    CvFont font;//显示的文本字体
    char nchar[10];//显示数字字符串
   
    //声明矩阵类型变量
    CvMat* measurement;
    CvMat* realposition;
    const CvMat* prediction;

    CvCapture* pCapture = NULL;
    CvBGStatModel *bg_model = NULL;
    CvMemStorage * m_storage = cvCreateMemStorage(0);
    CvSeq *m_contour = NULL;
    int nFrmNum = 0;
    int i = 0;      
   
    cvNamedWindow("视频图像", 1);
    cvNamedWindow("背景图像", 1);//该背景图像指的是经Gassian(简单背景差分,帧间差分)得到的当前综合背景
    cvNamedWindow("前景图像", 1);
        
    cvMoveWindow("视频图像", 30, 0);
    cvMoveWindow("背景图像", 450, 0);
    cvMoveWindow("前景图像", 900, 0);
   
    pCapture = cvCaptureFromFile("001.avi");    //换视频文件的地方,改引号里面的文件名就好
   
    while(pFrame = cvQueryFrame(pCapture))
    {
        pImage = cvCreateImage( cvGetSize(pFrame), 8, 3);
        pImage->origin = pFrame->origin;
        hsv = cvCreateImage(cvGetSize(pFrame), 8, 3);
        hue = cvCreateImage(cvGetSize(pFrame), 8, 1);
        mask = cvCreateImage(cvGetSize(pFrame), 8, 1);

        pBackproject = cvCreateImage(cvGetSize(pFrame), 8, 1);
        pBackproject->origin = pFrame->origin;

        pTraje = cvCreateImage( cvGetSize(pFrame), 8, 1);
        pTraje->origin = pFrame->origin;

        cvCopy(pFrame, pImage, 0);
        cvCvtColor(pImage, hsv, CV_BGR2HSV);
        nFrmNum++;
        i++;

        //高斯建模部分
        if(nFrmNum==1)
        {
            pBkImg = cvCreateImage(cvGetSize(pFrame), IPL_DEPTH_8U, 3);
            pFrImg = cvCreateImage(cvGetSize(pFrame), IPL_DEPTH_8U, 1);
            bg_model = (CvBGStatModel*)cvCreateGaussianBGModel(pFrame, 0);
        }
        else
        {
            cvUpdateBGStatModel( pFrame, bg_model);
            
            cvErode(bg_model->foreground, bg_model->foreground,0,1); //腐蚀
            cvDilate(bg_model->foreground, bg_model->foreground,0,1);//膨胀   
            
            cvCopy(bg_model->foreground, pFrImg);
            cvCopy(bg_model->background, pBkImg);

            pBkImg->origin=1;
            pFrImg->origin=1;
            
            //高级形态学变换
            cvMorphologyEx( pFrImg, pFrImg, 0, 0, CV_MOP_CLOSE, 1);//闭运算
            cvMorphologyEx( pFrImg, pFrImg, 0, 0, CV_MOP_OPEN, 1 );//开运算
         }
          cvShowImage("视频图像", pFrame);
          cvShowImage("背景图像", pBkImg);
        
        //提取轮廓 到contour序列中
        
          cvFindContours( pFrImg,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( 100, 0, 100 );
              CvScalar color_rect = CV_RGB( 0, 255, 255);
              CvPoint PointArray[1250];
              CvPoint* pt;
              pt = PointArray;
            
              CvRect m_contour_rect = cvBoundingRect(m_contour, 1);
                          
              if((30 < m_contour_rect.width < 100) && (30 < m_contour_rect.height < 100) && (m_contour_rect.height < m_contour_rect.width < (1.5 * m_contour_rect.height)))
              {
                  cvRectangle(pFrImg, 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);
               
                  cvDrawContours( pFrImg, m_contour, color, color, -1, CV_FILLED, 8, cvPoint(0,0));
                  cvShowImage("前景图像",  pFrImg);
               
              }
           }

           int c = cvWaitKey(33);
           if(c == 'p')
               c = cvWaitKey(0);
           if(c == 's')
               c = cvWaitKey(33);
           if(c == 'q')
            break;
    }
   
    cvReleaseBGStatModel( &bg_model );
    cvReleaseCapture(&pCapture);
    cvReleaseImage(&pFrImg);
    cvReleaseImage(&pBkImg);
    cvReleaseImage(&pImage);
    cvReleaseImage(&pTraje);
    if(m_storage) cvClearMemStorage(m_storage);
    cvDestroyWindow("视频图像");
    cvDestroyWindow("背景图像");
    cvDestroyWindow("前景图像");
    return 0;
搜索更多相关主题的帖子: 检测 return include 
2013-05-26 22:10
快速回复:《基于帧间差分与背景差分下的运动目标检测》 求大神注释尽量详细点 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038620 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved