gpt4 book ai didi

visual-c++ - 如何从视频中保存下一帧和当前帧

转载 作者:行者123 更新时间:2023-12-02 17:52:39 26 4
gpt4 key购买 nike

我想减去 current来自 previous 的视频帧相同视频的帧,并对所有视频执行相同的操作,而不是 write those frame as seprate video ,我在保存当前帧而不是下一帧时感到困惑。如何保存它们,以便在下一帧到来时成为下一帧的前一帧

    IplImage* skipNFrames(CvCapture* capture, int n)
{

for(int i = 0; i < n; ++i)
{
if(cvQueryFrame(capture) == NULL)
{
return NULL;
}
else
{

char* New_image;
// New_image* = cvQueryFrame(capture) - cvCloneImage(capture);
Mat current_frame;
Mat previous_frame;

if (!capture.isOpened()) {
return -1;
}

while(1)
{
capture >> current_frame;
if (current_frame.empty())
break;
// do video processing

imshow("Window", current_frame);
waitKey(10);
frame = skipNFrames(capture, 1)
frame.copyTo(previous_frame);

int frame_width=(int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
int frame_height=(int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
//creating video writer structure
CvVideoWriter *writer = 0;
writer=cvCreateVideoWriter("savedVideo.avi",CV_FOURCC('M', 'P', '4', '2'),fps,cvSize(frame_width,frame_height),1);

if( !writer ) {
//release memory
cvReleaseCapture( &capture );
return 0;
}

while(true) {
//grab and retrieve each frames of the video sequentially
IplImage* frame = cvQueryFrame( New_image );
if( !frame ) break;

//writing frame by frame to the output file
cvWriteFrame(writer,frame);
}


cvReleaseVideoWriter(&writer);
cvReleaseCapture( &capture );

return 0;
}

}
// return cvNew_image
//return cvQueryFrame(capture);
}
int main(int argc, char* argv[])
{
CvCapture* capture = cvCaptureFromFile("try.avi");
char buffer[1000];
int flag=0;
IplImage* frame = NULL;
do
{
//frame = skipNFrames(capture, 1);
cvNamedWindow("frame", CV_WINDOW_AUTOSIZE);
cvShowImage("frame", frame);
cvWaitKey(0);
char *str=new char[50];
flag++;
sprintf(str,"%d",flag);
strcat(str," frame");
strcat(str,".png");
Mat image=frame;
imwrite(str,image);
} while( frame != NULL );

cvReleaseCapture(&capture);
cvDestroyWindow("frame");
cvReleaseImage(&frame);

return 0;
}

最佳答案

这是使用较新的 OpenCV 2.x 接口(interface)的示例。此外,在您的代码中,您同时使用 IplImageMat .通常,除非必须使用 C,否则请使用 OpenCV 的 C++ 接口(interface)。

VideoCapture cap;
Mat current_frame;
Mat previous_frame;
Mat result;

cap.open(0);
if (!cap.isOpened()) {
cerr << "can not open camera or video file" << endl;
return -1;
}

while(1)
{
cap >> current_frame;
if (current_frame.empty())
break;

if (! previous_frame.empty()) {
// subtract frames
subtract(current_frame, previous_frame, result);
}


imshow("Window", result);
waitKey(10);

frame.copyTo(previous_frame);
}

关于visual-c++ - 如何从视频中保存下一帧和当前帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16401102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com