title: Apply heatmap on video with OpenCV and Python
body: <p>I want to write a code that will apply heatmap on the video on places where there is a movement.
  I wrote a code that detects movement, with contours but I do not know how to make heat map.</p>
  <p>This is the code that I have</p>
  <pre><code>import cv2
  import numpy as np
  
  # upload video
  cap = cv2.VideoCapture('test_video.mp4')
  
  #reading two frames
  ret, frame1 = cap.read()
  ret, frame2 = cap.read()
  
  while cap.isOpened():
  
       # get diference between two frames
       diff = cv2.absdiff(frame1, frame2)
  
       # convert diference in gray
       gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
  
       # bluring and treshold
       blur = cv2.GaussianBlur(gray, (5,5), 0)
       _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
  
       dilated = cv2.dilate(thresh, None, iterations = 3)
  
       # define contours
       contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
       
       # draw contours
       cv2.drawContours(frame1, contours, -1, (255,0,0), 1)
  
       # show frames
       cv2.imshow('frame', frame1)
       frame1 = frame2
       ret, frame2 = cap.read()
  
       if cv2.waitKey(60) == 60:
            break
  
  cv2.destroyAllWindows()
  cap.release()
  </code></pre>
  <p>I have seen this link: <a href="https://towardsdatascience.com/build-a-motion-heatmap-videousing-opencv-with-python-fd806e8a2340" rel="nofollow noreferrer">Build a Motion Heatmap Video Using OpenCV With Python</a>. And I wanted to reproduce the code, but a lot of stuff such as <code>fourcc</code>, <code>image_folder</code>, and <code>images</code> is not defined, so I tried to do it the other way.</p>
  <p>Can you help me with this? Basically, I want to apply the heatmap to the video where there is a movement.</p>