【问题标题】:How to capture the desktop in OpenCV (ie. turn a bitmap into a Mat)?如何在 OpenCV 中捕获桌面(即将位图变成 Mat)?
【发布时间】:2012-12-18 09:31:14
【问题描述】:

我想使用 OpenCV 像处理视频流一样处理我的桌面。
我熟悉 OpenCV。
我不熟悉 Windows API。 我意识到还有其他方法可以捕获屏幕,但就我的问题而言,我需要使用 OpenCV 来完成。

这是我的(超级天真)代码:

HWND hDesktopWnd;
HDC hDesktopDC;
hDesktopWnd=GetDesktopWindow();
hDesktopDC=GetDC(hDesktopWnd);

// get the height and width of the screen
int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);

// create a bitmap
HBITMAP hbDesktop = CreateCompatibleBitmap( hDesktopDC, width, height);

Mat src(height,width,CV_8UC4);
src.data = (uchar*)hbDesktop;

imshow("output",src);  //fails :(

StackOverflow 上有类似的问题,但它们不是针对旧式 OpenCV,就是针对 Android 操作系统。
我在 Windows 7 64x 上
Opencv 2.4.3

感谢任何可以回答这个问题的人。

【问题讨论】:

标签: opencv bitmap desktop mat


【解决方案1】:

经过 MUCH 反复试验,我设法编写了一个函数来完成它。这是给其他可能想要的人的:

#include "stdafx.h"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <Windows.h>
#include <iostream>
#include <string>
using namespace std;
using namespace cv;

Mat hwnd2mat(HWND hwnd){

    HDC hwindowDC,hwindowCompatibleDC;

    int height,width,srcheight,srcwidth;
    HBITMAP hbwindow;
    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/2;  //change this to whatever size you want to resize to
    width = windowsize.right/2;

    src.create(height,width,CV_8UC4);

    // create a bitmap
    hbwindow = CreateCompatibleBitmap( hwindowDC, width, height);
    bi.biSize = sizeof(BITMAPINFOHEADER);    //http://msdn.microsoft.com/en-us/library/windows/window/dd183402%28v=vs.85%29.aspx
    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); //change SRCCOPY to NOTSRCCOPY for wacky colors !
    GetDIBits(hwindowCompatibleDC,hbwindow,0,height,src.data,(BITMAPINFO *)&bi,DIB_RGB_COLORS);  //copy from hwindowCompatibleDC to hbwindow

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

    return src;
}

【讨论】:

  • 可以添加以下行以避免内存泄漏: DeleteObject (hbwindow); DeleteDC(hwindowCompatibleDC); ReleaseDC(hwnd, hwindowDC);
  • 太棒了。感谢您的代码。我已将@Steck 的代码添加到您的帖子中以使其完整。
  • 感谢您的代码,它运行良好。添加了您最初问题的信息以处理屏幕。在我的代码中添加了@Steck 的注释,并使用这些内存释放编辑了您的答案。希望没问题。
  • 我正在尝试使用它来捕获 MEmu 窗口。它只捕获窗口的“框架”(窗口标题、菜单项、关闭按钮等)。知道为什么会这样吗?
  • 这在 Win10 中有效吗?通过对高度和宽度的代码进行一些调整,对我来说,当 hwnd 是窗口句柄时它会返回黑框,但当它为 NULL 时,它会执行应有的操作,返回整个屏幕。
【解决方案2】:

更好的方法是在为像素分配内存的同时只分配一次。所以这里做的唯一副本是BitBlt制作的那个

int main()

{

    int x_size = 800, y_size = 600; // <-- Your res for the image





    HBITMAP hBitmap; // <-- The image represented by hBitmap

    Mat matBitmap; // <-- The image represented by mat





    // Initialize DCs

    HDC hdcSys = GetDC(NULL); // Get DC of the target capture..
    HDC hdcMem = CreateCompatibleDC(hdcSys); // Create compatible DC 






    void *ptrBitmapPixels; // <-- Pointer variable that will contain the potinter for the pixels









    // Create hBitmap with Pointer to the pixels of the Bitmap
    BITMAPINFO bi; HDC hdc;
    ZeroMemory(&bi, sizeof(BITMAPINFO));
    bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bi.bmiHeader.biWidth = x_size;
    bi.bmiHeader.biHeight = -y_size;  //negative so (0,0) is at top left

    bi.bmiHeader.biPlanes = 1;

    bi.bmiHeader.biBitCount = 32;
    hdc = GetDC(NULL);
    hBitmap = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, &ptrBitmapPixels, NULL, 0);
    // ^^ The output: hBitmap & ptrBitmapPixels


    // Set hBitmap in the hdcMem 
    SelectObject(hdcMem, hBitmap);



    // Set matBitmap to point to the pixels of the hBitmap
    matBitmap = Mat(y_size, x_size, CV_8UC4, ptrBitmapPixels, 0);
    //                ^^ note: first it is y, then it is x. very confusing

    // * SETUP DONE *




    // Now update the pixels using BitBlt
    BitBlt(hdcMem, 0, 0, x_size, y_size, hdcSys, 0, 0, SRCCOPY);


    // Just to do some image processing on the pixels.. (Dont have to to this)
    Mat matRef = matBitmap(Range(100, 200), Range(100, 200));
    //                              y1    y2            x1     x2
    bitwise_not(matRef, matRef); // Invert the colors in this x1,x2,y1,y2




    // Display the results through Mat
    imshow("Title", matBitmap);

    // Wait until some key is pressed

    waitKey(0);


    return 0;


}

请注意,此处未进行错误处理以使其易于理解,但您必须在代码中进行错误处理!

希望对您有所帮助

【讨论】:

  • 为什么GetDC(NULL) 两次?
  • 酷。值得注意的是,如果在可重复的场景(例如循环)中使用上面的代码,则会留下内存泄漏,并会在长时间内淹没您的堆内存/崩溃应用程序。你必须调用 DeleteObject(hBitmap);和 matBitmap.release() 完成图像后。
猜你喜欢
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 2018-10-09
  • 2018-03-16
  • 1970-01-01
相关资源
最近更新 更多