python处理彩色图像通道拆分与合并

彩色图像通道拆分与合并

  • 待处理图像 ML.jpg
    请添加图片描述

1. 使用 opencv

import cv2
import matplotlib.pyplot as plt
import numpy as np
# 读取图像
# 读取图像
image = cv2.imread('ML.jpg')
plt.imshow(image)
print(type(image))  # 输出:<class 'numpy.ndarray'>
print(image.shape)  # 输出:(152, 150, 3)
b,g,r = cv2.split(image)
print(b.shape,g.shape,r.shape) # 输出:(152, 150) (152, 150) (152, 150)
# 使用matplotlib显示拆分后的通道
plt.figure(figsize=(12, 4))
plt.subplot(1,3,1),plt.imshow(b,cmap='gray'),plt.title('Blue Channel')
plt.subplot(1,3,2),plt.imshow(g,cmap='gray'),plt.title('Green Channel')
plt.subplot(1,3,3),plt.imshow(r,cmap='gray'),plt.title('Red Channel')
plt.show()

在这里插入图片描述

# 保存图像
# 保存图像
cv2.imwrite('ML_cv_B.jpg',b)
cv2.imwrite('ML_cv_G.jpg',g)
cv2.imwrite('ML_cv_R.jpg',r)
print(b.shape,g.shape,r.shape)  # 输出:(152, 150) (152, 150) (152, 150)

# 在把三张单通道图像读取进来,需要设定IMREAD_GRAYSCALE,保证以单通道读取
image_b = cv2.imread('ML_cv_B.jpg',cv2.IMREAD_GRAYSCALE)
image_g = cv2.imread('ML_cv_G.jpg',cv2.IMREAD_GRAYSCALE)
image_r = cv2.imread('ML_cv_R.jpg',cv2.IMREAD_GRAYSCALE)

# 把三个单通道图像合成一个三通道图像,也就是把3个 二维矩阵堆叠成一个三维矩阵的过程 
image_bgr = cv2.merge([image_b,image_g,image_r])
# 显示合成后的图像
print(image_bgr.shape)  # 输出:(152, 150, 3)
plt.imshow(image_bgr)

2. 使用 numpy

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

# 读取图像
image = Image.open('ML.jpg')
print(image)  # 输出:<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=150x152 at 0x1D0ED6F2850>
# 转张量
arr = np.array(image)
print(arr.shape)  # 输出:(152, 150, 3)
# 切分
r,g,b = arr[:,:,0],arr[:,:,2],arr[:,:,2]
print(r.shape,g.shape,b.shape)  #输出:(152, 150) (152, 150) (152, 150)
plt.imshow(arr)

plt.subplot(131),plt.imshow(r,cmap='gray')
plt.subplot(132),plt.imshow(g,cmap='gray')
plt.subplot(133),plt.imshow(b,cmap='gray')

在这里插入图片描述

在这里插入代码片# 将NumPy数组转换为Pillow图像  
img_r = Image.fromarray(r)  
img_g = Image.fromarray(g)  
img_b = Image.fromarray(b)  
print(img_r)  # 输出:<PIL.Image.Image image mode=L size=150x152 at 0x1D0EB524C90>
# 保存
img_r.save('ML_PIL_R.jpg')
img_g.save('ML_PIL_G.jpg')
img_b.save('ML_PIL_B.jpg')

# 再读取单通道图像
image_r = Image.open('ML_PIL_R.jpg')
image_g = Image.open('ML_PIL_G.jpg')
image_b = Image.open('ML_PIL_B.jpg')
print(image_r)  # 输出:<PIL.JpegImagePlugin.JpegImageFile image mode=L size=150x152 at 0x1D0F166FDD0>
# 转张量
R,G,B = np.array(image_r),np.array(image_g),np.array(image_b)

print(R.shape,G.shape,B.shape) # 输出:(152, 150) (152, 150) (152, 150)
# 数组堆叠,升维,变成多通道图像
RGB_Image = np.stack([R,G,B],2)
print(RGB_Image.shape)  # 输出:(152, 150, 3)
# 显示图像
plt.imshow(RGB_Image)
  • 这里有个问题,重新堆叠的图像彩色没有那么鲜艳了
    在这里插入图片描述

相关推荐

  1. python-pdf的合并

    2024-07-19 06:40:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 06:40:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 06:40:03       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 06:40:03       58 阅读
  4. Python语言-面向对象

    2024-07-19 06:40:03       69 阅读

热门阅读

  1. git-各种场景-撤销指令

    2024-07-19 06:40:03       19 阅读
  2. Stripe web 支付语言设置

    2024-07-19 06:40:03       18 阅读
  3. git-指令 -stash暂存

    2024-07-19 06:40:03       19 阅读
  4. [C/C++入门][for]25、药房管理(循环经典练习)

    2024-07-19 06:40:03       19 阅读
  5. golang 实现负载均衡器-负载均衡原理介绍

    2024-07-19 06:40:03       22 阅读
  6. pytorch的MINST数据集示例

    2024-07-19 06:40:03       17 阅读
  7. 在Ubuntu 12.04上安装和设置Postfix的方法

    2024-07-19 06:40:03       23 阅读