Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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!