import numpy as np
import cv2 as cv
import matplotlib.pyplot as pltimg = cv.imread("../SampleImages/pomeranian.png", cv.IMREAD_COLOR)
plt.imshow(img[:,:,::-1])#像素操作
pixel = img[320,370]
print(pixel)#只獲取藍色通道的值
pixel_blue = img[320,370,0]
print(pixel_blue)#只獲取綠色通道的值
pixel_green = img[320,370,1]
print(pixel_green)#只獲取紅色通道的值
pixel_red = img[320,370,2]
print(pixel_red)#修改像素顏色
#索引的第一個數是行數,第二個數是列數
img[320,370] = [0,0,255]
img[321,370] = [0,0,255]
img[322,370] = [0,0,255]
plt.imshow(img[:,:,::-1])#獲取圖像屬性
#圖像形狀
print(img.shape)
#圖像大小
print(img.size)
#圖像數據類型
print(img.dtype)#圖像通道的拆分
b,g,r = cv.split(img)
print(b[320,370])
print(g[320,370])
print(r[320,370])#圖像通道的合并,恢復被修改的像素各個通道的顏色后進行合并
b[320,370] = 25
g[320,370] = 30
r[320,370] = 41
b[321,370] = 25
g[321,370] = 30
r[321,370] = 41
b[322,370] = 25
g[322,370] = 30
r[322,370] = 41
img = cv.merge((b,g,r))
plt.imshow(img[:,:,::-1])#顏色空間轉換
#cv.cvtColor(image, flag)
# flag: cv.COLOR_BGR2GRAY, cv.COLOR_BGR2HSV等等
#參考資料:https://blog.csdn.net/guduruyu/article/details/68941554
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
plt.imshow(img_gray, cmap=plt.cm.gray)img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
plt.imshow(img_hsv, cmap=plt.cm.hsv)
?
?
?
?
?
?
?
?
?