文章目录
- (3D-GRAY) to (3D-RGB):使用颜色映射的方式,将灰度值映射到彩色空间中的特定颜色。
- 一、多维数组:10x12x14
- (1)channel重复:在RGB中,将【灰度图】分别赋值给【R/G/B图】,显示彩色图。
- (2)channel叠加:在RGB的R图中,将【灰度图】与【R图】进行叠加,显示红色。
- (3)channel拼接:(10x12x14)x1+(10x12x14)x3=(10x12x14)x4
- 二、二维数组:10x10
- (1)channel叠加:10x12x1+10x12x3=10x12x3
(3D-GRAY) to (3D-RGB):使用颜色映射的方式,将灰度值映射到彩色空间中的特定颜色。
一、多维数组:10x12x14
(1)channel重复:在RGB中,将【灰度图】分别赋值给【R/G/B图】,显示彩色图。
import numpy as np
import tifffile
image_data = tifffile.imread('gray_image_raw.tif')
image_data = image_data.astype(np.uint32)
color_image = np.zeros((*image_data.shape, 3), dtype=np.uint8)
color_image[..., 0] = (image_data >> 8) & 0xFF
color_image[..., 1] = (image_data >> 4) & 0xFF
color_image[..., 2] = image_data & 0xFF
tifffile.imwrite('color_image.tif', color_image)
(2)channel叠加:在RGB的R图中,将【灰度图】与【R图】进行叠加,显示红色。
import numpy as np
import tifffile
gray_image_raw = tifffile.imread('gray_image_raw.tif')
gray_image_circle = tifffile.imread('gray_image_circle.tif')
color_image = np.zeros((*gray_image_raw.shape, 3))
color_image[..., 0] = gray_image_raw
color_image[..., 1] = gray_image_raw
color_image[..., 2] = gray_image_raw
color_image[gray_image_circle == 65535, 0] = 65535
color_image = color_image.astype(gray_image_raw.dtype)
tifffile.imwrite('color_image.tif', color_image)
print(color_image.shape[0], color_image.shape[1], color_image.shape[2], color_image.shape[3])
(3)channel拼接:(10x12x14)x1+(10x12x14)x3=(10x12x14)x4
import numpy as np
import tifffile
gray_image_raw = tifffile.imread('gray_image_raw.tif')
gray_image_circle = tifffile.imread('gray_image.tif')
color_image = np.zeros((*gray_image_raw.shape, 3))
color_image[..., 0] = gray_image_raw
color_image[..., 1] = gray_image_raw
color_image[..., 2] = gray_image_raw
color_image[gray_image_circle == 65535, 0] = 65535
color_image = color_image.astype(gray_image_raw.dtype)
gray_image_raw = np.expand_dims(gray_image_raw, axis=-1)
merged_image = np.concatenate((gray_image_raw, color_image), axis=3)
tifffile.imwrite('color_image.tif', merged_image)
print(merged_image.shape[0], merged_image.shape[1], merged_image.shape[2], merged_image.shape[3])
二、二维数组:10x10
(1)channel叠加:10x12x1+10x12x3=10x12x3
import cv2
import numpy as np
rgb_image = cv2.imread('image.jpg', cv2.IMREAD_COLOR)
gray_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2GRAY)
height, width, _ = rgb_image.shape
merged_image = np.zeros((height, width, 3), dtype=np.uint8)
merged_image[..., 0] = gray_image
merged_image[..., 1] = gray_image
merged_image[..., 2] = gray_image
merged_image[..., 2] = rgb_image[..., 2]
cv2.imshow("Merged Image", merged_image)
cv2.imshow("gray_image", gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()