Python OpenCV图像颜色变换示例
作者:Hong_Youth 时间:2023-07-30 06:05:00
给图像添加颜色
在使用OpenCV操作图像时,有时候需要给图像添加不同的颜色,以达到不同的风格效果。这里介绍的主要是opencv中的cv.applyColorMap()
函数。
给图像应用颜色函数cv.applyColorMap(src, colormap, dst=None)
src
:表示传入的原图;colormap
:颜色图类型(17种)。可以单独使用,也可以以一个列表的形式批量使用。
以下图举例实现:
直接上代码:
# -*-coding:utf-8-*-
"""
File Name: color_operation.py
Program IDE: PyCharm
Create File By Author: Hong
"""
import cv2 as cv
import numpy as np
color_map = [
cv.COLORMAP_AUTUMN,
cv.COLORMAP_BONE,
cv.COLORMAP_JET,
cv.COLORMAP_WINTER,
cv.COLORMAP_PARULA,
cv.COLORMAP_OCEAN,
cv.COLORMAP_SUMMER,
cv.COLORMAP_SPRING,
cv.COLORMAP_COOL,
cv.COLORMAP_PINK,
cv.COLORMAP_HOT,
cv.COLORMAP_PARULA,
cv.COLORMAP_MAGMA,
cv.COLORMAP_INFERNO,
cv.COLORMAP_PLASMA,
cv.COLORMAP_TWILIGHT,
cv.COLORMAP_TWILIGHT_SHIFTED
]
def color_operation(image_path: str):
img = cv.imread(image_path, cv.IMREAD_COLOR) # 以彩色模式读图像
cv.namedWindow('input', cv.WINDOW_AUTOSIZE) # 根据图像大小自动调节窗口大小
cv.imshow('input', img)
index = 0
while True:
dst = cv.applyColorMap(img, color_map[index % len(color_map)]) # 在原图上应用不同的颜色模式
cv.imshow('{}'.format(color_map[index % len(color_map)]), dst)
index += 1
c = cv.waitKey(1000)
if c == 27:
break
cv.destroyAllWindows()
if __name__ == '__main__':
path = 'images/daiyutong.png'
color_operation(path)
效果展示:
图像按位操作
图像的位级操作主要包括:与、或、非、异或四种操作。
与:cv.bitwise_and(img1,img2)
,两幅图像按位进行与操作;或:cv.bitwise_or(img1, img2)
,两幅图像按位进行或操作;异或:cv.bitwise_xor(img1, img2)
,两幅图像按位进行异或操作;非:cv.bitwise_not(img)
,将图像按位取反操作。
具体代码如下:
# -*-coding:utf-8-*-
"""
File Name: color_operation.py
Program IDE: PyCharm
Create File By Author: Hong
"""
import cv2 as cv
import numpy as np
def bitwise_operation(image_path1: str, image_path2: str):
img1 = cv.imread(image_path1, cv.IMREAD_COLOR)
img2 = cv.imread(image_path2, cv.IMREAD_COLOR)
img2 = cv.resize(img2, (300, 300))
# img1 = np.zeros((400, 400, 3), dtype=np.uint8) # 创建一个空白图像
# img1[:, :] = (255, 0, 255) # 给所有像素的b和r通道赋值
# img2 = np.zeros((400, 400, 3), dtype=np.uint8)
# img2[:, :] = (0, 255, 0) # 给所有像素的g通道赋值
dst1 = cv.bitwise_and(img1, img2) # 图像的与操作
dst2 = cv.bitwise_or(img1, img2) # 图像的或操作
dst3 = cv.bitwise_xor(img1, img2) # 图像的异或操作
dst4 = cv.bitwise_not(img1) # 图像的非操作
cv.imshow('img1', img1)
cv.imshow('img2', img2)
cv.imshow('bitwise_and', dst1)
cv.imshow('bitwise_or', dst2)
cv.imshow('bitwise_xor', dst3)
cv.imshow('bitwise_not', dst4)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == '__main__':
path1 = 'images/daiyutong.png'
path2 = 'images/2.png'
bitwise_operation(path1, path2)
结果展示:
图像的通道操作
彩色图像由R、G、B三通道组成,我们可以分别对每个通道进行操作。主要有通道分离、通道合并、通道交换等操作。
通道分离:cv.split(img)
通道合并:cv.merge(mv)
通道交换:cv.mixChannels(src, dst, fromTo)
代码实现:
# -*-coding:utf-8-*-
"""
File Name: color_operation.py
Program IDE: PyCharm
Create File By Author: Hong
"""
import cv2 as cv
import numpy as np
def channel_operation(image_path: str):
img = cv.imread(image_path, cv.IMREAD_COLOR)
cv.namedWindow('input', cv.WINDOW_AUTOSIZE)
cv.imshow('input', img) # 彩色图像,3个通道,每个通道都是H×W。
# 通道分离
mv = cv.split(img)
print('mv[0]', mv[0]) # 图像的b通道
print('mv[1]', mv[1]) # 图像的g通道
print('mv[2]', mv[2]) # 图像的r通道
mv[0][:, :] = 255 # 给b通道上的所有像素值全部赋值为255
# 通道合并
result = cv.merge(mv)
# 通道交换
dst = np.zeros(img.shape, dtype=np.uint8)
cv.mixChannels([img], [dst], fromTo=[2, 0, 1, 1, 0, 2])
out = cv.cvtColor(img, cv.COLOR_BGR2RGB) # 与上面的通道交换bgr->rgb结果类似,
cv.imshow('bbb', img[:, :, 0]) # 显示第1个通道
cv.imshow('ggg', img[:, :, 1]) # 显示第2个通道
cv.imshow('rrr', img[:, :, 2]) # 显示第3个通道
cv.imshow('result', result)
cv.imshow('dst', dst)
cv.imshow('out', out)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == '__main__':
path = 'images/daiyutong.png'
channel_operation(path)
结果展示:
来源:https://blog.51cto.com/u_15483653/4904094
标签:Python,OpenCV,图像,颜色


猜你喜欢
python非标准时间的转换
2022-04-12 17:37:52
21行Python代码实现拼写检查器
2023-10-11 03:33:03
优化 SQL Server 索引的小技巧
2012-10-07 11:00:07
有效的提高编程技能的12个方法
2023-07-15 09:11:40

ASP中数据库调用中常见错误的现象和解决
2007-09-20 13:24:00
浅谈Python面向对象编程oop思想心得
2021-10-25 16:45:29
JavaScript lastIndexOf方法入门实例(计算指定字符在字符串中最后一次出现的位置)
2024-04-18 10:54:17
用函数模板,写一个简单高效的 JSON 查询器的方法介绍
2024-04-18 10:53:00

Vue路由模式中的hash和history模式详细介绍
2024-06-07 15:20:16

python打包压缩、读取指定目录下的指定类型文件
2021-01-19 08:23:26
vscode搭建go开发环境案例详解
2024-02-07 06:09:48

python读取json数据还原表格批量转换成html
2023-11-19 05:46:20
PHP实现设计模式中的抽象工厂模式详解
2024-06-05 09:39:58

asp IsValidEmail 验证邮箱地址函数(email)
2011-03-03 10:42:00
ASP.NET web.config中 数据库连接字符串加密解密
2024-01-14 07:02:09
2010怎么就宅了——我们是设计星球的阿凡达
2010-03-09 13:26:00

python机器学习Github已达8.9Kstars模型解释器LIME
2022-06-21 13:38:28

Vue学习之路之登录注册实例代码
2024-04-27 16:08:32

Python实现读取文件的方法总结
2021-05-04 00:56:26

python简单程序读取串口信息的方法
2024-01-02 02:42:18