2013-05-06 67 views
0

我想從相同視頻的previous幀中減去current幀的視頻,並對所有視頻和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; 
} 

回答

1

下面是使用新的OpenCV 2.x的界面例子。此外,在您的代碼中,您同時使用IplImageMat。在一般情況下,使用的OpenCV的C++接口,除非你有使用C.

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); 
} 
+0

如果你對此有何評論做視頻處理,在這裏我開始寫的視頻作爲新的視頻? – Rocket 2013-05-06 15:37:17

+0

我只是想告訴你如何保存當前幀和前一幀,以便它可以在你的循環中使用,因爲那是你的問題。是的,你可以寫入文件框架或做你需要的任何處理。此外,在第一次迭代中'previous_frame'沒有被初始化,所以你需要確保你不使用它(只在第一次迭代中)。 – Alexey 2013-05-06 15:46:30

+0

我很難理解你的代碼 – Rocket 2013-05-12 15:34:21

相關問題