影像處理筆記總目錄
1 影像縮放
縮放是對影像的大小進行調整,即使影像放大或縮小,
API:cv2.resize(src,dsize,fx=0,fy=0,interpolation=cv2.INTER_LINEAR)
引數:
- src : 輸入影像
- dsize: 絕對尺寸,直接指定調整后影像的大小
- fx,fy: 相對尺寸,將dsize設定為None,然后將fx和fy設定為比例因子即可
- interpolation:插值方法
| 插值 | 含義 |
|---|---|
| cv2.INTER_LINEAR | 雙線性插值法 |
| cv2.INTER_NEAREST | 最近鄰插值 |
| cv2.INTER_AREA | 像素區域重采樣(默認) |
| cv2.INTER_CUBIC | 雙三次插值 |
示例:
import cv2.cv2 as cv
# 1. 讀取圖片
img1 = cv.imread("./image/dog.jpeg")
# 2.影像縮放
# 2.1 絕對尺寸
rows,cols = img1.shape[:2]
res = cv.resize(img1,(2*cols,2*rows),interpolation=cv.INTER_CUBIC)
# 2.2 相對尺寸
res1 = cv.resize(img1,None,fx=0.5,fy=0.5)
# 3 影像顯示
# 3.1 使用opencv顯示影像(不推薦)
cv.imshow("orignal",img1)
cv.imshow("enlarge",res)
cv.imshow("shrink)",res1)
cv.waitKey(0)
# 3.2 使用matplotlib顯示影像
fig,axes=plt.subplots(nrows=1,ncols=3,figsize=(10,8),dpi=100)
axes[0].imshow(res[:,:,::-1])
axes[0].set_title("絕對尺度(放大)")
axes[1].imshow(img1[:,:,::-1])
axes[1].set_title("原圖")
axes[2].imshow(res1[:,:,::-1])
axes[2].set_title("相對尺度(縮小)")
plt.show()

2 影像平移
影像平移將影像按照指定方向和距離,移動到相應的位置,
API:cv.warpAffine(img,M,dsize)
引數:
- img:輸入影像
- M:2
?
*
? 3移動矩陣
對于 ( x , y ) (x,y) (x,y)處的像素點,要把它移動到 ( x + t x , y + t y ) (x+t_x , y+t_y) (x+tx?,y+ty?) 處時,M矩陣應如下設定:
M = [ 1 0 t x 0 1 t y ] M=\left[\begin{array}{lll} 1 & 0 & t_{x} \\ 0 & 1 & t_{y} \end{array}\right] M=[10?01?tx?ty??]
注意:將M設定為np.float32型別的Numpy陣列, - dsize: 輸出影像的大小
注意:輸出影像的大小,它應該是(寬度,高度)的形式,請記住,width=列數,height=行數,
示例:需求是將影像的像素點移動(50,100)的距離
import numpy as np
import cv2.cv2 as cv
import matplotlib.pyplot as plt
# 1. 讀取影像
img1 = cv.imread("./image/image2.jpg")
# 2. 影像平移
rows,cols = img1.shape[:2]
M = M = np.float32([[1,0,100],[0,1,50]])# 平移矩陣
dst = cv.warpAffine(img1,M,(cols,rows))
# 3. 影像顯示
fig,axes=plt.subplots(nrows=1,ncols=2,figsize=(10,8),dpi=100)
axes[0].imshow(img1[:,:,::-1])
axes[0].set_title("原圖")
axes[1].imshow(dst[:,:,::-1])
axes[1].set_title("平移后結果")
plt.show()

3 影像旋轉
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/350746.html
標籤:其他
