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

彩色图像通道拆分与合并

  • [1. 使用 opencv](#1. 使用 opencv)

  • [2. 使用 numpy](#2. 使用 numpy)

  • 待处理图像 ML.jpg

1. 使用 opencv

python 复制代码
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()
python 复制代码
# 保存图像
# 保存图像
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

python 复制代码
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')
python 复制代码
在这里插入代码片# 将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)
  • 这里有个问题,重新堆叠的图像彩色没有那么鲜艳了
相关推荐
无巧不成书021818 小时前
最新 PyPI可信发布完整教程 GitHub Actions+OIDC零Token实现Python包自动化发布
python·自动化·github·pypi
星恒随风20 小时前
C++11 加餐(2)并发支持库——mutex、lock_guard、unique_lock 与死锁问题
开发语言·c++
东莞市云毅网络有限公司1 天前
用 Node 做收录快照的周对比:差集、阈值与告警
python·sqlite·自动化运维·geo·数据监测
山楂树の1 天前
Python Pandas 数据处理:数据读取、筛选、清洗、排序与分组
开发语言·python·pandas
Logintern091 天前
langgraph 图编译的聊天机器人
python·langgraph
xurileidian1 天前
第 22 篇 · 边玩边学Python · 双等号与 is 不是一回事
开发语言·python·spring
FRing031 天前
for循环、while循环、Java高级语法、数组
java·开发语言·后端·算法
syf0611131 天前
Java Math 类详解:常用数学运算方法全解析
java·开发语言
是main不是漫1 天前
【双指针 | LeetCode11 盛最多水的容器】O(n²) 超时,O(n) 封神:这道“装水题“只差一个思路
开发语言·数据结构·c++·算法
钱栈up1 天前
GitHub热榜实测|Sherlock:一个用户名搜遍全网
python·开源·github