How to optimize conversion from PyCBitmap to OpenCV image

别等时光非礼了梦想. 提交于 2019-12-01 13:44:19

问题


I've got this bit of code, and it works... But it runs very slow:

hwin = win32gui.GetDesktopWindow()
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
hwindc = win32gui.GetWindowDC(hwin)
srcdc = win32ui.CreateDCFromHandle(hwindc)
memdc = srcdc.CreateCompatibleDC()
bmp = win32ui.CreateBitmap()
bmp.CreateCompatibleBitmap(srcdc, width, height)
memdc.SelectObject(bmp)
memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)

signedIntsArray = bmp.GetBitmapBits(False)
img = np.array(signedIntsArray).astype(dtype="uint8") # This is REALLY slow!
img.shape = (height,width,4)

srcdc.DeleteDC()
memdc.DeleteDC()
win32gui.ReleaseDC(hwin, hwindc)
win32gui.DeleteObject(bmp.GetHandle())

return cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)

This code grabs the entire windows desktop (both displays) and converts it to an OpenCV image that I use later. I believe this conversion is running slower than it could, if it was reworked a little. Specifically, the np.array(signedIntsArray) call is really slow!

Any thoughts on how to be a bit faster at converting a desktop capture into an OpenCV image?


回答1:


First time working with Python. I think I have a better understanding of now of the data structures. Here's the fix that drastically improves performance:

Change:

signedIntsArray = bmp.GetBitmapBits(False)
img = np.array(signedIntsArray).astype(dtype="uint8")

to:

signedIntsArray = bmp.GetBitmapBits(True)
img = np.fromstring(signedIntsArray, dtype='uint8')

and speed is greatly increased!

This is because it's a lot faster for the np library to create an array from a string than from a tuple.



来源:https://stackoverflow.com/questions/41785831/how-to-optimize-conversion-from-pycbitmap-to-opencv-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!