利用 `OpenCV` 和 `Matplotlib` 库进行图像读取、颜色空间转换、掩膜创建、颜色替换

这段代码主要涉及图像处理任务,利用 OpenCVMatplotlib 库进行图像读取、颜色空间转换、掩膜创建、颜色替换等操作。下面是对代码的逐部分解释和归类:

1. 导入库

python 复制代码
import cv2
import matplotlib.pyplot as plt
import numpy as np
  • cv2: OpenCV库,用于图像处理。
  • matplotlib.pyplot: 用于绘制和显示图像。
  • numpy: 用于处理数组,尤其是在图像数据处理中的数值操作。

2. 读取和转换图像颜色空间

python 复制代码
image = cv2.imread('E:/insulator/1/1/picture/lion/4.png')
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  • cv2.imread(): 读取图像。
  • cv2.cvtColor(): 将图像从 BGR 颜色空间转换为 HSV 颜色空间(色调、饱和度、明度),这有助于根据颜色值进行更准确的处理。

3. 创建颜色掩膜

python 复制代码
lower_color = np.array([127, 29, 29])    # 红色低范围
upper_color = np.array([255, 255, 246])   # 红色高范围
mask = cv2.inRange(image, lower_color, upper_color)
  • 使用 cv2.inRange() 创建一个掩膜,只保留在 lower_colorupper_color 范围内的颜色(即红色范围)。

4. 应用掩膜并更改颜色

python 复制代码
new_color = np.array([156, 163, 175], dtype=np.uint8)
background = np.array([255, 255, 255], dtype=np.uint8)
colored_image = np.zeros_like(image)
colored_image[mask > 0] = new_color
colored_image[mask == 0] = background
  • 创建一个新的空白图像 colored_image,然后根据掩膜将红色区域替换为 new_color,背景区域设置为白色。

5. 保存结果

python 复制代码
cv2.imwrite('E:/insulator/1/1/picture/lion/colored_image.png', colored_image)
  • 使用 cv2.imwrite() 将处理后的图像保存为 colored_image.png

6. 操作与图像分析

python 复制代码
white_mask = cv2.inRange(image1, (204, 40, 52), (229, 5, 8))
image1[white_mask] = (0, 150, 105)
  • 创建白色区域的掩膜,将掩膜区域的颜色更改为指定的绿色 (0, 150, 105)

7. 处理图像中的像素值

python 复制代码
image1[image1 < 255] = 25
image1[image1 > 11] = (255, 0, 0)
  • 通过条件操作修改像素值:对于像素值小于 255 的区域,将其设为 25;对于像素值大于 11 的区域,将其设为蓝色 (255, 0, 0)

8. 平均化图像(通道降维)

python 复制代码
image2 = image2.mean(axis=2)
  • 将图像 image2 的通道降维,转为灰度图(通过取平均值)。

9. 根据像素值进行条件替换

python 复制代码
image1[np.logical_and(image3 > 222, image2 > 0)] = (0, 255, 0)
  • 使用 np.logical_and() 结合多个条件判断,修改符合条件的像素的颜色。

10. 更多条件操作与图像显示

python 复制代码
image1[np.logical_and(image3 > 222, image2 > 0)] = (0, 255, 0)
image[np.logical_and(image1 > 46, image1 < 88)] = (255, 0, 0)
  • 根据多个条件修改图像的像素值,修改符合条件的像素为指定颜色。

11. 噪声添加与二值化

python 复制代码
noise = np.random.rand(*image.shape)
thres = 0.3
image[noise > thres] = 255
image[image < 240] = 0
image[image >= 240] = 1
  • 使用随机噪声 np.random.rand() 修改图像的像素值,并根据阈值将图像转为二值图像(0 或 1)。

12. 根据掩膜对新图像进行处理

python 复制代码
jueyuanzi_mask = cv2.imread("./jueyuanzi_mask.png")
new_image[np.logical_and(jueyuanzi_mask != 255, image != 1)] = (185, 28, 28)
chuanshuxian_mask = cv2.imread("./chuanshuxian_mask.png")
new_image[np.logical_and(chuanshuxian_mask != 255, image != 1)] = (255, 255, 255)
  • 读取掩膜图像(jueyuanzi_maskchuanshuxian_mask),根据掩膜的条件修改图像 new_image 的颜色。

13. 保存最终图像

python 复制代码
new_image = cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR)
cv2.imwrite("new_image.png", new_image)
  • 将最终图像 new_image 转换为 BGR 颜色空间并保存为 new_image.png

总结

这段代码涉及图像处理中的几个常见任务:

  • 颜色空间转换:将图像从 BGR 转换为 HSV。
  • 掩膜创建与颜色替换:通过颜色范围提取特定区域,并替换为新颜色。
  • 图像的条件修改:根据像素值或掩膜条件改变图像区域的颜色。
  • 噪声添加与图像二值化:模拟噪声并进行图像二值化处理。
  • 图像合成与保存:通过多个操作合成新的图像,并最终保存。

这些操作可用于目标检测、图像分割、区域标记等应用,尤其在图像处理中用于特定颜色的提取与修改。

相关推荐
多恩Stone23 分钟前
【3DV 进阶-5】3D生成中 Inductive Bias (归纳偏置)的技术路线图
人工智能·python·算法·3d·aigc
HaiLang_IT24 分钟前
2026 人工智能与大数据专业毕业论文选题方向及题目示例(nlp/自然语言处理/图像处理)
大数据·人工智能·毕业设计选题
minhuan30 分钟前
构建AI智能体:八十二、潜藏秩序的发现:隐因子视角下的SVD推荐知识提取与机理阐释
人工智能·svd推荐·隐因子·推荐模型
Brianna Home1 小时前
大模型如何变身金融风控专家
人工智能·深度学习·机器学习·自然语言处理·stable diffusion·1024程序员节
HPC_C1 小时前
Efficient Memory Management for Large Language Model with PagedAttention
人工智能·语言模型·自然语言处理
维维180-3121-14551 小时前
ChatGPT-4o在自然科学中的应用:统计建模、机器学习与时空数据分析实战
人工智能·生态·环境·气象·农业
后端小肥肠1 小时前
从 Coze 到 n8n:我用 n8n 实现了10w+小林漫画的爆款流水线生产
人工智能·aigc·agent
MDLZH2 小时前
WSL实践二
人工智能·pytorch·深度学习
oliveray2 小时前
ATPrompt:基于属性的视觉提示
人工智能·prompt·vlm