利用 `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。
  • 掩膜创建与颜色替换:通过颜色范围提取特定区域,并替换为新颜色。
  • 图像的条件修改:根据像素值或掩膜条件改变图像区域的颜色。
  • 噪声添加与图像二值化:模拟噪声并进行图像二值化处理。
  • 图像合成与保存:通过多个操作合成新的图像,并最终保存。

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

相关推荐
taoqick2 小时前
对PosWiseFFN的改进: MoE、PKM、UltraMem
人工智能·pytorch·深度学习
suibian52352 小时前
AI时代:前端开发的职业发展路径拓宽
前端·人工智能
预测模型的开发与应用研究3 小时前
数据分析的AI+流程(个人经验)
人工智能·数据挖掘·数据分析
源大模型3 小时前
OS-Genesis:基于逆向任务合成的 GUI 代理轨迹自动化生成
人工智能·gpt·智能体
PowerBI学谦4 小时前
Python in Excel高级分析:一键RFM分析
大数据·人工智能·pandas
运维开发王义杰5 小时前
AI: Unsloth + Llama 3 微调实践,基于Colab
人工智能·llama
文心快码 Baidu Comate5 小时前
文心快码|AI重构开发新范式,从工具到人机协同
人工智能·ai编程·文心快码·智能编程助手·全栈编程
WHATEVER_LEO6 小时前
【每日论文】Latent Radiance Fields with 3D-aware 2D Representations
人工智能·深度学习·神经网络·机器学习·计算机视觉·自然语言处理
MYT_flyflyfly6 小时前
计算机视觉-OpenCV图像处理
图像处理·opencv·计算机视觉
浮华落定6 小时前
DeepSeek+即梦 做AI视频
人工智能·chatgpt·音视频