forked from KuKuXia/OpenCV_for_Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51_Select_ROI.py
51 lines (36 loc) · 1.02 KB
/
51_Select_ROI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Select region of interest
"""
# Import the packages
import numpy as np
import cv2
# Select single ROI
def single_roi(im):
# Select multiple ROI
roi = cv2.selectROI("Image", im, 0, 0)
# Crop images
im_crop = im[int(roi[1]):int(roi[1] + roi[3]),
int(roi[0]): int(roi[0] + roi[2])]
# Show the image
cv2.imshow("Cropped Image", im_crop)
# Select multi ROIs
def multi_rois(im):
# Select multiple ROI
roi = cv2.selectROIs("Image", im, 0, 0)
for i in range(len(roi)):
# Crop image
im_crop = im[int(roi[i][1]):int(roi[i][1] + roi[i][3]),
int(roi[i][0]): int(roi[i][0] + roi[i][2])]
# Show the image
cv2.imshow("Cropped Image: "+str(i), im_crop)
if __name__ == "__main__":
# Read the image
im = cv2.imread("./images/robot.jpg")
# Single ROI
# single_roi(im)
# Multi ROIs
multi_rois(im)
# Wait until a key pressed
cv2.waitKey(0)
# Destroy all the windows opened before
cv2.destroyAllWindows()