U.S*_*wap 1 python opencv numpy image
我有几张卫星图像,每张都代表主卫星图像的一个通道,总共 11 张图像,每张都标有不同的通道,所有图像都是带有灰度色彩空间的 .tiff 格式,现在我只想将这些图像合并到一,将所有通道表示为一个图像,这可能吗,请记住,我不想连接图像,可以使用以下方法完成:
vis = np.concatenate((img1, img2), axis=1)
Run Code Online (Sandbox Code Playgroud)
我想将它们全部合并为一张图像,而不会扭曲其中包含的数据,下面附上很少的通道图像。

任何帮助表示赞赏。
小智 7
首先,您需要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)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
为了快速获得结果,我手动将四张图像的大小编辑为442(h) x 331(w)像素。
要合并 11 个图像,您只需将代码扩展为:
img = img_1/no_img + img_2/no_img + img_3/no_img + ... + img_11/no_img
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12806 次 |
| 最近记录: |