简体   繁体   English

将 Python 中的两张灰度图像转换为一张 2 通道图像

[英]Convert two grayscale image to one 2 Channel Image in Python

i would like convert two grayscale images [256,256,1] - [256,256,1] to one 2 Channel Image [256,256,2] ..我想将两张灰度图像[256,256,1] - [256,256,1]转换为一张 2 通道图像[256,256,2] ..

How can I do this?我怎样才能做到这一点? How can I conacte the two images to one?如何将这两个图像合二为一?

The most basic principle is “matplotlib/opencv's image is actually numpy ndarray”, so you can use multiple methods supported by numpy.最基本的原理是“matplotlib/opencv的图像实际上是numpy ndarray”,所以可以使用numpy支持的多种方法。

Example:例子:

import numpy as np

# Create grayscale image A (The shape as you describe)
greyA = np.random.randint(0, high=256, size=(256, 256, 1))
# Create grayscale image B (The shape as you describe)
greyB = np.random.randint(0, high=256, size=(256, 256, 1))

# Confirm the shape of the grayscale image A
print(greyA.shape)  # (256, 256, 1)
# Confirm the shape of the grayscale image B
print(greyB.shape)  # (256, 256, 1)

# Merged image
merge_image = np.concatenate((greyA, greyB), axis=2)
# Confirm the shape of the Merged image
print(merge_image.shape)  # (256, 256, 2)

Answer your questioning in the comments在评论中回答你的问题

Read imshow() If the image is 8-bit unsigned, it is displayed as is.读取imshow()如果图像是 8 位无符号的,则按原样显示。 If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].如果图像是 16 位无符号或 32 位 integer,则像素除以 256。即取值范围 [0,255*256] 映射到 [0,255]。 If the image is 32-bit or 64-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].如果图像是 32 位或 64 位浮点,则像素值乘以 255。即值范围 [0,1] 映射到 [0,255]。

Therefore, it is not supported to directly output an image with a color space of 2. You may use an average or weighted average to fuse the pixel arrays of the two images.因此,不支持直接 output 一个颜色空间为 2 的图像。您可以使用平均或加权平均来融合两个图像的像素 arrays。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM