显示彩色图像的RGB通道

import cv2
import numpy as np
import matplotlib.pyplot as plt

def show_channel(image, channel_index, channel_name, subplot_index):
    plt.subplot(2, 2, subplot_index)
    channel = image[:,:,channel_index]
    plt.imshow(channel, cmap='gray')
    plt.title(f'{channel_name} Channel')
    plt.colorbar()



# 读取图像
image_path = 'C:/xx/1/1.jpg'
original_image = cv2.imread(image_path)

# 创建一个2行2列的画布,用于显示原图和R、G、B通道图像
plt.figure(figsize=(10, 8))

# 显示原图
plt.subplot(2, 2, 1)   #  图的行列数要与上述: plt.subplot(2, 2, subplot_index)  对应
plt.imshow(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.colorbar()


# 显示R、G、B通道图像
show_channel(original_image, 0, 'R', 2)
show_channel(original_image, 1, 'G', 3)
show_channel(original_image, 2, 'B', 4)

# 调整布局,防止重叠
plt.tight_layout()
plt.show()


你可能感兴趣的:(python,开发语言)