我有几张卫星图像,每张都代表主卫星图像的一个通道,总共 11 张图像,每张图像都标有不同的通道,所有图像均为 .tiff 格式,具有灰度色彩空间,现在我只想合并这些图像成一个,将所有通道表示成一张图像,所以这可能吗,请记住这里,我不想连接图像,这可以使用以下方法完成:
vis = np.concatenate((img1, img2), axis=1)
我想将它们全部合并成一张图像,而不扭曲其中包含的数据,下面附有一些通道图像。
如有任何帮助,我们将不胜感激。
首先,您需要
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)
由于
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)
像素。
要合并 11 张图像,您只需将代码扩展为:
img = img_1/no_img + img_2/no_img + img_3/no_img + ... + img_11/no_img
我已经连续尝试了 2 天,但我无法理解的一件事是 opencv 的合并函数接受一个元组,而不是 3 个不同的参数。
image = cv2.imread(image_file, 1)
R, G, B = cv2.split(image)
output_image = cv2.merge ( (R, G, B) )
合并是什么意思?它们都是灰度图像,因此它们在灰度颜色空间中基本上是相同的通道。当您尝试“混合”或“添加”图像时,某些信息注定会丢失。使用 OpenCV 或 Pillow 中的函数尝试上述两种方法。
mul1 = ImageChops.add(img1, img2, scale=2)
mul2 = ImageChops.add(img3, img4, scale = 2)
mul3 = ImageChops.add(mul1, mul2, scale = 2)
mul3.show()
这是相同的 Python 代码。它接受任何通道的图像,但必须保持宽度和高度,就像 hcontat、vcontat、hstack、vstack 的情况一样。
imgArray:是要连接的所有图像的数组,无论每个图像中的通道如何
import numpy as np
import cv2
def stackImages(scale,imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0],list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range(0,rows):
for y in range(0, cols):
if imgArray[x][y].shape[:2] == imgArray[x][y].shape[:2]:
imgArray[x][y] = cv2.resize(imgArray[x][y],(0,0),None,scale,scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[0]), None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y] =cv2.cvtColor(imgArray[x][y],cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height,width,3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x],(0,0),None,scale,scale)
else:
imgArray[x] = cv2.resize(imgArray[x],(imgArray[0].shape[0],imgArray[0].shape[1]),None,scale,scale)
if len(imgArray[x].shape) == 2: imgArray[x] =cv2.cvtColor(imgArray[x],cv2.COLOR_GRAY2BGR)
hor = np.hstack(imgArray)
ver = hor
return ver
属于提供轨迹栏和活动对象检测教程的人创建的包。检查此处:https://www.youtube.com/watch?v=Fchzk1lDt7Q&t=25s
我有一个问题。 我有 6 个灰度图像,属于一个场景,6 个灰度图像提供了有关一个场景的不同信息,我可以将 6 个 Png 图像连接为一个具有 6 个通道的 1 个图像吗?