Reputation: 103
i would like convert two grayscale images [256,256,1] - [256,256,1] to one 2 Channel Image [256,256,2]..
How can I do this? How can I conacte the two images to one?
Upvotes: 2
Views: 4327
Reputation: 127
The most basic principle is “matplotlib/opencv's image is actually numpy ndarray”, so you can use multiple methods supported by 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. 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]. 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].
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.
Upvotes: 1