6-1Canny_gpu.py

000: import cv2
001:
002: def __main():
003: gpuImg = cv2.cuda_GpuMat()
004: img = cv2.imread(‘IMG_0260.JPG’)
005:
006: gpuImg.upload(img)
007: gpuImg = getGpuResize(gpuImg)
008:
009: gpuImg = cv2.cuda.cvtColor(gpuImg, cv2.COLOR_BGR2GRAY)
010: # ここで処理を実行する
011: “””GPUを使用”””
012: detector = cv2.cuda.createCannyEdgeDetector(low_thresh=100, high_thresh=200)
013: dstImg = detector.detect(gpuImg)
014: dst = dstImg.download()
015:
016: cv2.imshow(‘Final result(GPU)’, dst)
017: cv2.waitKey(0)
018:
019: cv2.destroyAllWindows()
020:
021: def getGpuResize(gpuImg):
022: basePixSize = 1280 # 縦横で大きい辺の変更したいサイズ
023: width, height = gpuImg.size()
024:
025: largeSize = max(height, width) # 大きい方の辺のサイズ
026: resizeRate = basePixSize / largeSize # 変更比率を計算
027:
028: gpuImg = cv2.cuda.resize(gpuImg, (int(width * resizeRate), int(height * resizeRate)))
029:
030: return gpuImg
031:
032: if __name__ == ‘__main__’:
033: print(cv2.__version__)
034:
035: __main()