我正在尝试编写一个程序来玩有趣的全屏PC游戏(作为计算机视觉和人工智能的实验)。
对于这个实验,我假设游戏没有AI玩家的底层API(源也不可用)所以我打算处理游戏在屏幕上呈现的视觉信息。
游戏在win32系统上以全屏模式运行(我假设是直接X)。
目前我正在使用win32函数
#include <windows.h>
#include <cvaux.h>
class Screen {
public:
HWND windowHandle;
HDC windowContext;
HBITMAP buffer;
HDC bufferContext;
CvSize size;
uchar* bytes;
int channels;
Screen () {
windowHandle = GetDesktopWindow();
windowContext = GetWindowDC (windowHandle);
size = cvSize (GetDeviceCaps (windowContext, HORZRES), GetDeviceCaps (windowContext, VERTRES));
buffer = CreateCompatibleBitmap (windowContext, size.width, size.height);
bufferContext = CreateCompatibleDC (windowContext);
SelectObject (bufferContext, buffer);
channels = 4;
bytes = new uchar[size.width * size.height * channels];
}
~Screen () {
ReleaseDC(windowHandle, windowContext);
DeleteDC(bufferContext);
DeleteObject(buffer);
delete[] bytes;
}
void CaptureScreen (IplImage* img) {
BitBlt(bufferContext, 0, 0, size.width, size.height, windowContext, 0, 0, SRCCOPY);
int n = size.width * size.height;
int imgChannels = img->nChannels;
GetBitmapBits (buffer, n * channels, bytes);
uchar* src = bytes;
uchar* dest = (uchar*) img->imageData;
uchar* end = dest + n * imgChannels;
while (dest < end) {
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest += imgChannels;
src += channels;
}
}
使用这种方法处理帧的速度太慢了。有没有更好的方法来获取屏幕框架?