Interface編集部
4-4Resize_Cubic_gpu.py
000: import cv2
001: import numpy as np
002:
003: def __main():
004: img = createPatten()
005: org = img.copy()
006:
007: gpuImg = cv2.cuda_GpuMat()
008: gpuImg.upload(img)
009:
010: gpuImg = getResize(gpuImg)
011:
012: img = gpuImg.download()
013:
014: cv2.imshow(‘Original’, org)
015: cv2.imshow(‘INTER_CUBIC’, img)
016: cv2.waitKey(0)
017: cv2.destroyAllWindows()
018:
019: def getResize(gpuImg):
020: “””GPUを使用”””
021:
022: basePixSize = 240 # 縦横で大きい辺の変更したいサイズ
023: width, height = gpuImg.size()
024:
025: largeSize = max(height, width) # 大きい方の辺のサイズ
026: resizeRate = basePixSize / largeSize # 変更比率を計算
027:
028: # —ここでサイズ変更—
029: dst = cv2.cuda.resize(gpuImg, (int(width * resizeRate), int(height * resizeRate)), interpolation=cv2.INTER_CUBIC)
030:
031: return dst
032:
033: # サンプル画像を作成
034: def createPatten():
035: square = 1000
036: border = 2
037: size = np.array([square, square, 3])
038: color = np.array([255., 255., 255.])
039: img = np.full(size, color, dtype=np.uint8)
040:
041: # 斜め格子パターンを作成
042: borderColor = (255, 0, 0)
043: for wh in range(0, square, 10):
044: cv2.line(img=img, pt1=(wh, 0), pt2=(0, wh), color=borderColor, thickness=border, lineType=cv2.LINE_AA)
045: for wh in range(0, square, 10):
046: cv2.line(img=img, pt1=(square, wh), pt2=(wh, square), color=borderColor, thickness=border, lineType=cv2.LINE_AA)
047:
048: borderColor = (255, 0, 255)
049: for wh in range(square, 0, -10):
050: cv2.line(img=img, pt1=(wh, 0), pt2=(square, square – wh), color=borderColor, thickness=border, lineType=cv2.LINE_AA)
051:
052: return img
053:
054: if __name__ == ‘__main__’:
055: print(cv2.__version__)
056:
057: __main()