I am trying to create an image similar to the attached image.
Here is my code:
mri_image = cv2.imread('mri_image.png', cv2.IMREAD_GRAYSCALE)
mask_image = cv2.imread('mask_image.png', cv2.IMREAD_GRAYSCALE) # Read mask image (contains black and white)
mri_image_rgb = cv2.cvtColor(mri_image, cv2.COLOR_GRAY2RGB)
colored_mask = np.zeros_like(mri_image_rgb)
colored_mask[mask_image > 0] = [255, 0, 0]
overlayed_image = cv2.addWeighted(mri_image_rgb, 0.7, colored_mask, 0.3, 0)
cv2.imwrite('overlayed_image.png', overlayed_image)
The resultant image has problems; the tissue part appears all black and not grayscale values and the red mask is not transparent.
addWeighted()
is the wrong thing to do.
To "colorize" parts of an image, you need to multiply.
overlay = cv.cvtColor(mask_image, cv.COLOR_GRAY2BGR) / 255
overlay_color = np.array([0, 0, 255]) / 255 # BGR order
overlay = overlay * overlay_color
overlay_strength = 0.3
overlay = 1 - ((1 - overlay) * overlay_strength)
composite = (mri_image_color * overlay).astype(np.uint8)