9-3Rotation_gpu.py

“””
# 画像の回転画 use GPU
“””
import cv2
import time

def __main():
gpuImg = cv2.cuda_GpuMat()
img = cv2.imread(‘../IMG_1609.JPG’)

timeStart = time.time()
# GPUメモリへ転送
gpuImg.upload(img)
gpuImg = getGpuResize(gpuImg)
width, height = gpuImg.size()

M = cv2.getRotationMatrix2D(center=(width / 2, height / 2), angle=60, scale=0.5)
# ここで処理を実行
gpuDst = cv2.cuda.warpAffine(src=gpuImg, M=M, dsize=(width, height))
# GPUメモリから戻す
dst = gpuDst.download()

timeEnd = time.time()
print(“{0} = {1}”.format(‘GPU’, (timeEnd – timeStart) * 1000) + “/ms”)

cv2.imshow(‘Final result’, dst)
cv2.waitKey(0)

cv2.destroyAllWindows()

def getGpuResize(gpuSrc):
basePixSize = 1280 # 縦横で大きい辺の変更したいサイズ
width, height = gpuSrc.size()
largeSize = max(height, width) # 大きい方の辺のサイズ
resizeRate = basePixSize / largeSize # 変更比率を計算

gpuSrc = cv2.cuda.resize(gpuSrc, (int(width * resizeRate), int(height * resizeRate)), interpolation=None)

return gpuSrc

if __name__ == ‘__main__’:
print(cv2.__version__)

__main()