opencv python将不同的频道图像合并到

2024-06-07 06:05:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几个卫星图像,每个都代表主卫星图像的一个通道,总共11个图像,每个都有不同的通道标签,所有的图像都是。tiff格式的灰度彩色空间,现在我只想把这些图像合并成一个,把所有通道表示成一个图像,这可能吗,请记住,我不想浓缩图像,可以使用以下方法:

vis = np.concatenate((img1, img2), axis=1)

我想把它们合并成一个单一的图像,而不扭曲其中包含的数据,下面只附上几个通道图像。 enter image description hereenter image description hereenter image description hereenter image description here

如有任何帮助,我们将不胜感激。


Tags: 方法图像格式np空间代表标签vis
3条回答

合并是什么意思?它们都是灰度图像,所以它们在灰度颜色空间中基本上是相同的通道。当您尝试blendadd图像时,某些信息注定会丢失。使用OpenCV或枕头中的函数尝试以上两种方法。

mul1 = ImageChops.add(img1, img2, scale=2)
mul2 = ImageChops.add(img3, img4, scale = 2)
mul3 = ImageChops.add(mul1, mul2, scale = 2)

mul3.show()

using Add

R1 = img1.convert('RGBA').resize([456,512])
R2 = img2.convert('RGBA').resize([456,512])
R3 = img3.convert('RGBA')
R4 = img4.convert('RGBA')

S1 = ImageChops.blend(R1,R2,0.5)
S2 = ImageChops.blend(R3,R4,0.5)
S3 = ImageChops.blend(S1,S2,0.5)
S3.show()

Using Blend

OpenCV 3.x将图像存储为numpy数组时,只要图像的高度和宽度完全相同,我们就可以对每个图像进行平均并将它们相加。

img_1 = cv2.imread('./imagesStackoverflow/sat_1_331-442.png')
img_2 = cv2.imread('./imagesStackoverflow/sat_2_331-442.png')
img_3 = cv2.imread('./imagesStackoverflow/sat_3_331-442.png')
img_4 = cv2.imread('./imagesStackoverflow/sat_4_331-442.png')

no_img = 4
img = img_1/no_img + img_2/no_img + img_3/no_img + img_4/no_img

为了得到一个快速的结果,我手动编辑了四个图像的大小为442(h) x 331(w)像素。

enter image description here 下面是合并的图像,有3个通道。

enter image description here

要合并11个图像,您可以将代码扩展为:

img = img_1/no_img + img_2/no_img + img_3/no_img + ... + img_11/no_img

首先,您需要carefully考虑您拥有的number of channels,以便可以创建一个有用的图像。在下面的例子中,我假设您有三个通道(红色、绿色和蓝色),可以组合成一个RGB图像。

import numpy as np
import cv2

"""Read each channel into a numpy array. 
Of course your data set may not be images yet.
So just load them into three different numpy arrays as neccessary"""
a = cv2.imread('chanel_1.jpg', 0)
b = cv2.imread('chanel_2.jpg', 0)
c = cv2.imread('chanel_3.jpg', 0)

"""Create a blank image that has three channels 
and the same number of pixels as your original input"""
needed_multi_channel_img = np.zeros((a.shape[0], a.shape[1], 3))

"""Add the channels to the needed image one by one"""
needed_multi_channel_img [:,:,0] = a
needed_multi_channel_img [:,:,1] = b
needed_multi_channel_img [:,:,2] = c

"""Save the needed multi channel image"""
cv2.imwrite('needed_multi_channel_img.png',needed_multi_channel_img)

相关问题 更多 >