OpenCV計算機視覺實戰(Python版)
資料讀取-圖像
cv2.IMREAD_COLOR:彩色圖像
cv2.IMREAD_GRAYSCALE:灰階圖像
import cv2 #opencv讀取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
img=cv2.imread('cat.jpg')
img
array([[[142, 151, 160],
[146, 155, 164],
[151, 160, 170],
...,
[183, 198, 200],
[128, 143, 145],
[127, 142, 144]]], dtype=uint8)
圖像的顯示,也可以建立多個視窗(imshow第一個參數表示打開視窗的名稱,第二個參數是檔案名)
cv2.imshow('image',img)
# 等待時間,毫秒級,0表示任意鍵終止
cv2.waitKey(0)
cv2.destroyAllWindows()
定義一個打開圖檔的函數
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img.shape
#圖像的顯示,也可以建立多個視窗
cv2.imshow('image',img)
# 等待時間,毫秒級,0表示任意鍵終止
cv2.waitKey(10000)
cv2.destroyAllWindows()
儲存圖像
#儲存
cv2.imwrite('mycat.png',img)
numpy.ndarray
像素點總和
img.size
圖像資料的類型
img.dtype
資料讀取-視訊
cv2.VideoCapture可以捕獲攝像頭,用數字來控制不同的裝置,例如0,1。
如果是視訊檔案,直接指定好路徑即可。
# 檢查是否打開正确
if vc.isOpened():
oepn, frame = vc.read()
else:
open = False
while open:
ret, frame = vc.read()
if frame is None:
break
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('result', gray)
if cv2.waitKey(100) & 0xFF == 27:
break
vc.release()
cv2.destroyAllWindows()
截取部分圖像資料
img=cv2.imread('cat.jpg')
cat=img[0:50,0:200]
cv_show('cat',cat)
顔色通道提取
r
array([[160, 164, 170, ..., 185, 184, 183],
[126, 131, 137, ..., 184, 183, 182],
[127, 131, 138, ..., 183, 182, 181],
...,
[198, 193, 178, ..., 206, 195, 174],
[176, 183, 175, ..., 188, 144, 125],
[190, 190, 157, ..., 200, 145, 144]], dtype=uint8)
r.shape
merge(注意opencv顔色通道的順序)
img=cv2.merge((b,g,r))
img.shape
隻保留R
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,1] = 0
cv_show('R',cur_img)
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,2] = 0
cv_show('G',cur_img)
# 隻保留B
cur_img = img.copy()
cur_img[:,:,1] = 0
cur_img[:,:,2] = 0
cv_show('B',cur_img)
邊界填充
top_size,bottom_size,left_size,right_size = (50,50,50,50)
replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv2.BORDER_CONSTANT, value=255)
import matplotlib.pyplot as plt
plt.subplot(231), plt.imshow(img, 'gray'), plt.title('ORIGINAL')
plt.subplot(232), plt.imshow(replicate, 'gray'), plt.title('REPLICATE')
plt.subplot(233), plt.imshow(reflect, 'gray'), plt.title('REFLECT')
plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title('REFLECT_101')
plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('WRAP')
plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('CONSTANT')
plt.show()
BORDER_REPLICATE:複制法,也就是複制最邊緣像素。
BORDER_REFLECT:反射法,對感興趣的圖像中的像素在兩邊進行複制例如:fedcba|abcdefgh|hgfedcb
BORDER_REFLECT_101:反射法,也就是以最邊緣像素為軸,對稱,gfedcb|abcdefgh|gfedcba
BORDER_WRAP:外包裝法cdefgh|abcdefgh|abcdefg
BORDER_CONSTANT:常量法,常數值填充。
數值計算
mg_cat2= img_cat +10
img_cat[:5,:,0]
rray([[142, 146, 151, ..., 156, 155, 154],
[108, 112, 118, ..., 155, 154, 153],
[108, 110, 118, ..., 156, 155, 154],
[139, 141, 148, ..., 156, 155, 154],
[153, 156, 163, ..., 160, 159, 158]], dtype=uint8)
array([[152, 156, 161, ..., 166, 165, 164],
[118, 122, 128, ..., 165, 164, 163],
[118, 120, 128, ..., 166, 165, 164],
[149, 151, 158, ..., 166, 165, 164],
[163, 166, 173, ..., 170, 169, 168]], dtype=uint8)
相當于% 256
#相當于% 256
(img_cat + img_cat2)[:5,:,0]
array([[ 38, 46, 56, ..., 66, 64, 62],
[226, 234, 246, ..., 64, 62, 60],
[226, 230, 246, ..., 66, 64, 62],
[ 32, 36, 50, ..., 66, 64, 62],
[ 60, 66, 80, ..., 74, 72, 70]], dtype=uint8)
使用cv2.add()将兩個圖像相加,可以使用numpy中的矩陣加法來實作,飽和操作
array([[255, 255, 255, ..., 255, 255, 255],
[226, 234, 246, ..., 255, 255, 255],
[226, 230, 246, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255]], dtype=uint8)
圖像融合
img_cat + img_dog
size必須一緻
注意
#在OpenCV中,img.shape[0]得到的是圖檔的高,img.shape[1]得到是圖檔的寬,
#可是在cv2.resize(img, (dimension[0], dimension[1]))函數裡,dimension[0]卻是新圖檔 的寬,dimension[1]是新圖
#在OpenCV中,img.shape[0]得到的是圖檔的高,img.shape[1]得到是圖檔的寬,
#可是在cv2.resize(img, (dimension[0], dimension[1]))函數裡,dimension[0]卻是新圖檔 的寬,dimension[1]是新圖檔的高,有點奇葩哈
img_dog = cv2.resize(img_dog, (500, 414))
img_dog.shape
res = cv2.resize(img, (0, 0), fx=4, fy=4)
plt.imshow(res)
res = cv2.resize(img, (0, 0), fx=1, fy=3)
plt.imshow(res)