Bootstrap

cv2保存图片&将mask覆盖在原图上

cv2保存图片

cv2 保存图片 cv2保存图片默认通道 BGR,无归一化:

img = mask_a[0].clone().detach().double().to(torch.device('cpu'))
img = np.ascontiguousarray(img.numpy().transpose((1,2,0)))
cv2.imwrite(f"mask.jpg", img)

若图片数据为RGB格式,则需要将其转化为BGR格式进行保存:

img = origanImage.clone().detach().double().to(torch.device('cpu'))
img = np.ascontiguousarray(img.numpy().transpose((1,2,0)))*255
img = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_RGB2BGR)
cv2.imwrite(f"img.jpg", img)

由于 torchvision 中 transform.ToTensor() 函数默认会进行归一化操作,因此*255

将mask添加在img上

img = images_a[0].clone().detach().double().to(torch.device('cpu'))
img = np.ascontiguousarray(img.numpy().transpose((1,2,0)))*255
img = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_BGR2RGB)

mask = mask_a[0].clone().detach().double().to(torch.device('cpu'))
mask = np.ascontiguousarray(mask.numpy())
# BGR
color = (0, 0, 1.0)
for c in range(3):
img[:, :, c] = np.where(mask == 1, img[:, :, c] * (1 - 0.5) + 0.5*color[c]* 255, img[:, :, c])
cv2.imwrite('1.jpg', img)

;