我想在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;
}
为什么会发生这种情况?
这是因为您不拥有cvQueryFrame
的结果,cvCapture
结构拥有。如果你想对结果做点什么,你必须先复印一份。你看到两个不同帧的原因是因为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;
}
}