Skip to content

Instantly share code, notes, and snippets.

@adamhrv
Last active June 17, 2021 19:31
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adamhrv/5224da679b444bfeaa0cdff6dfd29c53 to your computer and use it in GitHub Desktop.
Save adamhrv/5224da679b444bfeaa0cdff6dfd29c53 to your computer and use it in GitHub Desktop.
Convert image to heatmap
#!/usr/bin/env python
import cv2
import numpy as np
import argparse
'''
Create blended heat map with JET colormap
'''
def create_heatmap(im_map, im_cloud, kernel_size=(5,5),colormap=cv2.COLORMAP_JET,a1=0.5,a2=0.5):
'''
img is numpy array
kernel_size must be odd ie. (5,5)
'''
# create blur image, kernel must be an odd number
im_cloud_blur = cv2.GaussianBlur(im_cloud,kernel_size,0)
# If you need to invert the black/white data image
# im_blur = np.invert(im_blur)
# Convert back to BGR for cv2
#im_cloud_blur = cv2.cvtColor(im_cloud_blur,cv2.COLOR_GRAY2BGR)
# Apply colormap
im_cloud_clr = cv2.applyColorMap(im_cloud_blur, colormap)
# blend images 50/50
return (a1*im_map + a2*im_cloud_clr).astype(np.uint8)
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument('-m','--map-image',default='map.png',required=True,help="Path to map image")
ap.add_argument('-c','--cloud-image',default='clouds.png',required=True,help="Path to cloud image (grayscale)")
ap.add_argument('-s','--save',action='store_true',default=False,help="Save image")
#ap.add_argument('--cloud-pos',default=(0,0),required=False,help="Position of cloud, relative to map origin")
args = ap.parse_args()
im_map = cv2.imread(args.map_image)
im_cloud =cv2.imread(args.cloud_image)
# Normalize cloud image?
im_heatmap = create_heatmap(im_map, im_cloud, a1=.5, a2=.5)
if args.save:
cv2.imwrite('clouds_map_out.png',im_heatmap)
else:
cv2.imshow('cloud cover',im_heatmap)
cv2.waitKey(0)
@Rafay-create
Copy link

usage: Heat map.py [-h] -m MAP_IMAGE -c CLOUD_IMAGE [-s]
Heat map.py: error: the following arguments are required: -m/--map-image, -c/--cloud-image

this error is coming. what should I do?

@AKASH2907
Copy link

Those arguments are required you need to pass them while running the code.
For e.g. python heatmap.py -m map.png -c cloud.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment