Python项目实战:将3D灰度图像转换为3D彩色图像

文章目录

  • (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

# 加载16位灰度图像
image_data = tifffile.imread('gray_image_raw.tif')        # 原始-灰度图像(16-bit): 50, 444, 1112

# 将图像的数据类型转换为32位整数,以便进行操作
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  # 蓝色通道
###########################################################################
# (image_data >> 8) & 0xFF
# 		操作:通过右移8位(相当于除以256)来实现的,然后使用位运算AND操作符&和掩码0xFF来保留最低8位。
# 		作用:将16位图像的灰度范围缩放到0-255,适合于8位彩色图像的显示或处理。
###########################################################################

# 将彩色图像保存为新的tif文件
tifffile.imwrite('color_image.tif', color_image)

(2)channel叠加:在RGB的R图中,将【灰度图】与【R图】进行叠加,显示红色。

import numpy as np
import tifffile

# image_data与image_data_gray的图像尺度必须一致
gray_image_raw = tifffile.imread('gray_image_raw.tif')        	# 原始-灰度图像(16-bit):[50, 444, 1112]
gray_image_circle = tifffile.imread('gray_image_circle.tif')	# 空心圆-灰度图像(16-bit):[50, 444, 1112]

color_image = np.zeros((*gray_image_raw.shape, 3))          	# 空心圆-彩色图像(16-bit):[50, 444, 1112, 3]
# *gray_image_raw.shape表示将gray_image_raw.shape中的元素展开,并将它们作为参数传递给函数或操作。
color_image[..., 0] = gray_image_raw
color_image[..., 1] = gray_image_raw
color_image[..., 2] = gray_image_raw

# 将gray_image_circle中65535的值(空心圆=65535)赋给color_image
color_image[gray_image_circle == 65535, 0] = 65535              # 65535表示灰度图像中的最大像素值,0表示红色通道。
color_image = color_image.astype(gray_image_raw.dtype)          # 将数据类型转换为与image_data一样
tifffile.imwrite('color_image.tif', color_image)
print(color_image.shape[0], color_image.shape[1], color_image.shape[2], color_image.shape[3])       
# [50, 444, 1112, 3]

(3)channel拼接:(10x12x14)x1+(10x12x14)x3=(10x12x14)x4

import numpy as np
import tifffile

# image_data与image_data_gray的图像尺度必须一致
gray_image_raw = tifffile.imread('gray_image_raw.tif')        # 原始-灰度图像(16-bit): [50, 444, 1112]
gray_image_circle = tifffile.imread('gray_image.tif')			# 空心圆-灰度图像(16-bit): [50, 444, 1112]

color_image = np.zeros((*gray_image_raw.shape, 3))          	# 空心圆-彩色图像(16-bit): [50, 444, 1112, 3]
# *gray_image_raw.shape表示将gray_image_raw.shape中的元素展开,并将它们作为参数传递给函数或操作。
color_image[..., 0] = gray_image_raw
color_image[..., 1] = gray_image_raw
color_image[..., 2] = gray_image_raw

# 将gray_image_circle中65535的值(空心圆=65535)赋给color_image
color_image[gray_image_circle == 65535, 0] = 65535              # 65535表示灰度图像中的最大像素值,0表示红色通道。
color_image = color_image.astype(gray_image_raw.dtype)          # 将数据类型转换为与image_data一样

# 将灰度图像和调整后的 RGB 图像进行合并
gray_image_raw = np.expand_dims(gray_image_raw, axis=-1)
merged_image = np.concatenate((gray_image_raw, color_image), axis=3)

# 将彩色图像保存为新的tif文件
tifffile.imwrite('color_image.tif', merged_image)
print(merged_image.shape[0], merged_image.shape[1], merged_image.shape[2], merged_image.shape[3])		
# [50, 444, 1112, 4]

二、二维数组:10x10

(1)channel叠加:10x12x1+10x12x3=10x12x3

Python项目实战:将3D灰度图像转换为3D彩色图像_第1张图片

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()

你可能感兴趣的:(深度学习,3D可视化,python,图像处理,可视化)