OpenCV python中的实时热图生成
我想在视频播放时生成热图,基本上我想显示人群密度。我不知道该怎么做,我现在正在做,它在处理完整个视频后计算热图,我只是想让它直播,就像视频正在播放,颜色在不同的地方扩散一样OpenCV python中的实时热图生成,python,python-2.7,opencv,numpy,opencv3.0,Python,Python 2.7,Opencv,Numpy,Opencv3.0,我想在视频播放时生成热图,基本上我想显示人群密度。我不知道该怎么做,我现在正在做,它在处理完整个视频后计算热图,我只是想让它直播,就像视频正在播放,颜色在不同的地方扩散一样 import numpy as np import cv2 class Motion: def __init__(self): print("Motion Detection Object Created") # input file name of video self.inname = 'd
import numpy as np
import cv2
class Motion:
def __init__(self):
print("Motion Detection Object Created")
# input file name of video
self.inname = 'data.mp4'
# file name to save
self.outname = "C:/Heatmap"
def prep(self):
# just read the first frame to get height and width
cap = cv2.VideoCapture(self.inname)
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=1000, detectShadows=True)
#print(fgbg[0])
while (1):
ret, frame = cap.read()
#print(frame)
if not ret:
break
fgmask = fgbg.apply(frame)
self.accumulator = self.accumulator + fgmask
def write(self):
self.ab = cv2.convertScaleAbs(255 - np.array(self.accumulator, 'uint8'))
# only get reasonable high values, above mean
ret, self.acc_thresh = cv2.threshold(self.ab, self.ab.mean(), 255, cv2.THRESH_TOZERO)
# make a color map
acc_col = cv2.applyColorMap(self.acc_thresh, cv2.COLORMAP_RAINBOW)
cv2.imwrite(str(self.outname + "/heatmap.jpg"), acc_col)
# add to original frame
backg = cv2.addWeighted(np.array(acc_col, "uint8"), 0.45, self.orig_image, 0.55, 0)
cv2.imwrite(str(self.outname + "/heatmap_background.jpg"), backg)
# MAIN ENTRY POINT
if __name__ == "__main__":
motionVid = Motion()
motionVid.prep()
motionVid.run()
motionVid.write()
在
write
函数中,尝试添加以下内容:
VideoWriter video("output.avi",CV_FOURCC('M','J','P','G'),10,Size(frame_width,frame_height));
此处frame\u width
=宽度和frame\u height
=高度
您可以在OpenCV文档中获得视频压缩的编解码器列表。在
write
函数中,尝试添加以下内容:
VideoWriter video("output.avi",CV_FOURCC('M','J','P','G'),10,Size(frame_width,frame_height));
此处frame\u width
=宽度和frame\u height
=高度
您可以在OpenCV文档中获得视频压缩的编解码器列表。看起来您现在的方式只是在末尾保存一个图像,这是累加器的最新值。但是,你的累加器在整个视频中求和……这有什么作用呢?看起来你现在的方式只是在最后保存一幅图像,这是累加器的最新值。但是,在整个视频中,你的累加器求和……这能实现什么?