C++ 窗口捕获输出与所述窗口的大小不同

标签 c++ opencv computer-vision bluestacks

我有一个用 C++ 编写的简短程序,用于从这个 BlueStacks 仿真器窗口捕获像素,然后我将使用 OpenCV 操纵像素,然后根据一些决定发送鼠标输入 (win32api)。我在 Visual Studio Enterprise 2017 中工作。

唯一的问题是,与整个窗口相比,该函数似乎捕获了较小的像素区域。

这是一个示例图片。原始窗口在左侧,镜像输出在右侧。

example

我该如何解决这个问题?我已经在我的项目设置中启用了高 DPI 感知,不知道除了将随机魔数(Magic Number)添加到函数中之外还需要采取哪些其他步骤。

我已经用 Python 完成了这个程序,但我想用 C++ 重做这个以提高性能。

Here's a video (warning: noise). And another one.

这是我当前的代码:

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

using namespace std;
using namespace cv;

Mat hwnd2mat(HWND hwnd);

int main(int argc, char **argv)
{
    printf("Hello, world!\n");
    Sleep(1000);

    HWND hwndDesktop;

    hwndDesktop = GetForegroundWindow();
    // namedWindow("output", WINDOW_NORMAL);
    int key = 0;
    Mat src;

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

}

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

最佳答案

拉伸(stretch)是由于 DPI 缩放。您自己的程序不支持 DPI,而其他程序似乎支持 DPI。让您的程序了解 DPI 的最简单方法是在程序开始时调用 SetProcessDPIAware();

另外请注意,调用 GetDIBits 时,不应在设备上下文中选择 HBITMAP 句柄。您可以将代码重写为

SetProcessDPIAware();
...

Mat hwnd2mat(HWND hwnd)
{
    RECT rc;
    GetClientRect(hwnd, &rc);
    int width = rc.right;
    int height = rc.bottom;

    Mat src;
    src.create(height, width, CV_8UC4);

    HDC hdc = GetDC(hwnd);
    HDC memdc = CreateCompatibleDC(hdc);
    HBITMAP hbitmap = CreateCompatibleBitmap(hdc, width, height);
    HBITMAP oldbmp = (HBITMAP)SelectObject(memdc, hbitmap);

    BitBlt(memdc, 0, 0, width, height, hdc, 0, 0, SRCCOPY);
    SelectObject(memdc, oldbmp);

    BITMAPINFOHEADER  bi = { sizeof(BITMAPINFOHEADER), width, -height, 1, 32, BI_RGB };
    GetDIBits(hdc, hbitmap, 0, height, src.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);

    DeleteObject(hbitmap);
    DeleteDC(memdc);
    ReleaseDC(hwnd, hdc);

    return src;
}

此代码仅适用于 native Win32 程序。对于 Chrome、WPF、Qt 应用程序等其他程序,这将显示空白屏幕。您需要截取桌面窗口的屏幕截图。

关于C++ 窗口捕获输出与所述窗口的大小不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50395294/

相关文章:

opencv - 特征检测-大图中的小元素

python-3.x - 即使安装 ffmpeg 也找不到 FFserver 命令

machine-learning - 在没有提供注释的情况下,如何在 PascalVOC 2012 或 COCO 测试集上测试模型?

c++ - 如何在vc++ win32 api中控制滚动条

c++ - 如何创建一个可选的解析器,该解析器能够有条件地删除合成项

c++ - 在图像中的一些点周围绘制边界

c++ - 图像识别特征金字塔有多少步?

c++ - 有没有办法只释放 C\C++ 中动态分配的数组的一部分(缩小现有数组)?

Python 和 opencv : how do I convert the ALL of the background of this image to one colour or transparent

c++ - 为什么 Opencv GPU 代码比 CPU 慢?