Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Click on an image to superimpose a heatmap
import cv2
import numpy as np
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--img', default='image.jpg')
args = parser.parse_args()
def collect_clicks(event, x, y, flags, param):
global points
if event == cv2.EVENT_LBUTTONDOWN:
heatmap[y][x] += 1.0
hmap = cv2.GaussianBlur(heatmap, (k_size, k_size), 0)
hmap = hmap/hmap.max()
hmap = (hmap*255).astype(np.uint8)
hmap = cv2.applyColorMap(hmap, cv2.COLORMAP_JET)
image = 0.7*clone + 0.3*hmap
image = image.astype(np.uint8)
cv2.imshow("image", image)
# load the image, clone it, and setup the mouse callback function
image = cv2.imread(args.img)
clone = image.copy()
heatmap = np.zeros((image.shape[0], image.shape[1]))
k_size = int(np.sqrt(image.shape[0]*image.shape[1])/5.0)
if k_size%2==0:
k_size += 1
cv2.namedWindow("image")
cv2.imshow("image", image)
cv2.setMouseCallback("image", collect_clicks)
while True:
key = cv2.waitKey(1) & 0xFF
if ord('q')==key:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.