彩色影像通道順序是BGR
blue = img[78, 125, 0] # 藍色通道
green = img[78, 125, 1] # 綠色通道
red = img[78, 125, 2] # 紅色通道
修改畫素
img[100, 99, 0] = 255 # 修改第0個通道
img[100, 99, 1] = 255 # 修改第1個通道
img[100, 99, 2] = 255 # 修改第2個通道
img[100:150, 99:120] = [255, 0, 0] # 修改第0個通道
灰度影像
img[88, 99] = 255
-----------------------
import cv2
import numpy as np
a = cv2.imread('./test1.png')
b = np.ones((101, 101, 3))
b = a[220:400, 250:350]
a[0:180, 0:100] = b
cv2.imshow('original', a)
cv2.waitKey(0) # 保持視窗開啟,小於0等待鍵盤輸入;大於0表示等待時間
cv2.destroyAllWindows() # 銷燬視窗
-----------------------
通道拆分與合併
import cv2
import numpy as np
a = cv2.imread('./test1.png')
b = np.ones((101, 101, 3))
b = a[220:400, 250:350]
a[0:180, 0:100] = b
cv2.imshow('original', a)
b, g, r = cv2.split(a) # 拆分為bgr
b = cv2.split(a)[0] # 拆分為b
g = cv2.split(a)[1] # 拆分為g
r = cv2.split(a)[2] # 拆分為r
m = cv2.merge([b, g, r]) # 合併通道,注意通道合併的順序
cv2.imshow('merge', m)
cv2.waitKey(0) # 保持視窗開啟,小於0等待鍵盤輸入;大於0表示等待時間
cv2.destroyAllWindows() # 銷燬視窗
----------------------------
圖片畫素相加
import cv2
import numpy as np
a = cv2.imread('./test1.png')
b = a
add1 = a + b # 相加之後對255取餘
add2 = cv2.add(a, b) # 相加之後最大取255
cv2.imshow("add1", add1)
cv2.imshow("add2", add2)
cv2.imshow('original', a)
cv2.waitKey(0) # 保持視窗開啟,小於0等待鍵盤輸入;大於0表示等待時間
cv2.destroyAllWindows() # 銷燬視窗
----------------------------
影像融合
結果影像 = 影像1 * 係數1 + 影像2 * 係數2 + 亮度調節量
dst = cv2.addWeighted(src1, alpha, src2, beta, gamma)
src1: 影像1
alpha: 係數1
src2: 影像2
beta: 係數2
gamma: 亮度調節量
a = cv2.imread('./test1.png')
a.shape
a = a[0:397, 0:596, :]
b = cv2.imread('./test2.jpeg')
b.shape
result = cv2.addWeighted(a, 0.3, b, 0.7, 0)
cv2.imshow("a", a)
cv2.imshow("b", b)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
----------------------------
----------------------------
影像轉換
import cv2
import numpy as np
a = cv2.imread('./test1.png', cv2.IMREAD_UNCHANGED)
b = cv2.cvtColor(a, cv2.COLOR_GRAY2BGR)
cv2.imshow('lena', a)
cv2.imshow('RGB', b)
b = cv2.cvtColor(a, cv2.COLOR_BGR2RGB)
bb, bg, br = cv2.split(a)
cv2.imshow("bb", bb)
cv2.imshow("bg", bg)
cv2.imshow("br", br)
cv2.waitKey(0)
cv2.destroyAllWindows()
----------------------------
影像翻轉
dst = cv2.flip(src, flipCode) flipCode=0沿x軸翻轉,flipCode=1沿y軸翻轉,flipCode=-1先沿x軸翻轉再沿y軸翻轉,
import cv2
a = cv2.imread('./test1.png')
b = cv2.flip(a, 0)
cv2.imshow('original', a)
cv2.imshow('flip', b)
cv2.waitKey(0)
cv2.destroyAllWindows()
----------------------------
閾值分割
retval, dst = cv2.threshold(src, thresh, maxval, type)
retval: 閾值
dst: 處理結果
src: 源影像
threshold: 閾值
maxval: 最大值
type: 型別 # 二進位制閾值化cv2.THRESH_BINARY # 反二進位制閾值化 # 閾值化 # 反閾值化為0 cv2.THRESH_TOZERO_INV
import cv2
o = cv2.imread('./lena512.png', cv2.IMREAD_UNCHANGED) # 讀取灰度圖
r, b = cv2.threshold(o, 128, 255, cv2.THRESH_BINARY) # 二值化
r2, b2 = cv2.threshold(o, 128, 255, cv2.THRESH_BINARY_INV) # 反二值化
r, b = cv2.threshold(o, 128, 255, cv2.THRESH_TRUNC) # 截斷
r, b = cv2.threshold(o, 128, 255, cv2.THRESH_TOZERO) # 閾值化為0
r, b = cv2.threshold(o, 128, 255, cv2.THRESH_TOZERO_INV) # 反閾值化為0
cv2.imshow('original', o)
cv2.imshow('result', b)
cv2.imshow('result2', b2)
cv2.waitKey(0)
cv2.destroyAllWindows()