0

我想在openCV中对视频帧进行帧比较。让我们说视频文件中的第 10 帧和第 100 帧。如果我在主视频捕获循环中执行此操作,它会起作用。但现在我想将其移至专用方法(将 IplImages 作为输入)。

 // MAIN CAPTURE LOOP
 while(1) {

        cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, 10);
        frame = cvQueryFrame(capture);
        if (!frame) break;
        cvShowImage("Window_1", frame);

        cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, 100);
        nextFrame = cvQueryFrame(capture);
        if (!nextFrame) break;

        cvShowImage("Window_2", nextFrame);  // THIS SHOWS ME TWO DIFFERENT FRAMES

        // PASS THE IMAGES TO THE NEW METHOD

        [self compareFrame: frame withFrame: nextFrame]);                       

        char c = cvWaitKey(5);  
        if(c==27) {
            NSLog(@"ESC pressed!");
            break;
        }

这是我的方法(简化)

- (int) compareFrame: (IplImage*) firstFrame withFrame: (IplImage*) secondFrame
{

// THIS SHOWS ME ONLY THE firstFrame IN BOTH      
// WINDOWS?

cvShowImage("Window_1", firstFrame);
cvShowImage("Window_2", secondFrame); 

return 1;

}

为什么会这样?

4

1 回答 1

3

这是因为你不拥有 的结果cvQueryFramecvCapture结构拥有。如果你想对结果做点什么,你必须先做一个副本。您看到两个不同框架的原因是因为cvShowImage在显示之前制作了自己的副本。到达调用位置[self compareFrame]后, frame 和 nextFrame 将指向相同的数据。

试试这个:

while (1) {
    cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, 10);
    frame =cvQueryFrame(capture);
    if (!frame) {
        NSLog(@"Couldn't read frame 10");
        break;
    };

    frame = cvCloneImage(frame); //Make a copy of the frame     

    cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, 100);
    nextFrame = cvQueryFrame(capture);
    if (!nextFrame) {
        NSLog(@"Couldn't read frame 100");
        break;
    };

    nextFrame = cvCloneImage(nextFrame); //Make a copy of the frame

    // PASS THE IMAGES TO THE NEW METHOD
    [self compareFrame: frame withFrame: nextFrame];
    char c = cvWaitKey();

    cvReleaseImage(&frame);
    cvReleaseImage(&nextFrame);
    if(c==27) {
        NSLog(@"ESC pressed!");
        break;
    }
}
于 2012-12-29T00:56:16.707 回答