将3通道图像转换为1通道python

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

我正在使用Unet在python中进行分段,而我的unet的输出是具有此形状[512,512,1]的蒙版。

[预测蒙版后,我想在预测蒙版和测试图像的真实蒙版之间进行f1评分。问题在于实际的蒙版形状为[512,512,3],当我转换为灰度时,形状更改为[512,512]。我需要将实际蒙版从[512,512,3]转换为[512,512,1]。

有人可以帮我吗?

python image-segmentation unity3d-unet
1个回答
0
投票

您可以使用Pillow

from PIL import Image
img = Image.open('image.png').convert('LA')
img.save('greyscale.png')
          • OR

使用matplotlib和the formula

Y' = 0.2989 R + 0.5870 G + 0.1140 B

您可以做:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])

img = mpimg.imread('image.png')
gray = rgb2gray(img)
plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()

您可以参考answer以获取更多信息

© www.soinside.com 2019 - 2024. All rights reserved.