Skip to content

Commit a36f5d8

Browse files
Cropping Images Dynamically (#10)
* Added Image cropping * Update README.md * Added Shape Contouring * Re-organised Directories
1 parent 2f2c679 commit a36f5d8

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed
1.05 MB
Loading
903 KB
Loading
24.4 KB
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Image Cropper
2+
This code allows you to crop images dynamically and save them. Mouse Events in OpenCV will be used to achieve this.
3+
4+
### Executing the code
5+
1. Run the .py file by running the command ``` python3 crop.py``` in the terminal or cmd.
6+
2. Use the **Left** mouse button to drag out a rectangular region of the image you want to crop. **Release** the button, once you are done.
7+
3. The selected rectangular is shown on the image. You can press **r** to reset your selection.
8+
4. Press **c** to crop the image. A new window opens up.
9+
a) Press **s** to save the cropped image.
10+
b) Press **r** to reset and return to the original image.
11+
5. Repeat from step 2 to crop more images.
12+
13+
### Demo
14+
![](https://github.com/Pranjalmishra30/OpenCV-Rep/blob/master/Mini-Projects/Cropping_Images/Data/crop-DEMO.gif)
15+
16+
### Refrences
17+
1. Mouse events [tutorial](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_mouse_handling/py_mouse_handling.html)
18+
2. Pyimagesearch [tutorial](https://www.pyimagesearch.com/2015/03/09/capturing-mouse-click-events-with-python-and-opencv/)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import cv2
2+
refpt = [] #List of refrence points
3+
4+
def select_roi (event,x,y,flags,param):
5+
global refpt #Global refrences
6+
7+
if event == cv2.EVENT_LBUTTONDOWN: # When the left mouse button is clicked
8+
refpt = [(x,y)]
9+
10+
elif event == cv2.EVENT_LBUTTONUP: # When the left mouse button is released
11+
refpt.append((x,y)) # recording the last coordinates
12+
cv2.rectangle(img_main,refpt[0],refpt[1],(0,255,0),2)
13+
cv2.imshow("frame",img_main)
14+
print("Selection Successful")
15+
16+
img = cv2.imread("Data/Man_United.jpeg")
17+
img_main = cv2.resize(img,(400,400)) #Resizing image
18+
19+
clone = img_main.copy() # To reset the image after cropping
20+
clone2 = img_main.copy() # To crop a section out without affecting the original image
21+
22+
cv2.namedWindow("frame")
23+
cv2.setMouseCallback("frame",select_roi)
24+
25+
i=1 # Numbering for saving images
26+
27+
while True:
28+
cv2.imshow("frame",img_main)
29+
var = cv2.waitKey(0)
30+
31+
'''
32+
Instructions
33+
- Select a region , then press c to crop that portion.
34+
- Press r to reset your selection.
35+
- In the Crop mode , press s to save your cropped image or press r to reset selection
36+
- Press q to exit the program.
37+
'''
38+
39+
if var == ord('c'): # Crop selected images
40+
41+
if len(refpt)==2:
42+
roi = clone2[refpt[0][1]:refpt[1][1], refpt[0][0]:refpt[1][0]] # [x1:x2 , y1:y2]
43+
cv2.namedWindow("Crop")
44+
cv2.imshow("Crop",roi)
45+
print("Cropped")
46+
47+
var2 = cv2.waitKey(0)
48+
49+
if var2 == ord('s'): # Saving cropped image
50+
cv2.imwrite("Data/cropped image{}.png".format(i),roi) # Name of the saved image
51+
i=i+1
52+
print("image saved\n")
53+
cv2.destroyWindow("Crop")
54+
img_main = clone.copy()
55+
56+
elif var2 == ord('r'): # Reset
57+
cv2.destroyWindow("Crop")
58+
print("Reset\n")
59+
img_main = clone.copy()
60+
61+
elif var == ord('r'): # Reset
62+
print("Reset\n")
63+
img_main = clone.copy()
64+
65+
elif var == ord('q'): # Exit the loop
66+
print("Exiting ...")
67+
break
68+
69+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)