【问题标题】:Problem with foreground extraction using opencv c++ and MS VS2010使用 opencv c++ 和 MS VS2010 提取前景的问题
【发布时间】:2011-06-18 09:37:46
【问题描述】:

我在从视频帧中提取前景时遇到问题。 它也提取了一些背景对象。我从示例视频中拍摄了一张快照以用作背景图像。建议我应该做什么。是否为背景图像拍摄单独的图像而不是从视频中拍摄快照(假设,snapshot 比图片分辨率低)或其他更好的代码。用于提取的代码是

int _tmain(int argc, _TCHAR* argv[])
{   

    IplImage  *frame = NULL;
    IplImage  *img1 = NULL;

    IplImage  *grey  = NULL;
    IplImage  *edges = NULL;
        int delay = 0, key=0, i=0;
    CvCapture *video = NULL;
    CvCapture *video1 = NULL;

        cvNamedWindow("window_name");

        video = cvCreateFileCapture("sample.avi");
        video1 = cvCreateFileCapture("sample.avi");


    frame = cvQueryFrame(video);
    img1 = cvQueryFrame(video1);
       grey  = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
       edges  = cvCreateImage(cvGetSize(img1), IPL_DEPTH_8U, 1);

  cvShowImage("backgrnd", img1);

//get height and width using OpenCV functions
 const int &rows = img1->width;
 const int &cols =frame->height;
  BYTE *Pixel2=0;
  cvGetRawData(img1,&Pixel2,0,0);
         while (frame) {


 BYTE *Pixel1=0;
//extract pixels using the OpenCV function cvGetRawData

 cvGetRawData(frame,&Pixel1,0,0);

//register int to increase the speed
register  int r,ri,c;

 //to find diffrence of 2 images by pixel to pixel comparision
for(r = 0, ri = 0; r < rows*3; r++, ri += cols)
 {
     for(c = 0; c < cols; c++)
         {
             //get the difference in pixels
             Pixel1[ri + c] = Pixel1[ri + c] - Pixel2[ri + c];

             //set threshold value as 100 for comparision, it can be changed to values between 50 and 200, for getting binary image
             if(Pixel1[ri + c] < 150)
                 {
                     Pixel1[ri + c]=0;
                 }
             else   
                    Pixel1[ri + c]=255;   

         }//for c

 }//for r, ri   
Return 0;
}

【问题讨论】:

  • 请学习格式化/缩进你的代码
  • 能把输入输出的截图也发一下吗?很难想象你期望代码应该做什么。

标签: c++ opencv foreground


【解决方案1】:

前景/背景分割是一个深刻而困难的问题。要问自己的第一件也是最重要的事情是“与背景相比,前景的定义是什么?”有时背景会随着时间的推移而发生巨大变化,使得前景/背景分割变得特别困难。根据您的回答,有多种方法可以很好地工作。有些是统计性质的,处理给定像素处颜色的概率分布,或处理从图像中提取的特征。一些尝试跟踪视频中的特征并根据各种移动特征的动态对图像进行分割。还有一些在本质上更代数,基于主成分分析的变体。那里有大量优秀的算法,但现在没有人使用帧差分。

如果您使用的是OpenCV,我强烈建议您使用OpenCV2.2,这是最新的新闻。在OpenCV2.2video 模块中,有几个很好的前景/背景分割工具。代码中的文档甚至会为您指出描述各种算法背后的理论的参考资料和论文。

祝你好运!

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多