简体   繁体   English

如何从picamera(python,opencv)中提取数组中的图像

[英]how to abstract images in an array from picamera(python , opencv)

I am quite new in picamera. 我在皮卡梅拉很新。 Now I want to detect uncomplicated motions by camera, and I use cv2.absdiff() . 现在,我想通过摄像头检测简单的动作,并使用cv2.absdiff() However, the arrays (image1, image2) I get are the same. 但是,我得到的数组(image1,image2)是相同的。 How can I get different arrays for abstraction or get the elements (images) in an array? 如何获得用于抽象的不同数组或获取数组中的元素(图像)? Here is my code: 这是我的代码:

import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
from picamera.array import PiRGBArray
from picamara import PiCamera
import cv2
import time
import time
import numpy as np

camera = PiCamera()
camera.resolution = (640,480)
camera.framerate = 32
rawCapture = PiRGBArray(camera,size=(640,480))

time.sleep(0.1)

for frame in camera.capture_continuous(rawCapture,format="bgr",use_video_port=True):

    image1 = frame.array
    gray1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)
    cv2.waitKey(33)
    image2 = frame.array
    gray2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)

    diff = cv2.absdiff(gray1,gray2)
    cv2.imshow("DIFF",diff)

    key = cv2.waitKey(33)&0xFF
    rawCapture.truncate(0)
    if key == ord("q"):
        break

Your script is attempting to compare the same frame with itself which will obviously not show any differences. 您的脚本正在尝试将同一帧与其自身进行比较,这显然不会显示任何差异。

One approach would be to take a single initial frame before starting your loop, and then compare any later frames to that, something like as follows: 一种方法是在开始循环之前获取一个初始帧,然后将其与任何后续帧进行比较,如下所示:

import sys
#sys.path.append('/usr/local/lib/python2.7/site-packages')
from picamera.array import PiRGBArray
from picamara import PiCamera
import cv2
import time
import time
import numpy as np

camera = PiCamera()
camera.resolution = (640,480)
camera.framerate = 32
rawCapture = PiRGBArray(camera,size=(640,480))
time.sleep(0.1)
initial_frame = camera.capture(rawCapture, format="bgr", use_video_port=True):

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    cv2.waitKey(33)
    image2 = frame.array
    gray = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)

    diff = cv2.absdiff(initial_frame, gray)
    cv2.imshow("DIFF", diff)

    key = cv2.waitKey(33) & 0xFF
    rawCapture.truncate(0)

    if key == ord("q"):
        break 

You could further enhance it to periodically update the initial frame to account for any long term changes to the background. 您可以进一步增强它以定期更新初始帧,以解决对背景的任何长期更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM