0

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

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;
}
4

1 回答 1

0

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

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

祝你好运!

于 2011-02-02T08:17:04.543 回答