gpt4 book ai didi

python - OpenCV 错误 - 不是 numpy 数组

转载 作者:太空宇宙 更新时间:2023-11-03 21:08:43 24 4
gpt4 key购买 nike

我收到这个错误:

<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

我希望你能帮我解决这个问题。我已经检查了所有的依赖项,如果我删除行,它们也能正常工作

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

下一个,程序运行没有问题

最佳答案

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

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

关于python - OpenCV 错误 - <unknown> 不是 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32428855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com