2015-10-10 2353 views
1

以下程序显示“前景”完全黑色,而不是“帧”。我还检查了'frame'中的所有值都等于'foreground'中的值。 他们有相同的信道,数据类型等 我使用python 2.7.6和OpenCV版本2.4.8使用Python中的Opencv从图像中减去背景

 
    import cv2 
    import numpy as np 

    def subtractBackground(frame,background): 
     foreground = np.absolute(frame - background) 
     foreground = foreground >= 0 
     foreground = foreground.astype(int) 
     foreground = foreground * frame 
     cv2.imshow("foreground",foreground) 
     return foreground 

    def main(): 
     cap = cv2.VideoCapture(0) 
     dump,background = cap.read() 

     while cap.isOpened(): 
      dump,frame = cap.read() 
      frameCopy = subtractBackground(frame,background) 
      cv2.imshow('Live',frame) 
      k = cv2.waitKey(10) 
      if k == 32: 
       break 

    if __name__ == '__main__': 
     main() 

回答

1

因为你告诉OpenCV的显示64bpc图像。您根据您的体系结构投掷.astype(int),这意味着“int64”或“int32”。改为使用.astype('uint8')。与完整的64位范围相比,255的最大亮度看起来是黑色的。

相关问题:

foreground = np.absolute(frame - background) 

整数下溢。表达式frame - background不会自动转换为签名数据类型。你需要一个不同的数据类型来进行这样的计算(如果性能无关紧要,请尝试浮动),或者找到一个能够为你处理整个事情的OpenCV函数。

foreground = foreground >= 0 

因为​​是类型“UINT8”,这永远不能否定的,结果是全1。只需插入一些print repr(foreground)语句来调试这些问题。