简体   繁体   中英

How to merge two bitmasks in OpenCV with different colors

I have to masks, for example these:

在此处输入图像描述 在此处输入图像描述

I want to merge them in one image, but the first and the second mask must have different colors, so the desired result is:

在此处输入图像描述

I know I can convert them both to BGR colorspace, then replace colors and merge them like this:

img1 = cv2.cvtColor(mask1, cv2.COLOR_GRAY2BGR)
img1[np.where((img1==[255, 255, 255]).all(axis=2))] = [0, 0, 255]
img2 = cv2.cvtColor(mask2, cv2.COLOR_GRAY2BGR)
img2[np.where((img2==[255, 255, 255]).all(axis=2))] = [0, 255, 0]
img = cv2.bitwise_or(img1, img2)

But it seems like there should be a simpler and more optimal way to do that. Is there such way?

Hope it help's you.

img1[img1[:,:,0]==255]=[0,255,0]
img2[img2[:,:,0]==255]=[0,0,255]
img1[img2[:,:,2]==255]=[0,0,255]

cv2.imshow('img1',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM