[英]Stacking frames to a 3D array
我正在一個Raspberry Pi項目中,在該項目中,我需要每秒拍攝約30張圖像(無電影),並使用numpy數組將每個2D圖像堆疊到3D陣列中,而無需將每個2D捕獲保存為文件(因為速度很慢) 。
我發現此Python代碼可以盡可能快地拍攝圖像,但是我不知道如何將所有圖像快速堆疊到3D圖像堆棧中。
import io
import time
import picamera
#from PIL import Image
def outputs():
stream = io.BytesIO()
for i in range(40):
# This returns the stream for the camera to capture to
yield stream
# Once the capture is complete, the loop continues here
# (read up on generator functions in Python to understand
# the yield statement). Here you could do some processing
# on the image...
#stream.seek(0)
#img = Image.open(stream)
# Finally, reset the stream for the next capture
stream.seek(0)
stream.truncate()
with picamera.PiCamera() as camera:
camera.resolution = (640, 480)
camera.framerate = 80
time.sleep(2)
start = time.time()
camera.capture_sequence(outputs(), 'jpeg', use_video_port=True)
finish = time.time()
print('Captured 40 images at %.2ffps' % (40 / (finish - start)))
你們中誰知道如何使用Python和Raspberry Pi相機模塊將這段代碼中拍攝的2D圖像堆疊到3D numpy數組中? 無需將每個2D捕捉保存為文件
最好的問候,阿古斯丁
numpy.dstack()
/ numpy.hstack()
/ numpy.vstack()
和numpy.reshape()
任意組合都可以很好地工作,具體取決於您要實現的目標,例如:
>>> A,B,C = (np.array([[1,2],[3,4]])+i*10 for i in range(3))
>>> A
array([[1, 2],
[3, 4]])
>>> B
array([[11, 12],
[13, 14]])
>>> C
array([[21, 22],
[23, 24]])
>>> D=np.reshape(np.vstack((A,B,C)), (3,2,2))
>>> D
array([[[ 1, 2],
[ 3, 4]],
[[11, 12],
[13, 14]],
[[21, 22],
[23, 24]]])
>>> D[0]
array([[1, 2],
[3, 4]])
>>> D[1]
array([[11, 12],
[13, 14]])
>>> D[2]
array([[21, 22],
[23, 24]])
>>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.