10-4SelectMultiImage.py

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