Select ROI or Multiple ROIs [Bounding box] in OPENCV python.
In this tutorial, we will learn how to Graphically select a Region of interest (ROI) in an image and save it.
There are two functions which can be used to select ROI or ROIs in OPENCV.
- “cv2.selectROI” which can be used to select a single ROI bounding box.
- “cv2.selectROIs” which is used when you want to select multiple ROI from an image.
Selecting Single ROI bounding Box in OpenCV [python].
Selecting a single ROI is useful when you are sure you have to crop a single subject/ ROI from the image.
Click and drag the mouse to make bounding box on the image . Press enter to finish selecting ROI and resume the program.
Steps to crop a single single subject from an image.
- Import the necessary libraries.
- import cv2
- import numpy
- Read the image by using “imread” function.
- Pass the image in “SelectROI” function.
- save the selected rectangle point (roi) in a variable.
- roi variable will save xy coordinate of left top corner and bottom right corner point of bounding box
- Use the rectangle points to crop.
- roi_cropped=img_raw[int(roi):int(roi+roi), int(roi):int(roi+roi)]
- Display the Cropped ROI
- Save the cropped ROI.
Select ROI code [Python Code]
import cv2 import numpy as np #image_path img_path="image.jpeg" #read image img_raw = cv2.imread(img_path) #select ROI function roi = cv2.selectROI(img_raw) #print rectangle points of selected roi print(roi) #Crop selected roi from raw image roi_cropped = img_raw[int(roi):int(roi+roi), int(roi):int(roi+roi)] #show cropped image cv2.imshow("ROI", roi_cropped) cv2.imwrite("crop.jpeg",roi_cropped) #hold window cv2.waitKey(0)
Selecting Multiple ROI bounding box
Selecting Multiple ROIs is useful when you have multiple subjects to select from an image.
Use the “selectROIs” function will save selected ROIs rectangle point in a array which can be accessed by using indexing.
Click and drag the mouse to make a bounding box on the image. Press enter every time you select a bounding box this will save your selections in ROIs variable, once you are done selecting press escape to resume the program.
Select Multiple ROIs [Python Code]
import cv2 import numpy as np #image_path img_path="image.jpeg" #read image img_raw = cv2.imread(img_path) #select ROIs function ROIs = cv2.selectROIs("Select Rois",img_raw) #print rectangle points of selected roi print(ROIs) #Crop selected roi ffrom raw image #counter to save image with different name crop_number=0 #loop over every bounding box save in array "ROIs" for rect in ROIs: x1=rect y1=rect x2=rect y2=rect #crop roi from original image img_crop=img_raw[y1:y1+y2,x1:x1+x2] #show cropped image cv2.imshow("crop"+str(crop_number),img_crop) #save cropped image cv2.imwrite("crop"+str(crop_number)+".jpeg",img_crop) crop_number+=1 #hold window cv2.waitKey(0)
Additionally You can use :
By default the bounding box is created from the top left corner but you can also use fromCenter= true argument to start bounding box from the center.
You can disable the cross hair by using showCrosshair= false argument .