Search code examples
pythonnumpyopencvimage-processingbitwise-operators

How to identify the pixels with the highest activation from four or more grayscale images and save as the final heatmap?


I have four attention maps, each of dimension 217 x 217 and in the form of array of float64. I need to perform an operation using these attention maps and save the final attention map where only the pixels with the highest activations are present. A custom function is needed in this regard. Attached are the heatmaps:

heatmap4.png heatmap3.png heatmap2.png heatmap1.png

The starter code is here:

import cv2
import numpy as np
import os
from PIL import Image

im1 = np.array(Image.open("heatmap1.png").convert('L'))
im2 = np.array(Image.open("heatmap2.png").convert('L'))
im3 = np.array(Image.open("heatmap3.png").convert('L'))
im4 = np.array(Image.open("heatmap4.png").convert('L'))

#compute to save only the pixels with the highest activation

#save final heatmap as a PNG file
cv2.imwrite("final_heatmap.png", bitwise_AND)

Solution

  • To compute the pixelwise maximum between two images, use np.maximum(im1, im2). The bitwise logical AND gives the maximum only for Boolean values, but does not generalize to gray-scale values.

    To find the pixelwise maximum across four images, use a pyramidal scheme with pairwise operators:

    out1 = np.maximum(im1, im2)
    out2 = np.maximum(im3, im4)
    out = np.maximum(out1, out2)
    

    You can do the above a bit more efficiently if you are willing to overwrite inputs:

    np.maximum(im1, im2, out=im1)
    np.maximum(im3, im4, out=im3)
    np.maximum(im1, im3, out=im1)  # im1 now has the result