【问题标题】:How to save RGB data from RPi Camera then save file to matlab/Python readable format?如何从 RPi Camera 保存 RGB 数据,然后将文件保存为 matlab/Python 可读格式?
【发布时间】:2021-10-09 00:11:42
【问题描述】:

由于我的应用程序的时间限制,我正在连续拍摄多个白光图像,稍后我想将其拆分为 RGB 图像。我目前将原始 RGB 图像保存为.data,但无法弄清楚如何将文件读回数组以允许我在单独的脚本中对其进行操作。有没有更好的方法可以非常快速地保存这些 RGB 数据,以便我以后可以访问它?或者甚至更好地将其拆分为 R G 和 B 然后单独保存这些图像?

相机捕捉:

self.camera.capture('file_location.data', 'rgb')

读回 Python(单独的脚本):

path = 'file_location.data'

with open(path, 'rb') as f:
  contents = f.read()

我能够读取二进制文件,但还没有找到如何将contents 转换为我可以操作的数组。

【问题讨论】:

  • 你能提供一个你的数据文件的例子吗?
  • 您需要连续保存多少张图片?请问图片的高度和宽度是多少,以像素为单位?保存图像的常规方法是使用 PNG 或 JPEG 文件。请问具体时间限制是多少?
  • @MarkSetchell 我必须连续拍摄 7 张图像。我拍摄图像然后移动电机,拍摄图像移动电机等。我需要这个过程尽可能快。我相信到目前为止,所有 7 张图像的时间为 20 秒(输出 .data 文件)。在每个电机位置,我拍摄一张白光图像和另一张背景图像(无光),并希望将这些图像保存为 RGB 输出,以便稍后将 R G 和 B 分量提取为 3 个单独的图像。在这种情况下,我不能使用 PNG 或 JPEG。我需要自己保存 RGB 数据,或者在成像期间非常快速地分离图像。
  • @MarkSetchell 高度和宽度是相机的最大分辨率(2592x1944)。我曾想过将这些图像保存到一个数组中,然后在该过程完成后保存所有 7 个图像,但尚未尝试。不确定树莓派 3B+ 是否有足够的 RAM 来处理这么大的阵列。 7*(2592x1944x3)
  • 这些图像每个只有 15MB,所以 7 大约需要 105MB,这对于 RasPi 来说应该很容易。您不需要将其作为单个连续数组,您可以拥有 7 个图像的列表。

标签: python raspberry-pi rgb binaryfiles picamera


【解决方案1】:

这是按照您的要求做的一种方法 - 它将 7 张图像保存在内存中的列表中,以便您可以快速连续拍摄照片,然后在实验完成后以更悠闲的速度将它们刷新到磁盘。

您似乎误解了压缩意味着数据丢失。在PNG 的情况下,压缩是无损的,不像JPEG 是有损的。

#!/usr/bin/env python3

import numpy as np
from PIL import Image

# Make 7 random RGB images 2592x1944 and append to list in memory
imageList = []
for i in range(7):
   print(f'Creating image {i}')
   im = np.random.randint(0,256,(1944,2592,3), dtype=np.uint8)
   imageList.append(im)

# Show user length of list
print(f'Number of images in list: {len(imageList)}')

# Save images after experiment is complete
for i,image in enumerate(imageList):
    filename = f'image-{i}.png'
    print(f'Saving image: {filename}')
    Image.fromarray(image).save(filename)

样本输出

Creating image 0
Creating image 1
Creating image 2
Creating image 3
Creating image 4
Creating image 5
Creating image 6
Number of images in list: 7
Saving image: image-0.png
Saving image: image-1.png
Saving image: image-2.png
Saving image: image-3.png
Saving image: image-4.png
Saving image: image-5.png
Saving image: image-6.png

如果你运行程序,你会看到这 7 个图像几乎是瞬间创建的,只有最后保存到磁盘需要一段时间。

这里是创建的文件:

-rw-r--r--@ 1 mark  staff  15132795  6 Aug 11:01 image-0.png
-rw-r--r--  1 mark  staff  15132768  6 Aug 11:01 image-1.png
-rw-r--r--  1 mark  staff  15132789  6 Aug 11:01 image-2.png
-rw-r--r--  1 mark  staff  15132792  6 Aug 11:01 image-3.png
-rw-r--r--  1 mark  staff  15132790  6 Aug 11:01 image-4.png
-rw-r--r--  1 mark  staff  15132791  6 Aug 11:01 image-5.png
-rw-r--r--  1 mark  staff  15132784  6 Aug 11:01 image-6.png

如果你想分析内存使用情况,你可以在程序运行的时候运行htop观察内存使用情况,也可以这样运行:

/usr/bin/time -l ./script.py 

        6.79 real         6.98 user         0.21 sys
       162914304  maximum resident set size
               0  average shared memory size
               0  average unshared data size
               0  average unshared stack size
           40470  page reclaims
               0  page faults
               0  swaps
               0  block input operations
               0  block output operations
               0  messages sent
               0  messages received
               0  signals received
               9  voluntary context switches
            2620  involuntary context switches
     48621173737  instructions retired
     30872454100  cycles elapsed
       145956864  peak memory footprint

请注意,您同样可以使用 OpenCV 而不是 PIL,只需使用:

import cv2

最后

cv2.imwrite(filename, image)

请注意,Raspberry Pi 有 4 个 CPU 内核,而 Python 往往只使用一个,因此如果您希望能够存储更多图像,您可以启动 3 个额外的进程,等待来自采集过程的图像并写入它们到磁盘。然后,您将以 3 倍的速率从未保存的图像中清除 RAM。这对于 Redis 或 Python 3.8 多处理共享内存非常简单。如果您在 Raspberry Pi 上运行 Redis 实例,它可以在没有 WiFi 的情况下运行(因为它是本地的),但您可以在之后(或实时)从您的 PC 拉取图像以供 Matlab 使用过程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多