gpt4 book ai didi

python - 将 Nmpy 图像从 BGR 转换为 RBG 后 wx.Bitmap.FromBuffer 中的 "ValueError: Invalid data buffer size."

转载 作者:太空宇宙 更新时间:2023-11-03 23:05:27 26 4
gpt4 key购买 nike

我正在将一个 numpy 图像从 BGR 格式转换为 RBG 格式,并且该图像正在被转换为 wx.Bitmap 图像,但在这样做时我收到“ValueError:无效数据缓冲区大小”。对于位图图像。如果没有 BGR 到 RBG 的转换,BITMAP 图像将被正确生成。

错误代码:

class Video:
def __init__(self):
# self._filename = file_name
self._frame_number = -1
self._cap = cv2.VideoCapture('Path/t/o/Video') ###self._filename
self._frameCount = int(self._cap.get(cv2.CAP_PROP_FRAME_COUNT))
self._frameWidth = int(self._cap.get(cv2.CAP_PROP_FRAME_WIDTH))
self._frameHeight = int(self._cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self._frame_count = 0
self._frame_grabbed = True
self.img = np.array((self._frameHeight, self._frameWidth, 3), np.dtype('uint8'))

def iter_frames(self): # Generator to return Numpy and BitMap Image
while self._frame_count < self._frameCount and self._frame_grabbed:
self._frame_count += 1
self._frame_grabbed, self.img = self._cap.read()
print(type(self.img))
#self.img.reshape(self._frameHeight,self._frameWidth, 3)
self.img = self.img[..., ::-1] # To convert BGR to RGB
yield self.img, wx.Bitmap.FromBuffer(self._frameWidth, self._frameHeight, self.img)

v=Video()
print(next(iter_frames()))

遇到错误:

  File "C:/Users/qpjg1605/Documents/Python_proj/Auptimo/Video_file.py", line 46, in <module>
print(next(v.iter_frames()))
File "C:/Users/qpjg1605/Documents/Python_proj/Auptimo/Video_file.py", line 30, in iter_frames
yield self.img, wx.Bitmap.FromBuffer(self._frameWidth, self._frameHeight, self.img)
ValueError: Invalid data buffer size.

但是当我使用 array of numpy arrays(self._buf[]) 的代码时,它工作正常"

相同的代码:

class Video:
def __init__(self):
# self._filename = file_name
self._frame_number = -1
self._cap = cv2.VideoCapture('Path\to\Video') ###self._filename
self._frameCount = int(self._cap.get(cv2.CAP_PROP_FRAME_COUNT))
self._frameWidth = int(self._cap.get(cv2.CAP_PROP_FRAME_WIDTH))
self._frameHeight = int(self._cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self._buf = np.empty((self._frameCount, self._frameHeight, self._frameWidth, 3),np.dtype('uint8')) # Empy numpy array of the shape and dtype(pixel from 0 to 255)

self._frame_count = 0
self._frame_grabbed = True
print("here")
while self._frame_count < self._frameCount and self._frame_grabbed:
self._frame_grabbed, self._buf[self._frame_count] = self._cap.read()
self._buf[self._frame_count] = self._buf[self._frame_count][..., ::-1]## Converting BGR to RBG(last dimension)
#self._buf[self._frame_count] = imutils.resize(self._buf[self._frame_count], 640, 360)
self._frame_count += 1


def iter_frames(self):
while self._frame_number < self._frameCount:
self._frame_number = self._frame_number + 1
# yield int(self._frame_number)
# cur_bmp = wx.Bitmap.FromBuffer(self.get_org_frame_width(), self.get_org_frame_height(), )
print(self._frame_number)
yield self._buf[self._frame_number], wx.Bitmap.FromBuffer(self.get_org_frame_width(),
self.get_org_frame_height(),
self._buf[self._frame_number])

最佳答案

self.img = np.array((self._frameHeight, self._frameWidth, 3), np.dtype('uint8'))

这将创建一个包含 3 个项目的数组。第一个参数是用于填充数组的可迭代对象,而不是数组的形状。也许您想使用 np.zeros 或类似的方式创建数组。在这种情况下,第一个参数是形状。

关于python - 将 Nmpy 图像从 BGR 转换为 RBG 后 wx.Bitmap.FromBuffer 中的 "ValueError: Invalid data buffer size.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57637428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com