【问题标题】:currImage = cv.QueryFrame(capture) affects other variablescurrImage = cv.QueryFrame(capture) 影响其他变量
【发布时间】:2012-08-18 10:41:00
【问题描述】:

我正在尝试获取第一帧并保持不变,但在每次分配给另一个变量( currImage = cv.QueryFrame(capture) )后它都会发生变化。我做错了什么?

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)

#SET CAMERA INDEX BELOW
camera_index = -1 

capture = cv.CaptureFromCAM(camera_index)
isRunning = True
firstImage = cv.QueryFrame(capture)

def repeat():
  global capture #declare as globals since we are assigning to them now
  global camera_index
  global isRunning
  global firstImage
  c = cv.WaitKey(100) % 0x100
  currImage = cv.QueryFrame(capture) 
  cv.ShowImage("w1",firstImage)

  if(c==27):
    isRunning = False

while isRunning:
    repeat()

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    让我从opencv wiki引用你的两段,即这个页面:http://opencv.willowgarage.com/documentation/python/reading_and_writing_images_and_video.html

    cvQueryFrame:
    The function cvQueryFrame [...] is just a combination of GrabFrame and RetrieveFrame.
    
    RetrieveFrame:
    The function cvRetrieveFrame returns the pointer to the image grabbed.
    

    您在代码中所做的不是复制对象,您只是创建另一个指向相应内存的对象。对 cvQueryFrame 的后续调用只是修改对象,因此您必须复制它。 尝试这样做:

    import copy 
    import cv
    
    cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
    
    #SET CAMERA INDEX BELOW
    camera_index = -1 
    
    capture = cv.CaptureFromCAM(camera_index)
    isRunning = True
    firstImage = copy.deepcopy(cv.QueryFrame(capture))
    
    def repeat():
      global capture #declare as globals since we are assigning to them now
      global camera_index
      global isRunning
      global firstImage
      c = cv.WaitKey(100) % 0x100
      currImage = cv.QueryFrame(capture) 
      cv.ShowImage("w1",firstImage)
    
      if(c==27):
        isRunning = False
    
    while isRunning:
        repeat()
    

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 2019-01-19
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 2019-06-02
      • 1970-01-01
      相关资源
      最近更新 更多