gpt4 book ai didi

c++ - OPENCV 桌面捕获

转载 作者:搜寻专家 更新时间:2023-10-31 00:11:18 25 4
gpt4 key购买 nike

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

你好,

任何人都可以向我解释如何在 OpenCV 中使用这段代码来捕获桌面屏幕吗?我一直在尝试让它运行大约 30-45 分钟,但是当我运行它时我的屏幕没有捕捉到任何东西。

在我的 main 应用程序中,我有以下三个语句

HWND hwndDesktop = GetDesktopWindow();
hwnd2mat(hwndDesktop);
imshow("output", src);

我正在调用上面链接中的函数 hwnd2mat。我是菜鸟。

感谢任何回答的人。

最佳答案

根据 OP 的评论,我认为仍然需要解释如何像视频流一样捕获桌面

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <Windows.h>
#include <iostream>

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/1; //change this to whatever size you want to resize to
width = windowsize.right/1;

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;
}

int main(int argc, char **argv)
{
HWND hwndDesktop = GetDesktopWindow();
namedWindow("output",WINDOW_NORMAL);
int key = 0;

while( key != 27 )
{
Mat src = hwnd2mat(hwndDesktop);
// you can do some image processing here
imshow("output", src);
key = waitKey(60); // you can change wait time
}

}

编辑:您可以找到用于屏幕捕获和另存为视频的示例代码 here

关于c++ - OPENCV 桌面捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34466993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com