使用 BitmapBufferFormat_RGBA 将 wx 位图转换为 numpy(python)

问题描述 投票:0回答:3

我正在尝试使用 wxPython 捕获窗口并使用 cv2 处理结果。这看起来相当简单,因为 wx 有一个内置函数可以将位图对象转换为简单的 RGB 数组。

问题是我无法弄清楚语法。文档很少,我能找到的几个示例要么已弃用,要么不完整。

这基本上就是我想要的

app = wx.App(False)
img = some_RGBA_array #e.g. cv2.imread('some.jpg')
s = wx.ScreenDC()
w, h = s.Size.Get()
b = wx.EmptyBitmap(w, h)
m = wx.MemoryDCFromDC(s)
m.SelectObject(b)
m.Blit(0, 0, w, h, s, 0, 0)
m.SelectObject(wx.NullBitmap)
b.CopyFromBuffer(m, format=wx.BitmapBufferFormat_RGBA, stride=-1)#<----problem is here
img.matchSizeAndChannels(b)#<----placeholder psuedo
img[:,:,0] = np.where(img[:,:,0] >= 0, b[:,:,0], img[:,:,0])#<---copy a channel

为了简单起见,这没有指定窗口并且只处理一个通道,但它应该让我了解我正在尝试做什么。

每当我尝试使用 CopyFromBuffer 那样运行它时,它都会告诉我存储在“b”中的位图不是可读的缓冲区对象,但如果我将其传递给 SaveFile,它会按预期写出图像。

不确定我在这里做错了什么。

编辑:事实证明我做错的是尝试使用 BitmapBufferFormat_RGBA 将 wxBitmaps 转换为 cv2 rgb。根据下面的答案,我应该使用以下内容(其中“b”是位图):

wxB = wx.ImageFromBitmap(b)#input bitmap 
buf = wxB.GetDataBuffer() 
arr = np.frombuffer(buf, dtype='uint8',count=-1, offset=0)
img2 = np.reshape(arr, (h,w,3))#convert to classic rgb
img2 = cv2.cvtColor(img2, cv2.COLOR_RGB2BGR)#match colors to original image
python arrays numpy bitmap wxpython
3个回答
3
投票

有一段时间没有这样做了:但是 OpenCV 位图 本质上是一个 numpy 数组。要从通用数组创建

wx.Bitmap
,您必须采用
wx.Image
路线。请参阅 wxPython wiki 中的条目(中间的某个位置)有关转换 numpy 数组的信息:

array = ... # the OpenCV image
image = ... # wx.Image
image.SetData(array.tostring())
wxBitmap = image.ConvertToBitmap()       # OR:  wx.BitmapFromImage(image)

编辑:反之亦然:

import numpy
img = wx.ImageFromBitmap(wxBitmap)
buf = img.GetDataBuffer() # use img.GetAlphaBuffer() for alpha data
arr = numpy.frombuffer(buf, dtype='uint8')

# example numpy transformation
arr[0::3] = 0 # turn off red
arr[1::3] = 255 # turn on green

0
投票

对于那些获得:

wxpython AttributeError: 'memoryview' object has no attribute '__buffer__'

解决方案是使用:

arr = np.asarray(img.GetDataBuffer())
img_data = np.copy(np.reshape(arr, (img.GetHeight(),img.GetWidth(),3)))

0
投票

如果你想将wx.Bitmap图像转换为数组以供使用open cv,你可以这样做:

img = bmp.ConvertToImage()
img_arr= np.reshape(np.frombuffer(img.GetDataBuffer(), dtype='uint8'), 
                   (bmp.GetHeight(), bmp.GetWidth(), 3))
© www.soinside.com 2019 - 2024. All rights reserved.