2010-06-07 19 views
2

我想編寫一個程序來播放全屏幕PC遊戲的樂趣(如計算機視覺和人工智能的實驗)。如何快速採集和處理實時畫面輸出

在這個實驗中,我假定遊戲對電腦玩家沒有底層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; 
    } 
} 

在這我可以處理使用這種方法實在太慢幀的速率。有沒有更好的方法來獲取屏幕框架?

+0

可能要到3D或遊戲添加到您的meta標籤 – 2010-06-07 00:41:26

+0

我認爲根本問題比我的具體應用更普遍的(這恰好是一個等距RTS遊戲) – Akusete 2010-06-07 00:45:32

回答

0

作爲一般的方法,我會鉤的緩衝器翻轉的函數調用,所以我可以捕獲每個幀上的幀緩衝區。

http://easyhook.codeplex.com/