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