keras 如何合并多个通道的图像?

bnl4lu3b  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(120)

我正在尝试使用多通道文件来训练unet分类。
我的数据集是5 * 1通道和1 * 3通道图像,我希望它可以是8通道.npy文件。
我使用np.concatenate来合并,但它不能应用于单通道图像。下面是我的代码:

for i in range(6):
  img = data[i]
  images.append(img)
img_batch = np.concatenate(images, axis=3)

字符串
因此,它应该首先将1通道图像扩展为3通道,然后连接,然后扩展为18通道.mpy文件

Image.open("class1_image1.jpg").convert("RGB")   #expand 1-channel-image to 3-channel image


有没有一种方法可以将多个图像合并到一个多通道.npy文件中,而不需要扩展单通道图像?
或者18通道和8通道是同一个文件的unet分类任务?

57hvy0tb

57hvy0tb1#

您能否提供图像(至少1通道图像)?不管图像有多少通道,或者即使图像有不同数量的通道,但我将做一个合并两个3通道图像和一个1通道图像的示例:
我们可以通过将图像插入第4维(然后在第4维上求和),将图像的RGB(或任何第3维通道)值与numpy合并。
首先,我们有一些图像(不管我们如何获得它们,我只是在这个例子中使用了Google图像),并确保它们的大小相同:

from skimage import io as io
image1 = io.imread('https://www.allrecipes.com/thmb/c_2gXiAwkO6u1UJCY-1eAVCy0h0=/1500x0/filters:no_upscale():max_bytes(150000):strip_icc()/54679_perfect-baked-potato-Rita-1x1-1-91711252bb3740088c8ea55c5f9bef1c.jpg')
image2 = io.imread('https://cdn.loveandlemons.com/wp-content/uploads/2020/01/baked-potato.jpg')
image3 = io.imread('https://www.southerncravings.com/wp-content/uploads/2020/01/Crispy-Baked-Potatoes-7.jpg')

image1 = image1[:, 200:-200, :]
image2 = image2[:-60, :-60, :]
import numpy as np
image3 = np.sum(image3[:-300, :-100, :], axis=2)

字符串
image1
x1c 0d1x的数据
image2



image3(仅1通道):



image3(在第3维度中级联之后,因此3通道;如下所述):



我们将不得不通过在第三维中使用numpyconcatenate函数来扩展图像列表中通道数小于最大值的图像:

import numpy as np
def make_list_of_images(images_list):
    list_of_images = []
    for _image in images_list:
        if len(_image.shape) == 2:
            _image.shape += (1,)
        if _image.shape[2] < np.max([_image_channels.shape[2] for _image_channels in images_list if len(_image_channels.shape) > 2]):
            _image = np.concatenate([_image for _ in range(np.max([_image_channels.shape[2] for _image_channels in images_list if len(_image_channels.shape) > 2]) - _image.shape[2] + 1)], axis=2)
        list_of_images.append(_image)
    return list_of_images


我们将不得不归一化RGB(或第三维通道)值,所以这里有一个函数来做到这一点:

import numpy as np
def normalize_rgb_values(rgb_values, max_value=1.0):
    norm_rgb_values = (rgb_values - np.mean(rgb_values)) / np.var(rgb_values)**0.5
    norm_rgb_values += abs(np.min(norm_rgb_values))
    norm_rgb_values *= (max_value / np.max(norm_rgb_values))
    return np.round(norm_rgb_values, decimals=0).astype(int) if max_value == 255 else np.round(norm_rgb_values, decimals=9).astype(float)


下面是imagesnumpy零数组,我们通过枚举list_of_images并将每个_image插入到第4维来填充:

import numpy as np
max_number_of_channels = np.max([_image_channels.shape[2] for _image_channels in list_of_images])
images = np.zeros((image1.shape[0], image1.shape[1], max_number_of_channels, len(list_of_images))).astype(float)
for _image_num, _image in enumerate(list_of_images):
    images[:, :, :, _image_num] = _image


我们可以使用numpysum来合并images(在第4个维度上求和),而不是连接:

import numpy as np
summed_images = np.sum(images, axis=3)
from matplotlib import pyplot as plt
plt.imshow(normalize_rgb_values(summed_images))
plt.show()


合并的图像(即summed_images):
x1c4d 1x的
代码如下:

from skimage import io as io
image1 = io.imread('https://www.allrecipes.com/thmb/c_2gXiAwkO6u1UJCY-1eAVCy0h0=/1500x0/filters:no_upscale():max_bytes(150000):strip_icc()/54679_perfect-baked-potato-Rita-1x1-1-91711252bb3740088c8ea55c5f9bef1c.jpg')
image2 = io.imread('https://cdn.loveandlemons.com/wp-content/uploads/2020/01/baked-potato.jpg')
image3 = io.imread('https://www.southerncravings.com/wp-content/uploads/2020/01/Crispy-Baked-Potatoes-7.jpg')
image1 = image1[:, 200:-200, :]
image2 = image2[:-60, :-60, :]
import numpy as np
image3 = np.sum(image3[:-300, :-100, :], axis=2)

def make_list_of_images(images_list):
    list_of_images = []
    for _image in images_list:
        if len(_image.shape) == 2:
            _image.shape += (1,)
        if _image.shape[2] < np.max([_image_channels.shape[2] for _image_channels in images_list if len(_image_channels.shape) > 2]):
            _image = np.concatenate([_image for _ in range(np.max([_image_channels.shape[2] for _image_channels in images_list if len(_image_channels.shape) > 2]) - _image.shape[2] + 1)], axis=2)
        list_of_images.append(_image)
    return list_of_images

list_of_images = make_list_of_images([image1, image2, image3])

def normalize_rgb_values(rgb_values, max_value=1.0):
    norm_rgb_values = (rgb_values - np.mean(rgb_values)) / np.var(rgb_values)**0.5
    norm_rgb_values += abs(np.min(norm_rgb_values))
    norm_rgb_values *= (max_value / np.max(norm_rgb_values))
    return np.round(norm_rgb_values, decimals=0).astype(int) if max_value == 255 else np.round(norm_rgb_values, decimals=9).astype(float)

from matplotlib import pyplot as plt
for _image in list_of_images:
    if np.max(_image) > 1.0:
        plt.imshow(normalize_rgb_values(_image))
    else:
        plt.imshow(_image)
    plt.show()

max_number_of_channels = np.max([_image_channels.shape[2] for _image_channels in list_of_images])
images = np.zeros((image1.shape[0], image1.shape[1], max_number_of_channels, len(list_of_images))).astype(float)
for _image_num, _image in enumerate(list_of_images):
    images[:, :, :, _image_num] = _image

summed_images = np.sum(images, axis=3)
plt.imshow(normalize_rgb_values(summed_images))
plt.show()


如果您使用ImagefromPIL对图像文件进行open操作,则可能必须首先将其设置为uint8typenumpyarray

import numpy as np
image = normalize_rgb_values(np.array(image).astype(np.uint8))

相关问题