I have two masks and the desired mask result as shown below:
2 original masks and the desired result
I want to create a desired mask result by combining the first 2 masks. Howvever, I only want to produce the white regions if they overlap. IF they don't overlap, then the area should remain black. I'm unsure of how to proceed.
At the moment I have imported the image via cv2 and created a third numpy array based on the dimensions of the original image. I then loop through the two masks and have set a condition based on whether the two values (255 or 0) are the same. If they are then I want to store them or set them in the new mask...:
necrosis_mask_observer_1 = cv2.imread(mask1, 0)
necrosis_mask_observer_2 = cv2.imread(mask2, 0)
map = np.empty(necrosis_mask_observer_1.shape)
height, width = map.shape
# do something here?
for i in range(width):
for j in range(height):
necrosis_mask_observer_1_sum = necrosis_mask_observer_1[j : (j+1), i : (i+1)].sum()
necrosis_mask_observer_2_sum = necrosis_mask_observer_2[j : (j+1), i : (i+1)].sum()
if necrosis_mask_observer_1_sum == necrosis_mask_observer_2_sum:
#do something here?
else:
continue
You can do that with a bitwise and operation in Python/OpenCV.
Mask1:
Mask2:
import cv2
# read mask 1
mask1 = cv2.imread('mask1.png')
# read mask 2
mask2 = cv2.imread('mask2.png')
# combine mask
mask3 = cv2.bitwise_and(mask1,mask2)
# save result
cv2.imwrite("mask3.png",mask3)
# show result
cv2.imshow('mask3', mask3)
cv2.waitKey(0)