2

我正在尝试将 numpy 调色板方法应用于 opencv 处理的视频(参考:this question and this tutorial)。我的目标是用另一个替换某个颜色范围的所有帧像素。下面的代码是将黑色替换为绿色的示例。不幸的是,我的代码在线引发了一个错误:

image[np.where((image==[0,0,0]).all(axis=2))]=green

错误:exceptions.ValueError:axis(=2) out of bounds

我正在使用 PyScripter 运行 python 2.7,我觉得这很奇怪,因为代码之前确实有效,而且我没有对其进行任何重大修改。有人可以帮帮我吗?我很坚持这个...

我的代码:

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

##IMPORTS
import cv2.cv as cv
import numpy as np
import time

##VARIABLES
#colors
green=[0,255,0]

##MAIN
#video file input
frames = raw_input('Please input video file:')
if not frames:
   print "This program requires a file as input!"
   sys.exit(1)

#create window
cv.NamedWindow("image", 1)

#File capture
vidFile = cv.CaptureFromFile(frames)
nFrames = int(  cv.GetCaptureProperty( vidFile, cv.CV_CAP_PROP_FRAME_COUNT ) )
fps = cv.GetCaptureProperty( vidFile, cv.CV_CAP_PROP_FPS )
waitPerFrameInMillisec = int( 1/fps * 1000/1 )

#time adjustment, frame capture
for f in xrange( nFrames ):
   frame = cv.QueryFrame( vidFile )

   # create the images we need
   image = cv.CreateImage (cv.GetSize (frame), 8, 3)

   # copy the frame, so we can draw on it
   if not frame:
       break
   else:
       cv.Copy (frame, image)

   #get pixel HSV colors
   rows,cols=cv.GetSize(frame)
   image=np.asarray(image)
   image[np.where((image==[0,0,0]).all(axis=2))]=green
   image=cv.fromarray(image)

   #show the image
   cv.ShowImage("image", image)

   #quit command ESC
   if cv.WaitKey(waitPerFrameInMillisec)==27:
      break
   else:
      cv.WaitKey(waitPerFrameInMillisec) % 0x100
4

1 回答 1

1

我解决了。事实上,答案不是修改引发异常的行,而是修改传递给该行的参数。实际上,在将 cvMat 转换为 Numpy 并返回时,opencv 中似乎需要 '[:,:]' 参数。这是更正后的代码:

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

##IMPORTS
import cv2.cv as cv
import numpy as np

##VARIABLES
#colors
green=[0,255,0]

##MAIN
#start video stream analysis
frames = raw_input('Please enter video file:')
if not frames:
   print "This program requires a file as input!"
   sys.exit(1)


# first, create the necessary windows
cv.NamedWindow ('image', cv.CV_WINDOW_AUTOSIZE)


#File capture
vidFile = cv.CaptureFromFile(frames)
nFrames = int(  cv.GetCaptureProperty( vidFile, cv.CV_CAP_PROP_FRAME_COUNT ) )
fps = cv.GetCaptureProperty( vidFile, cv.CV_CAP_PROP_FPS )
waitPerFrameInMillisec = int( 1/fps * 1000/1 )


for f in xrange( nFrames ):
   #time adjustment, frame capture
   sec = f/fps
   frame = cv.QueryFrame( vidFile )

   # create the images we need
   image = cv.CreateImage (cv.GetSize (frame), 8, 3)


   # copy the frame, so we can draw on it
   if not frame:
      break
   else:
      cv.Copy (frame, image)

   #Replace pixel colors
   rows,cols=cv.GetSize(frame)
   image=np.asarray(image[:,:])
   image[np.where((image==[0,0,0]).all(axis=2))]=green
   image=cv.fromarray(image[:,:])

   #show the image
   cv.ShowImage("image", image)

   #quit command ESC
   if cv.WaitKey(waitPerFrameInMillisec)==27:
      break
   else:
      cv.WaitKey(waitPerFrameInMillisec) % 0x100

cv.DestroyAllWindows()
于 2013-06-02T12:16:09.677 回答