GetPixel太慢

时间:2015-10-10 03:02:34

标签: c++ winapi pixels

我有一堆正方形,每个正方形都有一个我需要识别的特定身份/符号。到目前为止,我有类似的东西:

#include <iostream>
#include <windows.h>

using namespace std;

int main() {
    HDC dc = GetDC(0);
    COLORREF color;
    int sum, x, y;

    while (true) {
        sum = 0;
        Sleep(100);
        for (x = 512; x < 521; x++) {
            for (y = 550; y < 565; y++) {
                color = GetPixel(dc, x, y);
                sum = GetRValue(color) + GetBValue(color) + GetGValue(color);
            }
        }
        cout << "SUM: " << sum << endl;
    }

    return 0;
}

显然它到目前为止只扫描一个块。问题是某种程度上,即使它只有100多个像素,它需要很长一段时间。我甚至无法想象会发生什么。每次重复都需要超过一秒钟,也许两秒钟。我能做什么?必须有一种更快的方法来做到这一点。如果我无法查询单个像素,是否有办法获得屏幕区域?该区域不在我的程序窗口内。

1 个答案:

答案 0 :(得分:1)

Jonathan在使用DIB的问题中发表评论,但没有答案说明如何使用DIB。为了完整起见,这里是Mister B's code

Traceback (most recent call last):
  File "c:/Users/micha/OneDrive/Desktop/TugasComvis.py", line 1, in <module>
    from skimage import io, img_as_float
  File "C:\Users\micha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\skimage\io\__init__.py", line 11, in <module>    
    from ._io import *
  File "C:\Users\micha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\skimage\io\_io.py", line 4, in <module>
    from ..color.colorconv import rgb2gray, rgba2rgb
  File "C:\Users\micha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\skimage\color\__init__.py", line 1, in <module>  
    from .colorconv import (convert_colorspace,
  File "C:\Users\micha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\skimage\color\colorconv.py", line 56, in <module>
    from scipy import linalg
  File "C:\Users\micha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\scipy\__init__.py", line 136, in <module>        
    from . import _distributor_init
  File "C:\Users\micha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\scipy\_distributor_init.py", line 61, in <module>
    WinDLL(os.path.abspath(filename))
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\ctypes\__init__.py", line 364, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

正如Ben在评论中指出的那样,这仍然很慢,解决方案是使用这些构造块立即复制整个区域。您的代码将变成这样:

COLORREF getcolor(POINT pt) {
    HDC hDc = GetDC(0);
    HDC hDcmem = CreateCompatibleDC(0);
    HBITMAP hBmp = CreateCompatibleBitmap(hDc, 1, 1);
    SelectObject(hDcmem, hBmp);
    BitBlt(hDcmem, 0, 0, 1, 1, hDc, pt.x, pt.y, SRCCOPY);
    LPBITMAPINFO lpbmi = new BITMAPINFO;
    lpbmi->bmiHeader.biBitCount = 24;
    lpbmi->bmiHeader.biCompression = BI_RGB;
    lpbmi->bmiHeader.biPlanes = 1;
    lpbmi->bmiHeader.biHeight = 1;
    lpbmi->bmiHeader.biWidth = 1;
    lpbmi->bmiHeader.biSize = sizeof(BITMAPINFO);
    BYTE lpvBits[4];
    GetDIBits(hDcmem, hBmp, 0, 1, lpvBits, lpbmi, DIB_RGB_COLORS);
    COLORREF currColor = RGB(lpvBits[2], lpvBits[1], lpvBits[0]);
    delete lpbmi;
    DeleteObject(hBmp);
    DeleteDC(hDcmem);
    ReleaseDC(0, hDc);
    return currColor;
}