0

I'm attempting to utilize OpenCV, C++, on my windows 10 system to record the screen as part of a larger program I am writing. I need the ability to record the display and save the recording for later review.

I was able to find this link on stackoverflow

How to capture the desktop in OpenCV (ie. turn a bitmap into a Mat)?

User john ktejik created a function that in essence completed exactly what I am looking to accomplish, short of saving the stream to file.

Now what I have always done in the past was once I've opened a connection to my webcam or a video file, I could simply create a VideoWriter Object and write the individual frames to file. I have attempted to do just that utilizing John's function to act as a video source.

int main (int argc, char **argv)
{
    HWND hwndDesktop = GetDesktopWindow ();
    int key = 0;
    int frame_width = 1920;
    int frame_height = 1080;

    VideoWriter video ("screenCap.avi", CV_FOURCC ('M', 'J', 'P', 'G'), 15, Size (frame_width, frame_height));

    while (key != 27)
    {

        Mat src = hwnd2mat (hwndDesktop);

        video.write (src);
        imshow ("Screen Capture", src);
        key = waitKey (27);
    }
    video.release ();
    destroyAllWindows ();
    return 0;
}

What I'm seeing as the output, is the file labeled "screenCap.avi", however the file is empty of video. The file saves as 16KB storage space.

John's function is on point, as it displays the frames just fine via imshow(), but doesn't seem to allow me to save them.

1 Answers1

0

So over the weekend I played with the software some more. And as I really don't have a firm grasp on it, I figured that there had to be a problem with settings between the screen capture and the file writer.

So I started looking at each of the lines in John's function. I came across

src.create(height, width, CV_8UC4);

It seems that the Mat object is being created as with 4 color channels. Did a bit more digging and I found a couple references that point to Videowriter expecting 3 channels.

So a simple change was to convert the output of Johns function from 4 channels to 3 channels. This fixed the problem and I can now write the frames to file.

int main (int argc, char **argv)
{
    HWND hwndDesktop = GetDesktopWindow ();
    int key = 0;
    int frame_width = 1920;
    int frame_height = 1080;

    VideoWriter video ("screenCap.avi", CV_FOURCC ('M', 'J', 'P', 'G'), 15, Size 
(frame_width, frame_height));

    while (key != 27)
    {

        Mat src = hwnd2mat (hwndDesktop);
        Mat dst;

        cvtColor (src, dst, COLOR_BGRA2RGB);

        video.write (dst);
        imshow ("Screen Capture", dst);
        key = waitKey (27);
    }
    video.release ();
    destroyAllWindows ();
    return 0;

}