How to superimpose heatmap of size (1024, 1024) on Image of size (1024, 1024, 3)?

asked 2020-06-29 13:07:56 -0500

Riya208 gravatar image

updated 2020-06-29 13:08:45 -0500

Hello,

I am still learning cv2 and trying to understand how to get things done.

I have a heatmap of size (1024, 1024) which is in the range of 0.0 and 1.0. The heatmap is of type 'float64' and it looks like this

heatmap

I have an image of size (1024, 1024, 3) of type 'float32'.

img of size (1024, 1024, 3)

When I superimpose heatmap on image using matplotlib, it works fine.

colormap = 'plasma'
fig = plt.figure(figsize=(20,20))

ax2 = plt.subplot(1, 4, 3, aspect='equal')
ax2.imshow(np.squeeze(img), alpha = 0.5, cmap='plasma')
hm = ax2.imshow(heatmap, alpha = 0.5)

Result is:

heatmap_on_image

How do I do the same using cv2?

I tried:

output = cv2.addWeighted(heatmap, 0.7, img, 0.3, 0)

It gave me the following error:

error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:669: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'

In order to match the size of heatmap and the image,

heatmap1 = cv2.cvtColor(heatmap.astype('float32'), cv2.COLOR_GRAY2RGB)

Now, the shape is (1024, 1024, 3), but when I plotted heatmap1 it shows

heatmap1

Any help or suggestions would be of great help! Thank you :)

Please note that I would like to keep the image and the heatmap in the range of [-1, 1] or [0,1] if possible. Converting the input image to dtype 'uint16' doesn't really work well. It shows the image with noise.

edit retag flag offensive close merge delete