9-1Blend_gpu.py

000: import cv2
001:
002: def __main():
003: img1 = cv2.imread(‘IMG_0260.JPG’)
004: img2 = cv2.imread(‘IMG_0181.JPG’)
005:
006: gpuImg = cv2.cuda_GpuMat()
007: gpuDst = cv2.cuda_GpuMat()
008: # GPUメモリへ転送
009: gpuImg.upload(img1)
010: gpuDst.upload(img2)
011:
012: gpuImg = getGpuResize(gpuImg)
013: gpuDst = getGpuResize(gpuDst)
014:
015: # ここで処理を実行
016: gpuImgMask = getGpuMaskImg(gpuImg, gpuDst)
017: # GPUメモリから戻す
018: imgMask = gpuImgMask.download()
019:
020: cv2.imshow(‘Final result’, imgMask)
021: cv2.waitKey(0)
022: cv2.destroyAllWindows()
023: # 合成実行
024: def getGpuMaskImg(gpuImg1, gpuImg2):
025: compImg = cv2.cuda.addWeighted(src1=gpuImg1, alpha=0.7, src2=gpuImg2, beta=0.3, gamma=0)
026:
027: return compImg
028:
029: def getGpuResize(gpuImg):
030: basePixSize = 1280 # 縦横で大きい辺の変更したいサイズ
031: width, height = gpuImg.size()
032:
033: largeSize = max(height, width) # 大きい方の辺のサイズ
034: resizeRate = basePixSize / largeSize # 変更比率を計算
035:
036: gpuImg = cv2.cuda.resize(gpuImg, (int(width * resizeRate), int(height * resizeRate)))
037:
038: return gpuImg
039:
040: if __name__ == ‘__main__’:
041: print(cv2.__version__)
042:
043: __main()