[英]Python OpenCV Error: Assertion failed (scn == 3 || scn == 4)
I am trying to detect objects using OpenCV and python. 我正在尝试使用OpenCV和python检测对象。 This is my code I tried to run.
这是我尝试运行的代码。
import cv2
def diffImg(t0, t1, t2):
d1 = cv2.absdiff(t2, t1)
d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)
cam = cv2.VideoCapture(1)
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
# Read three images first:
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_BGR2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_BGR2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_BGR2GRAY)
while True:
cv2.imshow( winName, diffImg(t_minus, t, t_plus) )
# Read next image
t_minus = t
t = t_plus
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_GRAY2BGR)
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName)
break
When I run this code it gives following error. 当我运行此代码时,它给出以下错误。
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file ..\..\..\..\opencv\modules\imgproc\src\color.cpp, line 3739
Traceback (most recent call last):
File "C:/Users/Ravi/PycharmProjects/Test/thread1.py", line 14, in <module>
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_BGR2GRAY)
cv2.error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
I have tried with changing color.BRG2GRAY for several ways(RGB2GRAY...etc) and I have tried using my default web cam and other usb web cam. 我已经尝试过几种方法来更改color.BRG2GRAY(RGB2GRAY ... etc),并且尝试使用默认的网络摄像头和其他USB网络摄像头。 But both ways it gives same error.
但是两种方式都给出相同的错误。 What can I do to solve this matter ?
我该怎么办才能解决这个问题?
When I run the same code in Ubuntu platform, it gives following error. 当我在Ubuntu平台上运行相同的代码时,出现以下错误。
Traceback (most recent call last):
File "/home/ravi/PycharmProjects/Test/thread1.py", line 11, in <module>
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
AttributeError: 'module' object has no attribute 'CV_WINDOW_AUTOSIZE'
cam.read() actually returns 2 values (you probably wont be needing the first value). cam.read()实际上返回2个值(您可能不需要第一个值)。
So try this: 所以试试这个:
cam = cv2.VideoCapture(1)
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
_,frame1 = cam.read()
_,frame2 = cam.read()
_,frame3 = cam.read()
# Read three images first:
t_minus = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
t = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
t_plus = cv2.cvtColor(frame3, cv2.COLOR_BGR2GRAY)
And similarly for the next section: 下一节类似:
while True:
cv2.imshow( winName, diffImg(t_minus, t, t_plus) )
# Read next image
t_minus = t
t = t_plus
_,frame = cam.read()
t_plus = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName)
break
For Ubuntu platform: just change the attribute cv2.CV_WINDOW_AUTOSIZE to cv2.WINDOW_NORMAL.This is due to opencv version i think 对于Ubuntu平台:只需将属性cv2.CV_WINDOW_AUTOSIZE更改为cv2.WINDOW_NORMAL。这是由于我认为是opencv版本
import cv2
def diffImg(t0, t1, t2):
d1 = cv2.absdiff(t2, t1)
d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)
cam = cv2.VideoCapture(0)
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.WINDOW_NORMAL)
# Read three images first:
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
while True:
cv2.imshow( winName, diffImg(t_minus, t, t_plus) )
# Read next image
t_minus = t
t = t_plus
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName)
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.