|
import cv2 |
|
import json |
|
import argparse |
|
from pathlib import Path |
|
|
|
|
|
|
|
ap = argparse.ArgumentParser() |
|
ap.add_argument("-i", "--image", required=True, help="Path to the image") |
|
args = vars(ap.parse_args()) |
|
|
|
|
|
img_p = Path(args["image"]) |
|
assert img_p.exists(), "Image does not exist" |
|
|
|
|
|
img = cv2.imread(args["image"]) |
|
|
|
|
|
ROI = cv2.selectROI("Image", img, False, False) |
|
|
|
|
|
|
|
with open("roi.csv", "a") as f: |
|
|
|
if f.tell() == 0: |
|
f.write("filename,ori_width,ori_height,roi_x,roi_y,roi_width,roi_height\n") |
|
ori_w, ori_h = img.shape[1], img.shape[0] |
|
f.write(f"{img_p.name},{ori_w},{ori_h},{ROI[0]},{ROI[1]},{ROI[2]},{ROI[3]}\n") |
|
|
|
|
|
cropped = img[ROI[1] : ROI[1] + ROI[3], ROI[0] : ROI[0] + ROI[2]] |
|
cv2.imshow("Cropped Image", cropped) |
|
cv2.waitKey(0) |
|
|