Analyze the frames of a video without performing the complete loop

Hello community, is it possible to analyze the frames of a video without performing the complete loop (update() and draw())?

I’m trying for a few days to make a program that is close to a motion video capture. I would like to keep only the frames that are active. For the moment there is a problem when I use Video_source.nextframe() in the while loop the Video_source.isFrameNew() becomes inactive. Here is the code in question, any suggestion will be appreciated.

Thank you so much!

void ofApp::update(){
    if (Video_source.isFrameNew()){
        int Frame_to_show = 0 ;
        while(Frame_to_show == 0 ){
            IMG_source_cv.allocate(Video_source.getWidth() ,Video_source.getHeight());
            IMG_source_cv.resize( VIDEO_width  , VIDEO_height );
            IMG_gray_cv = IMG_source_cv;
            IMG_gray_diff_cv.absDiff(IMG_last_gray_cv, IMG_gray_cv);
            IMG_last_gray_cv = IMG_gray_cv;
            unsigned int Total_changed_pixel = 0 ;
            unsigned int size =  IMG_gray_diff_cv.getWidth() * IMG_gray_diff_cv.getHeight();
            for(int i = 0; i < size ; i++) if(  IMG_gray_diff_cv.getPixels()[i] > 0)Total_changed_pixel  ++;
            if(Total_changed_pixel  > 1000) Total_changed_pixel  = 1000;
            MOTION_value = ofMap(Total_changed_pixel , 0 , 1000, 0,100);
            if(MOTION_value > Threshold2 ) {
                Frame_to_show = 1;
                IMG_action_cv = IMG_gray_cv;
            else {
            cout << Video_source.getCurrentFrame() << " " << Frame_to_show << endl;

Hi @Samset , can you omit the while loop and just analyze a single new frame of video in each cycle of ofApp::update()? If a frame doesn’t pass the test, the app could move along and process more frames in additional cycles until it finds one that passes the test.

Also, allocating and resizing images every frame can slow things down quite a bit. IMG_source_cv could be allocated once in ofApp::setup(). Drawing something into an ofFbo can be a quick way to resize and/or crop it.

Hi @Samset that is tied to the video implementation. If you use GStreamer you can actually go to the next frame in a while loop.
Please find here some code for doing such. let me know if it works for you

1 Like