【问题标题】:OpenCV accumulate max valueOpenCV累积最大值
【发布时间】:2015-01-21 16:59:52
【问题描述】:

我正在尝试使用 opencv 和 python 制作运动热图。

我的代码很简单,我在框架中读取,应用 MOG 背景减法,然后累积前景对象。

我发现累积数组不能超过 255。文档没有提到最大值。为什么是这样?我没有正确使用累积吗?

import numpy as np
import cv2

class Motion:

    def __init__(self):                  
                    print("Motion Detection Object Created")    
                    #input file name of video
                    self.inname= 'C:/Users/Ben/Desktop/MotionMeerkatTest/garcon_test.avi'

                    #file name to save
                    self.outname = "C:/MotionMeerkat"

    def prep(self):

        #just read the first frame to get height and width
        cap = cv2.VideoCapture(self.inname)     

        #uncomment this line and comment the one above if you want to read from webcam
        #cap = cv2.VideoCapture(0)     

        ret,self.orig_image = cap.read()
        width = np.size(self.orig_image, 1)
        height = np.size(self.orig_image, 0)
        frame_size=(height, width)           

        #make accumulator image of the same size
        self.accumulator =  np.zeros((height, width), np.float32) # 32 bit accumulator


    def run(self):
        cap = cv2.VideoCapture(self.inname)
        fgbg = cv2.createBackgroundSubtractorMOG2(varThreshold=80,detectShadows=False)
        while(1):
            ret, frame = cap.read()
            if not ret:
                break
            fgmask = fgbg.apply(frame)
            cv2.accumulate(fgmask,self.accumulator)
    def write(self):

        self.abs=cv2.convertScaleAbs(self.accumulator)  
        acc_col = cv2.applyColorMap(self.abs,cv2.COLORMAP_HOT)                
        cv2.imwrite(str(self.outname + "/heatmap.jpg"),acc_col)

        #add to original frame
        backg = cv2.addWeighted(np.array(acc_col,"uint8"),0.25,self.orig_image,0.75,0)

        cv2.imwrite(str(self.outname + "/heatmap_background.jpg"),backg)

【问题讨论】:

    标签: python opencv motion-detection


    【解决方案1】:

    在 C++ 文档中提到了"Value 0 in the mask always means background, 255 means foreground."。此外,看到的示例通常显示灰度图像,其中每个值限制为最大值 255。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-31
      • 2021-10-12
      相关资源
      最近更新 更多