树莓派捕获调整大小导致舍入维度 - 如何使用numpy切片结果?

问题描述 投票:-1回答:1

我正在尝试在覆盆子pi中进行视频捕捉,要求特定的宽度和高度为300x300。

这是我的代码:

  # Open image.
  with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.framerate = 30

    camera.start_preview()

    try:
      stream = io.BytesIO()
      for foo in camera.capture_continuous(stream,
              format='rgb',
              use_video_port=True,
              resize=(300, 300)):
        stream.truncate()
        stream.seek(0)
        input = np.frombuffer(stream.getvalue(), dtype=np.uint8)

但是,当运行此代码时,我收到一条警告消息:

/usr/lib/python3/dist-packages/picamera/encoders.py:544:PiCameraResolutionRounded:帧大小从300x300到320x304宽度,高度,宽度,高度))))

得到的张量是1d,我需要它作为代码的其余部分的1d。

如何将这个1d张量调整为3d,将其切成300x300,然后将其压平为1d?

python numpy raspberry-pi
1个回答
2
投票

也许这可以帮到你

import numpy as np
# This is your iamge, for example.
example_image = np.array([1,1,1,2,2,2,3,3,3])  
print(example_image)
# this is gonna be the same image but in other shape
example_image = np.reshape(example_image,(3,3)) 
print(example_image)

这段代码执行此操作:

# first print
[1 1 1 2 2 2 3 3 3]
# second print
[[1 1 1]
 [2 2 2]
 [3 3 3]]

好吧,让我们尝试使用这样的非方形矩阵:

import numpy as np
# This is your iamge, for example.
example_image = np.array([1,1,1,1,2,2,2,2,3,3,3,3])  
print(example_image)
# this is gonna be the same image but in other shape
# 3 Row, 4 Columns
example_image = np.reshape(example_image,(3,4)) 
print(example_image,"\n")
# or maybe this...
example_image = np.array([1,1,1,1,2,2,2,2,3,3,3,3])  
# 3 layers, 2 Row, 2 Columns
example_image = np.reshape(example_image,(3,2,2))   
print(example_image[0], "\n\n" ,example_image[1], "\n\n" ,example_image[2])

结果:

#original
[1 1 1 1 2 2 2 2 3 3 3 3]

#First reshape
[[1 1 1 1]
 [2 2 2 2]
 [3 3 3 3]] 

# Second Reshape
[[1 1]
 [1 1]] 

 [[2 2]
 [2 2]] 

 [[3 3]
 [3 3]]
© www.soinside.com 2019 - 2024. All rights reserved.