9-4Perspective_gpu.py

000: import cv2
001: import numpy as np
002:
003: def __main():
004: gpuImg = cv2.cuda_GpuMat()
005: img = cv2.imread(‘20200905_163733.jpg’)
006:
007: # マス目四隅の座標を登録
008: pts1 = np.float32([[388, 174], [943, 170], [243, 717], [1068, 720]])
009: # 変換後のマス目四隅の座標を登録
010: pts2 = np.float32([[100, 100], [720, 100], [100, 960], [720, 960]])
011:
012: # GPUメモリへ転送
013: gpuImg.upload(img)
014: timeStart = time.time()
015: gpuImg = getGpuResize(gpuImg)
016: width, height = gpuImg.size()
017:
018: M = cv2.getPerspectiveTransform(src=pts1, dst=pts2)
019: # ここで処理を実行
020: gpuDst = cv2.cuda.warpPerspective(src=gpuImg, M=M, dsize=(width, height))
021:
022: # GPUメモリから戻す
023: dst = gpuDst.download()
024: cv2.imshow(‘Final result’, dst)
025: cv2.waitKey(0)
026: cv2.destroyAllWindows()
027:
028: def getGpuResize(gpuSrc):
029: basePixSize = 1280 # 縦横で大きい辺の変更したいサイズ
030: width, height = gpuSrc.size()
031: largeSize = max(height, width) # 大きい方の辺のサイズ
032: resizeRate = basePixSize / largeSize # 変更比率を計算
033:
034: gpuSrc = cv2.cuda.resize(gpuSrc, (int(width * resizeRate), int(height * resizeRate)), interpolation=None)
035:
036: return gpuSrc
037:
038: if __name__ == ‘__main__’:
039: print(cv2.__version__)
040:
041: __main()