【问题标题】:raspberry pi picamera - image comparison树莓派 picamera - 图像比较
【发布时间】:2018-03-10 17:16:14
【问题描述】:

我试图检测从树莓派 picamera 拍摄的两张照片之间的运动。我通过将原始像素数据相互比较来做到这一点。 下面是我用来测试比较的简单脚本。

import picamera
import datetime
from fractions import Fraction
import time
import numpy as np

oldImage = np.empty((48, 64, 3))
newImage = np.empty((48, 64, 3))
color_offset = 25

with picamera.PiCamera() as camera:
    camera.resolution = (64,48)
    camera.start_preview()
    time.sleep(2)

    camera.capture(oldImage, "rgb")
    time.sleep(5)
    camera.capture(newImage, "rgb")

    x = 0
    y = 0
    diff = 0

    while(x < np.size(newImage, 0)):
        while(y < np.size(newImage, 1)):
            val1 = newImage[x, y, 0] + newImage[x, y, 1] + newImage[x, y, 2]
            val2 = oldImage[x, y, 0] + oldImage[x, y, 1] + oldImage[x, y, 2]
            print(val1)
            print(val2)
            pd = abs(val2 - val1)
            print(pd)

            if(pd > color_offset):
                diff += 1
            y +=1
        x += 1
        y=0

    print(str(diff))

在脚本中我启动相机,拍摄第一张照片,等待几秒钟,拍摄第二张照片并计算差异。但是每次我运行这个脚本时,“val1”变量总是 0.0。 任何帮助将不胜感激。

【问题讨论】:

  • 输出将被打印 3072 次 - 它总是为零?您可以尝试使用您可以控制的更小的图像数据,例如4x4 图像。这甚至可以放在源代码中,它可以是minimal reproducible example...
  • Yhis 输出打印了 3000 多次,我没有看到一个不为零的快速滚动。 picamera 仅允许低至 32x32 像素的分辨率。但是降低分辨率肯定会改变一些东西,val1 不再总是 0.0。很奇怪,但还是谢谢。

标签: python raspberry-pi


【解决方案1】:

因此,使用 2x2 的“图像”,您的颜色值增加

import numpy as np

newImage = np.array([[[0,0,0], [1,0,0]], [[0,1,0], [0,1,1]]])

x = 0
y = 0
while(x < np.size(newImage, 0)):
    while(y < np.size(newImage, 1)):
        val1 = newImage[x, y, 0] + newImage[x, y, 1] + newImage[x, y, 2]
        print(val1)
        y +=1
    x += 1
    y = 0

生产

0
1
1
2

正如我所料。

你期望什么结果?您的数据是否正常(提供样本数据)?

【讨论】:

  • 顺便说一句,我强烈怀疑 NumPy 具有求和和“差异”数组的功能......
  • 更改分辨率解决了这个问题,但数据有其他问题,或者我不明白图像是如何工作的。相加的像素值总是极高或极低。它们的注释如下:x.xe(-)xxx。
  • @emericw en.wikipedia.org/wiki/Scientific_notation#E-notation 也许 NumPy 处理整数图像数据不正确,需要转换成 NumPy 数组?我不知道。
  • 用 uint8 数据类型初始化数组就可以了。还是谢谢。
  • @emericw 你是什么意思:“无论如何”?
猜你喜欢
  • 2019-08-08
  • 1970-01-01
  • 2015-07-08
  • 2020-03-23
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
相关资源
最近更新 更多