mask_all = np.zeros((256, 256), dtype='uint8') 單通道
mask_all_enlarge = np.zeros((256, 256, 3), dtype='uint8' 三通道
#為三通道圖像賦值,這里我用的是循環,因該還有更簡單的方式
img_base = np.zeros((256, 256, 3), np.uint8)
for i in range(256):
for j in range(256):
img_base[i, j, 0] = np.uint8(123.7)
img_base[i, j, 1] = np.uint8(116.8)
img_base[i, j, 2] = np.uint8(103.9)
#為圖像的一部分賦值為另外一附圖像
img_base[64: 192, 104: 152] = img
#兩幅圖像之間可以直接進行或運算:
mask_all = mask_all | r['masks'][:, :, i]
mask_all = mask_all | r['masks'][:, :, person_index]
#將單通道圖像依次填充到三通道圖像中:
mask_all_enlarge[:, :, 0] = mask_all
mask_all_enlarge[:, :, 1] = mask_all
mask_all_enlarge[:, :, 2] = mask_all
#兩個三通道圖像可以直接進行乘法運算:
image_mask = mask_all_enlarge * img_base
另外python中使用cv2讀寫圖像與skimage.io.讀寫圖像時,三個通道之間的順序是不相同的,使用的時候最好統一使用同一個,如果一不小心用一個讀,另一個寫,后面還可以再讀入寫出進行調換,就是比較麻煩了。