raspberry pi picamera - 图像比较

时间:2018-03-10 17:16:14

标签: python raspberry-pi

我试图检测从覆盆子pi 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))

在脚本中我启动相机,拍摄第一张照片,等待几秒钟,拍摄第二张照片并计算差异。无论如何,我运行这个脚本,&#34; val1&#34;变量始终为0.0。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

因此,使用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

正如我所料。

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