din*_*ece 3 python opencv image-processing python-2.7
import cv2
ram_frames=30
cam = cv2.VideoCapture(0)
def get_image():
cap = cam.read()
return cap
for i in xrange(ramp_frames):
temp = get_image()
image = get_image()
cv2.imwrite('bin/color.jpg',image)
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
File "C:\modules\imlib.py", line 1035, in __init__
self.imin = self.WinWebCam()
File "C:\modules\imlib.py", line 1125, in WinWebCam
cv2.imwrite('bin/color.jpg',image)
TypeError: img is not a numerical tuple
Run Code Online (Sandbox Code Playgroud)
我做了一切正确我没有改变任何代码,当在一个单独的程序中执行它没有显示任何错误,但在我的代码中运行它显示错误.我复制的代码来自此链接
您在复制时更改了代码.显然,cam.read()
返回一个元组.从文档:
Python: cv2.VideoCapture.read([image]) ? retval, image
Run Code Online (Sandbox Code Playgroud)
您正在返回的整个元组retval
和image
,而例如只返回它的第二部分(图像).因此image
,第9行中的变量包含返回的完整元组,read()
而示例仅返回其第二部分.imwrite
然后失败,因为它不期望一个元组作为参数.
尝试更改您的代码,如下所示:
def get_image():
_, cap = cam.read()
return cap
Run Code Online (Sandbox Code Playgroud)
或者,甚至更好,
def get_image():
return cam.read()[1]
Run Code Online (Sandbox Code Playgroud)
此外,你有拼写错误的变量ramp_frames
如ram_frames
在第2行.
归档时间: |
|
查看次数: |
9505 次 |
最近记录: |