如何将分割结果与另一幅图像叠加

问题描述 投票:0回答:0

这是我的输入图像: Image without segmentation

我有这段代码用颜色覆盖输入图像的分割:

# sample execution (requires torchvision)
from PIL import Image
from torchvision import transforms
input_image = Image.open("samping.JPG")
input_image = input_image.convert("RGB")
preprocess = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model

# move the input and model to GPU for speed if available
if torch.cuda.is_available():
    input_batch = input_batch.to('cuda')
    model.to('cuda')

with torch.no_grad():
    output = model(input_batch)['out'][0]
output_predictions = output.argmax(0)

# create a color pallette, selecting a color for each class
palette = torch.tensor([2 ** 25 - 1, 2 ** 15 - 1, 2 ** 21 - 1])
colors = torch.as_tensor([i for i in range(21)])[:, None] * palette
colors = (colors % 255).numpy().astype("uint8")

# plot the semantic segmentation predictions of 21 classes in each color
r = Image.fromarray(output_predictions.byte().cpu().numpy()).resize(input_image.size)
r.putpalette(colors)

然后我得到了这个结果:这是我用颜色覆盖的分割图像: Segmented image with color

但是,我想用另一个图像覆盖/更改颜色,例如这个图像:

Image used to overlay

我想要的结果图:

Segmented image that i want 将分段板(颜色)更改为我的徽标,我该如何实现?

python pytorch image-segmentation semantic-segmentation
© www.soinside.com 2019 - 2024. All rights reserved.