Opencv:用python导入highgui

2024-10-01 19:34:10 发布

您现在位置:Python中文网/ 问答频道 /正文

使用以下代码:

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()

回溯(最近一次呼叫时间): 文件“pycam.py”,第21行,in 重复() 文件“pycam.py”,第12行,重复 c=highgui.cvWaitKey(10个) 名称错误:未定义全局名称“highgui” 清理了摄像机。


Tags: thetoinindexifglobalframecv
2条回答

新的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()

这是一个更简单,更干净的语法!

这意味着highgui不存在。尝试以这种方式导入:from opencv import *

如果不起作用,试着检查你的opencv安装。

相关问题 更多 >

    热门问题