Python-OpenCV 笔记5 -- 几何变换(Geometric Transformations)
生活随笔
收集整理的這篇文章主要介紹了
Python-OpenCV 笔记5 -- 几何变换(Geometric Transformations)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Python-OpenCV 筆記5 – 幾何變換(Geometric Transformations)
1、縮放(Scaling)
# 函數(shù)原型 cv.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])# 實(shí)例 cv2.resize(img,None,fx=2, fy=2)height, width = img.shape[:2] res = cv2.resize(img,(2*width, 2*height))- dsize:輸出圖片的尺寸
- fx:x軸縮放因子
- fy:x軸縮放因子
- interpolation:插值方法
2、平移(Translation)
# 實(shí)例,x軸平移100,y軸平移50 rows, cols = img.shape M = np.float32([[1,0,100],[0,1,50]]) dst = cv2.warpAffine(img,M,(cols,rows))3、旋轉(zhuǎn)(Rotation)
# 函數(shù)原型 getRotationMatrix2D(center, angle, scale)# 實(shí)例 rows,cols = img.shape # 注意減1 M = cv.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0), 90, 1) dst = cv.warpAffine(img,M,(cols,rows))- center:旋轉(zhuǎn)中心
- angle: 旋轉(zhuǎn)角度(°),正值代表順時(shí)針(默認(rèn)為左上角).
- scale:縮放比例
4、仿射變換(Affine Transformation)
# 函數(shù)原型 cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]] )# 實(shí)例 rows,cols,ch = img.shape pts1 = np.float32([[50,50],[200,50],[50,200]]) pts2 = np.float32([[10,100],[200,50],[100,250]]) # 獲取變換矩陣 M = cv2.getAffineTransform(pts1, pts2) # 進(jìn)行仿射變換 dst = cv2.warpAffine(img, M, (cols,rows))- M: 3×3 變換矩陣
- dsize:輸出圖片的尺寸
5、透視變換(Perspective Transformation)
# 函數(shù)原型 warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])# 實(shí)例 pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]]) pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]]) M = cv2.getPerspectiveTransform(pts1,pts2) dst = cv2.warpPerspective(img,M,(300,300))總結(jié)
以上是生活随笔為你收集整理的Python-OpenCV 笔记5 -- 几何变换(Geometric Transformations)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: django mongodb mysql
- 下一篇: python 猴子补丁_python面试