Opencv:用Python导入highgui
下面是一些代码:
import cv
cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
def repeat():
global capture #declare as globals since we are assigning to them now
global camera_index
frame = cv.QueryFrame(capture)
cv.ShowImage("w1", frame)
c = highgui.cvWaitKey(10)
if(c=="n"): #in "n" key is pressed while the popup window is in focus
camera_index += 1 #try the next camera index
capture = cv.CaptureFromCAM(camera_index)
if not capture: #if the next camera index didn't work, reset to 0.
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
while True:
repeat()
出现了一个错误信息(Traceback),意思是最近的一次调用出错了: 文件 "pycam.py",第21行,在 repeat() 这个函数里 文件 "pycam.py",第12行,在 repeat 函数里 c = highgui.cvWaitKey(10) 这里出错了 NameError: global name 'highgui' is not defined 这个错误的意思是说,'highgui' 这个名字没有被定义,也就是说程序找不到这个东西。 最后,摄像头已经被清理了。
2 个回答
0
这意味着 highgui
这个东西不存在。你可以试着这样导入: from opencv import *
。
如果这样还不行,建议你检查一下你的 opencv 安装情况。
6
新API有很多变化。下面的代码可以正常工作:
import cv
cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
def repeat():
global capture #declare as globals since we are assigning to them now
global camera_index
frame = cv.QueryFrame(capture)
cv.ShowImage("w1", frame)
c = cv.WaitKey(10)
if(c=="n"): #in "n" key is pressed while the popup window is in focus
camera_index += 1 #try the next camera index
capture = cv.CaptureFromCAM(camera_index)
if not capture: #if the next camera index didn't work, reset to 0.
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
while True:
repeat()
这个语法更简单、更清晰了!