【发布时间】:2015-08-05 14:50:37
【问题描述】:
我正在使用 Raspberry Pi、官方 Raspberry Pi 摄像头和 Python 的 OpenCV 编写运动检测系统。当我使用 absdiff 和 bitwise_and 操作时,它提出了这个:
OpenCV 错误:在 cvtColor 中断言失败 (scn == 3 || scn == 4), 文件 /home/pi/opencv-2.4.10/modules/imgproc/src/color.cpp,第 3739 行 Traceback(最近一次调用最后一次):文件“icanseu-diff.py”,第 18 行, 在 t_minus = cv2.cvtColor(camera.capture(rawCapture, format = "bgr", use_video_port = True), cv2.COLOR_RGB2GRAY) cv2.error: /home/pi/opencv-2.4.10/modules/imgproc/src/color.cpp:3739:错误: (-215) scn == 3 ||函数 cvtColor 中的 scn == 4
代码如下:
import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
camera = PiCamera()
camera.resolution = (320, 240)
camera.framerate = 30
camera.rotation = 180
rawCapture = PiRGBArray(camera, size = (320, 240))
def diffImg(t0, t1, t2):
d1 = cv2.absdiff(t2, t1)
d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)
# Read three images first
frame1 = cv2.cvtColor(camera.capture(rawCapture, format = "bgr", use_video_port = True), cv2.COLOR_RGB2GRAY)
frame2 = cv2.cvtColor(camera.capture(rawCapture, format = "bgr", use_video_port = True), cv2.COLOR_RGB2GRAY)
frame3 = cv2.cvtColor(camera.capture(rawCapture, format = "bgr", use_video_port = True), cv2.COLOR_RGB2GRAY)
while True:
cv2.imshow( motions, diffImg(t_minus, t, t_plus) )
# Read next image
frame1 = frame2
frame2 = frame3
frame3 = cv2.cvtColor(camera.capture(rawCapture, format = "bgr", use_video_port = True), cv2.COLOR_RGB2GRAY)
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(motions)
break
似乎是分配问题,但我不知道如何处理。我该怎么办?谢谢!
【问题讨论】:
-
您不应该使用
rgb格式而不是bgr来捕获图像,因为您的PiRGBArray旨在拍摄rgb图像?
标签: python opencv raspberry-pi