Guest User

Untitled

a guest
Feb 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. refPt = []
  4. cropping = False
  5. def click_and_crop(event, x, y, flags, param):
  6.  
  7. global refPt, cropping
  8. if event == cv2.EVENT_LBUTTONDOWN:
  9. refPt = [(x, y)]
  10. cropping = True
  11. elif event == cv2.EVENT_LBUTTONUP:
  12. refPt.append((x, y))
  13. cropping = False
  14. cv2.rectangle(clone1, refPt[0], refPt[1], (0, 255, 0), 2)
  15. cv2.imshow("image", clone1)
  16.  
  17. image = cv2.imread('orig.jpg')
  18. x,y,z=np.shape(image)
  19. clone1=cv2.resize(image,(592,700),interpolation=cv2.INTER_AREA)
  20.  
  21. clone=clone1.copy()
  22. cv2.namedWindow("image")
  23. cv2.setMouseCallback("image", click_and_crop)
  24. while True:
  25. cv2.imshow("image", clone1)
  26. key=cv2.waitKey(1) & 0xFF
  27. if key == ord("r"):
  28. clone1 = clone.copy()
  29. if key == ord("c"):
  30. break
  31. if len(refPt) == 2:
  32. roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
  33. roi1 = cv2.resize(roi,(y,x),interpolation=cv2.INTER_AREA)
  34. cv2.imwrite("ROI", roi1)
  35. cv2.waitKey(0)
  36. print('Cropping Complete')
  37. cv2.destroyAllWindows()
  38.  
  39. clone1=cv2.resize(image,(y/2,x/5),interpolation=cv2.INTER_AREA)
  40.  
  41. a,b,c,d=(refPt[0][1])*5,(refPt[1][1])*5,(refPt[0][0])*2,(refPt[1][0])*2
  42.  
  43. roi1=image[a:b, c:d]
Add Comment
Please, Sign In to add comment