Interface編集部
6-3Sobel.py
000: import cv2
001:
002: def __main():
003: img = cv2.imread(‘IMG_0260.JPG’)
004: img = getResize(img)
005: org = img.copy()
006:
007: img = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)
008: dst = cv2.Sobel(src=img, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=3) # 10 01 11 のどれかの組み合わせのみ使用可
009: dst = cv2.convertScaleAbs(src=dst) # 8ビット符号なし整数に変換
010:
011: cv2.imshow(‘Original’, org)
012: cv2.imshow(‘Final result’, dst)
013: cv2.waitKey(0)
014:
015: cv2.destroyAllWindows()
016:
017: def getResize(src):
018: basePixSize = 1280 # 縦横で大きい辺の変更したいサイズ
019: height = src.shape[0]
020: width = src.shape[1]
021:
022: largeSize = max(height, width) # 大きい方の辺のサイズ
023: resizeRate = basePixSize / largeSize # 変更比率を計算
024:
025: dst = cv2.resize(src, (int(width * resizeRate), int(height * resizeRate)), interpolation=None)
026:
027: return dst
028:
029: if __name__ == ‘__main__’:
030: print(cv2.__version__)
031:
032: __main()