Python OpenCV圖像顏色變換示例
給圖像添加顏色
在使用OpenCV操作圖像時(shí),有時(shí)候需要給圖像添加不同的顏色,以達(dá)到不同的風(fēng)格效果。這里介紹的主要是opencv中的cv.applyColorMap()函數(shù)。
給圖像應(yīng)用顏色函數(shù)cv.applyColorMap(src, colormap, dst=None)src:表示傳入的原圖;colormap:顏色圖類型(17種)??梢詥为?dú)使用,也可以以一個(gè)列表的形式批量使用。
以下圖舉例實(shí)現(xiàn):

直接上代碼:
# -*-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) # 根據(jù)圖像大小自動(dòng)調(diào)節(jié)窗口大小
cv.imshow('input', img)
index = 0
while True:
dst = cv.applyColorMap(img, color_map[index % len(color_map)]) # 在原圖上應(yīng)用不同的顏色模式
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)
效果展示:

圖像按位操作
圖像的位級(jí)操作主要包括:與、或、非、異或四種操作。
與:cv.bitwise_and(img1,img2),兩幅圖像按位進(jìn)行與操作;或:cv.bitwise_or(img1, img2),兩幅圖像按位進(jìn)行或操作;異或:cv.bitwise_xor(img1, img2),兩幅圖像按位進(jìn)行異或操作;非: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) # 創(chuàng)建一個(gè)空白圖像
# 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)
結(jié)果展示:

圖像的通道操作
彩色圖像由R、G、B三通道組成,我們可以分別對(duì)每個(gè)通道進(jìn)行操作。主要有通道分離、通道合并、通道交換等操作。
通道分離:cv.split(img)通道合并:cv.merge(mv)通道交換:cv.mixChannels(src, dst, fromTo)
代碼實(shí)現(xiàn):
# -*-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個(gè)通道,每個(gè)通道都是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結(jié)果類似,
cv.imshow('bbb', img[:, :, 0]) # 顯示第1個(gè)通道
cv.imshow('ggg', img[:, :, 1]) # 顯示第2個(gè)通道
cv.imshow('rrr', img[:, :, 2]) # 顯示第3個(gè)通道
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)
結(jié)果展示:

到此這篇關(guān)于Python OpenCV圖像顏色變換示例的文章就介紹到這了,更多相關(guān)Python OpenCV圖像顏色內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)數(shù)據(jù)分析與建模
這篇文章主要介紹了python實(shí)現(xiàn)數(shù)據(jù)分析與建模功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
Python中時(shí)間datetime的處理與轉(zhuǎn)換用法總結(jié)
今天小編就為大家分享一篇關(guān)于Python中時(shí)間datetime的處理與轉(zhuǎn)換用法總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
pandas數(shù)據(jù)處理之 標(biāo)簽列字符轉(zhuǎn)數(shù)字的實(shí)現(xiàn)
這篇文章主要介紹了pandas數(shù)據(jù)處理之 標(biāo)簽列字符轉(zhuǎn)數(shù)字的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Python中time模塊與datetime模塊在使用中的不同之處
這篇文章主要介紹了Python中time模塊與datetime模塊在使用中的不同之處,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-11-11
如何將自己的python庫(kù)打包成wheel文件并上傳到pypi
這篇文章主要介紹了如何將自己的python庫(kù)打包成wheel文件并上傳到pypi,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
pytorch查看通道數(shù) 維數(shù) 尺寸大小方式
這篇文章主要介紹了pytorch查看通道數(shù) 維數(shù) 尺寸大小方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
深度學(xué)習(xí)Tensorflow?2.4?完成遷移學(xué)習(xí)和模型微調(diào)
這篇文章主要為大家介紹了深度學(xué)習(xí)Tensorflow?2.4?完成遷移學(xué)習(xí)和模型微調(diào),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

