Why imshow don't show all my frames? (UAV Surveillance System)

asked 2017-10-26 16:53:53 -0600

Maroquio gravatar image

Hi, everyone.
I'm doing a background subtraction for a UAV surveillance system using a video generated by a 3D simulator developed by myself (the captured video is here). The problem happens when I try to process the frames from the captured video and try to show it using imshow. Some frames aren't showed in the window. Almost every second the image seems to freeze. The execution results showing the issue can be seen here. My entire code is below:


#include <opencv2/opencv.hpp>
#include <time.h>

using namespace cv;
using namespace std;

const char* videoFile = "C:\\\captured_video.mp4";

Mat frame, foregroundMask, kernel;
clock_t t1, t2;

long timediff(clock_t t1, clock_t t2) {
    long elapsed;
    elapsed = ((double)t2 - t1) / CLOCKS_PER_SEC * 1000;
    return elapsed;
}

int main(int, char** argv)
{
    system("mode 45, 20");

    VideoCapture video(videoFile);
        if (!video.isOpened())
    {
        cout << "Video file couldn't be opened." << endl;
        return 1;
    }

    Ptr<BackgroundSubtractorMOG2> subtractor =
        createBackgroundSubtractorMOG2(500, 16, false); 
    subtractor->setNMixtures(5);
    kernel = getStructuringElement(MORPH_CROSS, Size(3, 3));    

    printf("Start video processing...");
    t1 = clock();
    while (true)
    {
        if (video.read(frame)) {
            subtractor->apply(frame, foregroundMask);
            erode(foregroundMask, foregroundMask, kernel);
            dilate(foregroundMask, foregroundMask, kernel, Point(-1, -1), 2);
            Mat frameCopy = foregroundMask.clone();         
            imshow("Viewer", frameCopy);
            if (waitKey(1) == 27) break;
        }
        else { break; }
    }
    t2 = clock();
    printf("Processing finished.");

    long elapsed = timediff(t1, t2);    
    printf("Elapsed time: %d milliseconds\n", elapsed);
    waitKey();
    return 0;
}

I hope someone can help me. This is really delaying my work...
Thanks in advance and best regards!


edit retag flag offensive close merge delete

Comments

Try increasing the delay in waitKey by a bit. 10 should be good.

If you want real-time display though, imshow is not good. It's fine for debugging or showing static output, but it has a lot of overhead, and it doesn't update very fast. You should probably look around for GUI frameworks if you want real-time display.

Tetragramm gravatar imageTetragramm ( 2017-10-26 20:20:41 -0600 )edit

also:

 subtractor->setNMixtures(5);

that's a lot. will take ages to process. please rather stay with the default(3).

i guess, you're stalling your system so much, that it can't do anything else (like drawing) any more.

berak gravatar imageberak ( 2017-10-27 00:30:38 -0600 )edit

I've tried these settings before write the post, but the problem continues. The lag has reduced from 1s step to 2s step. I think that is something related to keyframes in the screen capture software (Camtasia Studio 9). I'm trying different capture settings and the lag step time changes, so, probably it is something related to that. I hope...

Maroquio gravatar imageMaroquio ( 2017-10-27 08:31:19 -0600 )edit

I faced a similar issue when loading a HD video. Two options improved the performance. Option 1: Shrink the video down to 480p then loaded it as a whole. Option 2: I used the 720p video but resized the frames instead.

eshirima gravatar imageeshirima ( 2017-10-27 11:42:57 -0600 )edit