python - OpenCV累积最大值

标签 python opencv motion-detection

我正在尝试使用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)

最佳答案

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

关于python - OpenCV累积最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28072754/

相关文章:

python re如何在特定单词后留出空间?

python - 如何在图像旋转后重新映射一个点?

python - 在for循环中从多个视频中提取帧

c++ - 如何使用 OpenCV 有效地检测图像中的簇中心

python - 选择具有边界规则的感兴趣区域

python - 如何修复 QPropertyAnimation "starting an animation without end value"错误

iphone - 如何使用加速度计计算步数?

opencv - opencv中的快速运动检测

c++ - 如何找到图像的非方形 ROI?

java - 在LINUX系统中使用python或java执行KNIME工作流程