简体   繁体   中英

Create a divx-encoded avi from frames using opencv

This question is similar to this one and particularly this one but my desired output is different. I'm trying to capture the desktop to video using opencv. The preferred output is an avi file with divx encoding. I'm new to opencv and bitmap programming in general.

As a first step, to make sure the divx codec is present, I create a single frame (cv::Mat) of a solid color (yellow) and write that 100 times to the video file, as shown here:

int main(int argc, char* argv[])
{
   cv::Mat frame(1200, 1920, CV_8UC3, cv::Scalar(0, 50000, 50000));

   cv::VideoWriter* videoWriter = new cv::VideoWriter(
                "C:/videos/desktop.avi", 
                 CV_FOURCC('D','I','V','3'), 
                 5, cv::Size(1920, 1200), true);

   int frameCount = 0;

   while (frameCount < 100)
   {
      videoWriter->write(frame);
      ::Sleep(100);
      frameCount++;
    }

    delete videoWriter;
    return 0;
}

This works perfectly - the video file is created and can be played on my Win 10 machine with VLC, Windows Media Player or the Films&TV app. It's 100 frames of solid yellow, but it shows the video is being created properly.

Next step: replace the dummy cv::Mat frame in the code above with a series of screenshots of the desktop. I get a handle to the desktop window using GetDesktopWindow() , and then use the function hwnd2mat (taken from this SO question - thanks!) to convert the bitmap obtained from the desktop handle to a cv::Mat that I can write to my video.

I copied the hwnd2mat function verbatim except I don't scale the image - the desktop bitmap is already 1920x1200, and also the cv::Mat I create is CV_8UC3 instead of CV_8UC4 (CV_8UC4 causes my app to crash).

Here's the code, including a reprint of hwnd2mat:

int main(int argc, char* argv[])
{
   cv::VideoWriter* videoWriter = new cv::VideoWriter(
                "C:/videos/desktop.avi", 
                 CV_FOURCC('D','I','V','3'), 
                 5, Size(1920, 1200), true);

   int frameCount = 0;

   while (frameCount < 100)
   {
      HWND hDsktopWindow = ::GetDesktopWindow();
      cv::Mat frame = hwnd2mat(hDsktopWindow);
      videoWriter->write(frame);
      ::Sleep(100);
      frameCount++;
   }

   delete videoWriter;
   return 0;
}

cv::Mat hwnd2mat(HWND hwnd)
{
   HDC hwindowDC, hwindowCompatibleDC;
   int height, width, srcheight, srcwidth;
   HBITMAP hbwindow;

   cv::Mat src;
   BITMAPINFOHEADER  bi;

   hwindowDC = GetDC(hwnd);
   hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);

   SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);

   RECT windowsize;    // get the height and width of the screen
   GetClientRect(hwnd, &windowsize);
   srcheight = windowsize.bottom;
   srcwidth = windowsize.right;
   height = windowsize.bottom / 1;  //change this to whatever size you want to resize to
   width = windowsize.right / 1;

   src.create(height, width, CV_8UC3);

   // create a bitmap
   hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
   bi.biSize = sizeof(BITMAPINFOHEADER);   
   bi.biWidth = width;
   bi.biHeight = -height;  //this is the line that makes it draw upside down or not
   bi.biPlanes = 1;
   bi.biBitCount = 32; 
   bi.biCompression = BI_RGB;
   bi.biSizeImage = 0;
   bi.biXPelsPerMeter = 0;
   bi.biYPelsPerMeter = 0;
   bi.biClrUsed = 0;
   bi.biClrImportant = 0;

   // use the previously created device context with the bitmap
   SelectObject(hwindowCompatibleDC, hbwindow);

   // copy from the window device context to the bitmap device context
   StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0,srcwidth, srcheight, SRCCOPY); 

   GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);

   // avoid memory leak
   DeleteObject(hbwindow); DeleteDC(hwindowCompatibleDC); ReleaseDC(hwnd,hwindowDC);

   return src;
}

The result of this is that the video file is created and can be played without errors, but it's just solid grey. It seems like the bitmap of the desktop is not getting copied correctly into the cv::Mat frame. I've tried a zillion combinations of the values in the BITMAPINFOHEADER, but nothing works and I don't know what I'm doing to be honest. I know opencv has conversion functions but again, I don't even really know what I'm trying to convert to/from.

Any help appreciated!

Figured out a way to make it work - I have no idea if this is the best way, so comments or alternative solutions are still welcome.

It seems like for GetDIBits to work, the cv::Mat has to be 4-channel, ie CV_8UC4, like the original code of hwnd2mat before I changed it. If it is not CV_8UC4, no data is copied (GetDIBits returns 0 scan lines copied) and that's why my avi was just gray. So the first change was to create the src cv::Mat as 4-channel:

src.create(height, width, CV_8UC4);

But for the divx-encoded avi file that I'm trying to create, the frames should be 3-channel (don't ask me why). I added a call to an opencv conversion function after calling GetDIBits(), as follows:

cv::Mat dst;
dst.create(height, width, CV_8UC3); 
cv::cvtColor(src, dst, CV_RGBA2RGB);

And then I return dst from hwnd2mat instead of src. The call to cvtColor removes the alpha channel (the A in RGBA) and dst ends up with just the R,G,B channels.

You can get bitmap with no alpha channel from GetDIBits and write it straight to cv::VideoWriter. Just change biBitCount to 24. Leave Mat format to CV_8IC3. This worked for me. src.create(height, width, CV_8UC3);

bi.biBitCount = 24; // this is where to change

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM