Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- refPt = []
- cropping = False
- def click_and_crop(event, x, y, flags, param):
- global refPt, cropping
- if event == cv2.EVENT_LBUTTONDOWN:
- refPt = [(x, y)]
- cropping = True
- elif event == cv2.EVENT_LBUTTONUP:
- refPt.append((x, y))
- cropping = False
- cv2.rectangle(clone1, refPt[0], refPt[1], (0, 255, 0), 2)
- cv2.imshow("image", clone1)
- image = cv2.imread('orig.jpg')
- x,y,z=np.shape(image)
- clone1=cv2.resize(image,(592,700),interpolation=cv2.INTER_AREA)
- clone=clone1.copy()
- cv2.namedWindow("image")
- cv2.setMouseCallback("image", click_and_crop)
- while True:
- cv2.imshow("image", clone1)
- key=cv2.waitKey(1) & 0xFF
- if key == ord("r"):
- clone1 = clone.copy()
- if key == ord("c"):
- break
- if len(refPt) == 2:
- roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
- roi1 = cv2.resize(roi,(y,x),interpolation=cv2.INTER_AREA)
- cv2.imwrite("ROI", roi1)
- cv2.waitKey(0)
- print('Cropping Complete')
- cv2.destroyAllWindows()
- clone1=cv2.resize(image,(y/2,x/5),interpolation=cv2.INTER_AREA)
- a,b,c,d=(refPt[0][1])*5,(refPt[1][1])*5,(refPt[0][0])*2,(refPt[1][0])*2
- roi1=image[a:b, c:d]
Add Comment
Please, Sign In to add comment