1. 程式人生 > >opencv-api getRotationMatrix2D & warpAffine

opencv-api getRotationMatrix2D & warpAffine

1.getRotationMatrix2D

getRotationMatrix2D(center, angle, scale) -> retval

引數 描述
center 旋轉的中心點,一般是圖片的中心,用img.shape取得長寬,然後取一半
angle 旋轉的角度,正值是逆時針旋轉,負值時順時針旋轉
scale 縮放因子

2.warpAffine

warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) -> dst

引數 描述
src 輸入影象
M 2*3的變換矩陣,由getRotationMatrix2D獲得
dsize 輸出圖片的大小
dst 輸出影象,預設為None
flags 插值的方法
borderMode 邊界模式,預設為None
borderValue 邊界值,預設為None

import cv2 as cv
img = cv.imread("./data/minAreaRect.png",0)

# 這裡型別做了轉換 要注意
h, w = img.shape[:2]
center = (w//2, h//2)
angle=-40
M = cv.getRotationMatrix2D(center, angle, 1.0)


rotated = cv.warpAffine(img, M, (w, h), flags=cv.INTER_CUBIC, borderMode=cv.BORDER_REPLICATE)
cv.imshow('Input', img)
cv.imshow('Rotated', rotated)
k = cv.waitKey(0)  # 無限等待一個鍵擊,將此鍵擊存在k變數中
if k == 27:         # 27代表esc,可以檢視ascii碼錶
    cv.destroyAllWindows()  # 退出視窗

在這裡插入圖片描述