首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在屏幕上查找图像

在屏幕上查找图像
EN

Stack Overflow用户
提问于 2019-04-29 01:49:43
回答 1查看 598关注 0票数 0

我想知道如何解决这个问题。我希望每X秒检查一次屏幕是否包含图像(例如,一个红点),如果是,则返回True。我非常熟悉Python,有一些简单的解决方案。但我还没有找到类似的解决方案。

我主要想做的是:

在screenshot

  • Return screenshot

  • Locate

上拍摄screenshot

  • Locate映像X

我研究了OpenCV,可能会以这种方式解决它,但可能有点扩展过度。我在考虑使用getPixel来循环遍历屏幕上的所有像素。但它的速度非常慢。

代码语言:javascript
复制
#include <Windows.h>
#include <iostream>

using namespace std;

int main()
{
    HWND runelite = GetForegroundWindow();
    HMONITOR monitor = MonitorFromWindow(runelite, MONITOR_DEFAULTTONEAREST);
    MONITORINFO info;
    info.cbSize = sizeof(MONITORINFO);
    GetMonitorInfo(monitor, &info);
    int monitor_width = info.rcMonitor.right - info.rcMonitor.left;
    int monitor_height = info.rcMonitor.bottom - info.rcMonitor.top;

    int r, g, b;

    HDC screenshot = GetDC(NULL); 

    for (int i = 0; i < monitor_height; i++) {
        for (int j = 0; j < monitor_width; j++) {
            DWORD color = GetPixel(screenshot, j, i);
            cout << "Scanning -> X: " << j << " Y: " << i << endl;
            r = GetRValue(color);
            g = GetGValue(color);
            b = GetBValue(color);
            if (r == 0 && g == 0 && b == 0) {
                cout << "Button found by color!" << endl;
                goto end;
            }
        }
    }

    end:
    ReleaseDC(NULL, screenshot);
    return 0;
}
EN

回答 1

Stack Overflow用户

发布于 2019-04-29 04:28:32

如果您将HDC的内容复制到另一个位图,并获得指向图像数据的指针并在其上循环,则可以极大地提高速度。

创建内存位图

代码语言:javascript
复制
HDC memDC = CreateCompatibleDC ( hDC );
HBITMAP memBM = CreateCompatibleBitmap ( hDC, nWidth, nHeight );
SelectObject ( memDC, memBM );

然后通过BitBlt将屏幕数据比特转换为该位图,并使用GetDIBits获取位图数据。

还请注意,GetDC(NULL)不会截图,但会让你访问windows live main HDC。直接在桌面上绘制。这就是为什么它上的每一个GetPixel都需要相当长的时间。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55893011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档