【问题标题】:Python OpenCV Error: Assertion failed (scn == 3 || scn == 4)Python OpenCV 错误:断言失败(scn == 3 || scn == 4)
【发布时间】:2015-06-06 05:38:37
【问题描述】:

我正在尝试使用 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_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

我尝试过改变 color.BRG2GRAY 的几种方式(RGB2GRAY...等),并且我尝试过使用我的默认网络摄像头和其他 USB 网络摄像头。但是两种方式都会给出相同的错误。我该怎么做才能解决这个问题?

当我在 Ubuntu 平台上运行 same code 时,出现以下错误。

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'

【问题讨论】:

  • 该错误意味着您的输入图像没有 3 或 4 个通道,这是假设为彩色图像。我不了解 python api,但 cam.read()[1] 是否意味着您只使用通道号 1?请改用 cam.read()。
  • 我试过了,但它给出了src is not a numerical tuple错误@Micka
  • 您可以将图像读取到变量中并显示未修改的图像吗?并提前测试捕获的图像是否为空?

标签: python opencv numpy object-detection


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    对于 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多