10-4SelectImage.py

000: import cv2
001:
002: def __main():
003: img = cv2.imread(‘ruby.jpg’)
004:
005: img = getResize(img)
006: # ここで処理を実行
007: x, y, w, h = cv2.selectROI(‘Final result’, img=img, showCrosshair=False, fromCenter=False)
008: # 選択した領域を切り取る
009: dst = img[y:y+h, x:x+w]
010:
011: cv2.imshow(‘Final result’, dst)
012: cv2.waitKey(0)
013:
014: cv2.destroyAllWindows()
015:
016: def getResize(src):
017: basePixSize = 1280 # 縦横で大きい辺の変更したいサイズ
018: height = src.shape[0]
019: width = src.shape[1]
020:
021: largeSize = max(height, width) # 大きい方の辺のサイズ
022: resizeRate = basePixSize / largeSize # 変更比率を計算
023:
024: dst = cv2.resize(src, (int(width * resizeRate), int(height * resizeRate)), interpolation=None)
025:
026: return dst
027:
028: if __name__ == ‘__main__’:
029: print(cv2.__version__)
030:
031: __main()