ValueError:无效的数据缓冲区大小wx.Bitmap.FromBuffer格式将Nmpy图像从BGR转换为RBG后

2024-10-01 13:39:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在将一个numpy图像从BGR转换为RBG格式,这个图像正在转换为wx.位图图像,但这样做时,我得到“ValueError:Invalid data buffer size.”的位图图像。 在没有BGR到RBG转换的情况下,位图图像可以正常生成。你知道吗

错误代码:

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.

但是当我使用numpy数组(self.\u 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])

Tags: selfnumberimggetvideocountcv2frame
2条回答
self.img = np.array((self._frameHeight, self._frameWidth, 3), np.dtype('uint8'))

这将创建一个由3项组成的数组。第一个参数是用于填充数组的iterable,而不是数组的形状。也许您想创建带有np.zeros或类似内容的数组。在这种情况下,第一个参数是形状。你知道吗

解决了位图转换问题:

 def iter_frames(self):   # Frame Generator to yield next frame
    while self._frame_count < self._frameCount and self._frame_grabbed:
        self._frame_count += 1
        self._frame_grabbed, self.img = self._cap.read()
        self.img = cv2.cvtColor(self.img,cv2.COLOR_BGR2RGB)
        yield self.img, wx.Bitmap.FromBuffer(640, 360, cv2.resize(self.img, (640, 360)))

`

相关问题 更多 >