问题
I'm doing some kind of image recognition / motion detection and need to be able to access the pixel data from the screen with some manner of coordinates so I can see what color the pixel at [350,425] is and so on.
I recently asked a question about getting pixel data from an area of the screen. The default method of viewing pixels in AHK is extremely slow (would take half a day to record the data from a 1080p screen). The answer seems to be using gdip libraries in AHK such as this one:
https://github.com/cswoyer/AutoHotkey/blob/master/ScreenCapture.ahk
However I have no idea how to get the pixel data during the process in a format that I can work with. I need to access the data in some sort of structure is accessible via x,y coordinates, or a format that follows some basic pattern so I can reformat it into a grid data structure and work with it.
I keep trying to check the various variables for any kind of text data that I can try to work with but none of them that I've tried seem to contain text or object data.
回答1:
I'm not sure what you are trying to accomplish, but saving a screenshot in a variable and getting the pixels from there should be faster indeed. But obviously taking that screenshot will take a few milliseconds, so I'm not sure how useful this is gonna be in terms of "motion detection".
#SingleInstance, Force
SetBatchLines, -1
SetWorkingDir %A_ScriptDir%
#Include Gdip.ahk
pToken := Gdip_Startup()
pBitmap := Gdip_BitmapFromScreen() ; Make a screenshot
; Read RGB color from pixel x350 y425
ARGB := Gdip_GetPixel( pBitmap, 350, 425 )
pixelColor := ARGBtoRGB( ARGB )
MsgBox, % pixelColor
; Read RGB color from pixel x345 y567
ARGB := Gdip_GetPixel( pBitmap, 345, 567 )
pixelColor := ARGBtoRGB( ARGB )
MsgBox, % pixelColor
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
ARGBtoRGB( ARGB ) {
VarSetCapacity( RGB,6,0 )
DllCall( "msvcrt.dll\sprintf", Str,RGB, Str,"%06X", UInt,ARGB<<8 )
Return "0x" RGB
}
Credits to these two posts:
https://autohotkey.com/board/topic/47007-get-pixel-color-from-a-bmp/?p=293766
https://autohotkey.com/board/topic/91427-gdi-how-to-get-bitmap-from-certain-area-of-screen/?p=576860
Gdip.ahk can be found here: https://github.com/tariqporter/Gdip/blob/master/Gdip.ahk
来源:https://stackoverflow.com/questions/44625073/how-can-i-get-bitmap-data-from-gdip-libraries-in-ahk-in-a-grid-format