'How to color a grayscale image based on a mask?
Solution 1:[1]
Expressing your grey image pixel values across 3-channels gives you a color image. The result would look the same but when you check the dimensions it would be 3.
gray_3_channel = cv2.merge((gray, gray, gray))
gray.shape
>>> (158, 99)
gray_3_channel.shape
>>> (158, 99, 3)
For every white (255) pixel in the mask, assign the color (255, 255, 0) in gray_3_channel:
gray_3_channel[mask==255]=(255, 255, 0)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


