OpenCV错误 - <unknown>不是一个numpy数组

时间:2015-09-06 22:20:58

标签: python opencv numpy

我收到此错误:

END

执行此代码时:

<unknown> is not a numpy array

这是追溯:

import cv2
import numpy as np

try:

    cap = cv2.VideoCapture(0)

    while (cap.isOpened()):
        ret,img=cap.read()
        cv2.imshow('output',img)
        img2=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
        cv2.imshow('gray_scale',img2)
        imgthreshold=cv2.inRange(img,cv2.cv.Scalar(3,3,125),cv2.cv.Scalar(40,40,255),)
        cv2.imshow('thresholded',imgthreshold)

        k=cv2.waitKey(10)
        if k==27:
            break

    cap.release()
    cv2.destroyAllWindows()


except Exception as inst:

    cap.release()
    cv2.destroyAllWindows()
    print("Eroor!!")
    print(inst)
    raise

我希望你能帮助我解决这个问题。我已经检查了所有依赖项,如果删除行

,它们也能正常工作
Traceback (most recent call last):
    File "C:\Users\... ...\camara.py", line 14, in <module>
        imgthreshold=cv2.inRange(img,cv2.cv.Scalar(3,3,125),cv2.cv.Scalar(40,40,255),)
TypeError: <unknown> is not a numpy array

和下一个,程序没有问题

2 个答案:

答案 0 :(得分:1)

您需要检查VideoCapture::read功能返回的状态。它返回一个布尔标志,指示返回的图像是否完全有效。

import cv2
import numpy as np

try:

    cap = cv2.VideoCapture(0)

    while (cap.isOpened()):
        ret,img=cap.read()
        if not ret:
            continue

        cv2.imshow('output',img)
        img2=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
        cv2.imshow('gray_scale',img2)
        imgthreshold=cv2.inRange(img,cv2.cv.Scalar(3,3,125),cv2.cv.Scalar(40,40,255),)
        cv2.imshow('thresholded',imgthreshold)

        k=cv2.waitKey(10)
        if k==27:
            break

    cap.release()
    cv2.destroyAllWindows()


except Exception as inst:

    cap.release()
    cv2.destroyAllWindows()
    print("Eroor!!")
    print(inst)
    raise

答案 1 :(得分:1)

我找到了解决方案,它是将颜色的顶部参数转换为numpy数组 - &gt;

imgthreshold=cv2.inRange(img,np.array(cv2.cv.Scalar(3,3,125)),np.array(cv2.cv.Scalar(40,40,255)),)