Created
January 19, 2018 10:30
-
-
Save kushalvyas/4f5f71ba10a8f5c86eaf4a4fa84a057f to your computer and use it in GitHub Desktop.
OpenCV Cropping Utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Opencv Image Cropper | |
Commands: | |
s : Overwrite & Save image | |
left-arrow : previous | |
right-arrow : next | |
''' | |
import cv2 | |
import glob | |
import numpy as np | |
import sys, os | |
import argparse | |
class Cropper: | |
def __init__(self, args): | |
tmp_resolution = args.get("resolution").split("x") | |
self.width, self.height = map(int, tmp_resolution) | |
im_path = os.path.join(args.get("base_dir"), args.get("path")) | |
self.im_path = sorted(glob.glob(im_path + "/*"), key=lambda x: int(x.split("/")[-1].split(".")[0])) | |
print "Path list ", self.im_path | |
self.refPt = [] | |
self.cropping = False | |
self.selection = None | |
self.drag_start = None | |
self.show_backproj = False | |
self.track_window = None | |
cv2.namedWindow("Image") | |
cv2.namedWindow("new") | |
cv2.setMouseCallback("Image", self.click_and_crop) | |
def click_and_crop(self, event, x, y, flags, param): | |
''' | |
https://www.pyimagesearch.com/2015/03/09/capturing-mouse-click-events-with-python-and-opencv/ | |
''' | |
if event == cv2.EVENT_LBUTTONDOWN: | |
self.refPt = [(x, y)] | |
self.cropping = True | |
# check to see if the left mouse button was released | |
elif event == cv2.EVENT_LBUTTONUP: | |
# record the ending (x, y) coordinates and indicate that | |
# the cropping operation is finished | |
self.refPt.append((x, y)) | |
self.cropping = False | |
self.new_image = self.image[self.refPt[0][1]:self.refPt[1][1], self.refPt[0][0]:self.refPt[1][0]] | |
cv2.imshow("Image", self.image) | |
cv2.imshow("new", self.new_image) | |
cv2.rectangle(self.image, self.refPt[0], self.refPt[1], (255, 255, 255), 2) | |
def run(self): | |
quit_flag = False | |
i=0 | |
while i < len(self.im_path): | |
if quit_flag: | |
break | |
self.image = cv2.imread(self.im_path[i]).copy() | |
self.new_image = None | |
try: | |
cv2.destroyWindow("new") | |
except: | |
pass | |
while True: | |
cv2.imshow("Image", self.image ) | |
ch = cv2.waitKey(1) & 0xFF | |
if ch == 27: | |
quit_flag = True | |
print "Quit Flag is True" | |
break | |
elif ch == ord('s') or ch == ord("S"): | |
# compute_new_image | |
print "s pressed" | |
if self.new_image is not None: | |
cv2.imwrite(self.im_path[i], self.new_image) | |
i = i + 1 | |
break | |
elif ch == ord('d'): | |
print "D key pressed.\nLoading Next image" | |
i = i + 1 | |
break | |
elif ch == ord('a'): | |
print "A key pressed.\nLoading previous image" | |
i = i - 1 | |
break | |
def main(argv): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-b", "--base_dir", type=str, default="/home/<path to images>", help="Path for base dir of images") | |
parser.add_argument("-p", "--path", type=str, default=None, help="sub-dir for images" ) | |
parser.add_argument("-r", "--resolution", type=str, default="1280x960", help="Image resolution") | |
crop = Cropper(vars(parser.parse_args())) | |
crop.run() | |
if __name__ == '__main__': | |
main(sys.argv) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment