我試圖記錄與蟒蛇覆盆子相機模塊的視頻和 然後每一幀轉換爲OpenCV的框架,但沒有成功:如何picamera視頻幀轉換爲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框架? 我還是新來的圖像處理... 感謝您的任何幫助提前!
當你在'cvtColor'中忽略它們並使用'b'時,從'b'獲取'data'和'image'有什麼意義?實際上,考慮到將數據重塑爲3通道矩陣,「imdecode」似乎有點毫無意義。這整個片段看起來相當奇怪...... –
你是完全正確的......漫長的一天;)我試圖澄清我的剪輯並提供了一個完整且可用的示例 –
當調用write時'b'的長度是多少?我不確定是否保證每幀都有一個「寫入」。庫中提供的便利包裝似乎在'flush'中進行轉換。 |如果您查看['bytes_to_rgb'函數](http://picamera.readthedocs.io/en/release-1.8/_modules/picamera/array.html),'np.fromstring'方法似乎在右邊方向,假設你有整個框架可用。 –