Multiply Maps and Images Together OpenCV2

Viewed 67

I currently have a salience map for an image using the code below:

img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
saliency = cv2.saliency.StaticSaliencyFineGrained_create()
(success, saliencyMap) = saliency.computeSaliency(image)

# cv2.imshow("Image", image)
cv2.imshow("output", saliencyMap)

The output is the salience map below. It is normalized (I believe) and the array only contains values between 0 and 1.

enter image description here

I was wondering if there was a way to put this saliency mask over the image below to get an output image that highlights the salient regions?

enter image description here

I have tried splitting the image into 3 channels and multiplying each channel by the map and then stacking them back together but this hasn't shown to work. The code I tried was:

(B, G, R) = cv2.split(resized)
y = saliencyMap
# cv2.imshow("Blue", B)
# cv2.imshow("Green", G)
# cv2.imshow("Red", R)
# cv2.waitKey(0)

B = B * y.astype(y.dtype)
G = G * y.astype(y.dtype)
R = R * y.astype(y.dtype)


img = cv2.merge((B,G,R))
cv2.imshow("Image", img)
cv2.waitKey(0)

However, the output image I obtained was:

enter image description here

Which is obviously wrong. Any tips or advice?

1 Answers

You're correct, except that you need to convert your R,G,B to uint8 dtype. As of now they are float type (since saliency is also float), and float >1 will be white.

Also, you can use broadcast:

output = (img * saliency[...,None]).astype('uint8')

And output:

enter image description here

Related