Python OpenCV错误:断言失败(scn==3 | | scn==4)
我正在尝试使用OpenCV和python检测对象。这是我试图运行的代码Python OpenCV错误:断言失败(scn==3 | | scn==4),python,opencv,numpy,object-detection,Python,Opencv,Numpy,Object Detection,我正在尝试使用OpenCV和python检测对象。这是我试图运行的代码 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
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
当我运行这段代码时,它给出了以下错误
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
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'
我尝试过几种方法来改变color.BRG2GRAY…等等,我还尝试过使用我的默认网络摄像头和其他usb网络摄像头。但这两种方法都会产生相同的错误。我能做些什么来解决这件事
当我在Ubuntu平台上运行相同的代码时,它给出了以下错误
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
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返回2个值,您可能不需要第一个值 所以试试这个:
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)
同样,下一节:
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
对于Ubuntu平台:只需将属性cv2.CV\u WINDOW\u AUTOSIZE更改为cv2.WINDOW\u 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
该错误意味着您的输入图像没有3或4个通道,这是彩色图像假定的。我不知道PythonAPI,但是cam.read[1]是否意味着只使用1号频道?请改为尝试cam.read。我试过了,但它给出的src不是一个数值元组错误@Mickacan你将图像读取到一个变量,然后显示未修改的图像?并提前测试捕获的图像是否为empzy?