【问题标题】:How to convert picamera video frame to openCV object如何将 picamera 视频帧转换为 openCV 对象
【发布时间】:2017-06-18 12:01:00
【问题描述】:

我正在尝试使用 python raspberry 相机模块录制视频,并且 然后将每一帧转换为openCV帧,但没有成功:

import time
import picamera
import cv2
import numpy as np

class BroadcastOutput(object):
    def __init__(self, camera):
        return


    def write(self, b):

        #create numpy array from b
        data = np.fromstring(b, dtype=np.uint8)

        #doesn't work with reshape either
        #data = np.fromstring(b, dtype=np.uint8).reshape(320, 280, 3)

        #enconde as image
        image = cv2.imdecode(data, 1)

        #test if is valid cv2 object -> fails
        cv2.cvtColor(image, cv2.COLOR_BGR2HSV)


    def flush(self):
        print('Waiting for background conversion process to exit')

    #camera setup and start
    with picamera.PiCamera() as camera:
        camera.resolution = (320, 280)
        camera.framerate = 24
        time.sleep(2) # camera warm-up time

        print('Initializing broadcast thread')
        output = BroadcastOutput(camera)
        print('Starting recording')
        camera.start_recording(output, 'bgr')

        try:
            while True:
                camera.wait_recording(1)

        except KeyboardInterrupt:
            pass
        finally:
            print('Stopping recording')
            camera.stop_recording()

当我打印我的 numpy 数组时,它有内容,但解码后的图像对象总是没有。

所以我的问题是:如何正确使用 b 中提供的数据作为 cv2 框架? 我还是图像处理的新手... 提前感谢您的帮助!

【问题讨论】:

  • b 获取dataimage,然后忽略它们并在cvtColor 中使用b 有什么意义?实际上,考虑到您将数据重塑为 3 通道矩阵,imdecode 似乎有点毫无意义。这整个 sn-p 看起来很奇怪....
  • 你是完全正确的......漫长的一天;)我试图澄清我的片段并提供了一个完整且可用的示例
  • write 被调用时,b 的长度是多少?我不确定是否可以保证每帧获得一个write。库中提供的便利包装器似乎在flush 中进行了转换。 |如果您查看bytes_to_rgb functionnp.fromstring 方法似乎是正确的方向,假设您已经获得了整个框架。
  • 你是对的!写回调没有收到完整的帧! @fireant 为 PiRGBAnalysis 类提供了正确的提示

标签: python opencv numpy camera raspberry-pi


【解决方案1】:

请参阅PiRGBAnalysis 类的文档。

【讨论】:

  • 如前所述,写回调没有收到完整的帧,但 PiRGBAnalysis 是!所以感谢您的提示,现在一切正常。有关该类的工作示例,请参阅raspberrypi.stackexchange.com/questions/32926/…
  • 很高兴您找到了有效的示例代码并在此处分享了链接。
猜你喜欢
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
相关资源
最近更新 更多