Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python OpenCV – selectroi() Function

  • Last Updated : 24 Oct, 2021

In this article, we are going to see an interesting application of the OpenCV library, which is selectROI(). With this method, we can select a range of interest in an image manually by selecting the area on the image.

Syntax: cv2.selectROI(Window_name, source image)

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Parameter:

  • window_name:  name of the window where selection process will be shown.
  • source image:  image to select a ROI.
  • showCrosshair: if true crosshair of the selection rectangle will be displayed.
  • fromCenter: if true origin of selection will match initial mouse position

Using this function in OpenCV, we can precisely and manually select the area of interest we needed from the image and hence we can perform many tasks for that specific area. We can pass that particular area as an input for another task. We can also draw a tracking figure(rectangle) on the area using the coordinates or we can precisely and freely crop an image. First of all, we need to import the required libraries which in our case it is OpenCV and NumPy. NumPy library plays a very crucial part in this program because OpenCV uses NumPy as the backbone to do all image manipulations.

Before doing all sorts of functions in an image it is obvious to read the image first. And store it in a variable to access it in the future for further manipulations.



Syntax: 

cv2.imread(source image)

Now we are getting into the actual function which is selectROI(). So, basically, this function will allow us to select a range of interest in an image(a particular area of an image) and perform different actions in that area, in this particular example we are going to crop the image to display the cropped image.

Now we are going to call the selectRoi() function and pass in the image as a parameter and what the function is going to return is an array of different values which contained the coordinates of the selected area and we are storing it in a variable called “r”. Which is basically the starting and ending pixels of the selected area in an image and the output array in order of  [Top_X, Top_Y, Bottom_X, Bottom_Y]

Which in OpenCV  the x and y coordinates are kind of swapped,

Note: This selectedROI() function has its own default output which will show us the image automatically and let us manually select the ROI in the image. We can also name that window by passing windowname parameter in the function()

Controls of the function: After selecting the ROI, we are asked to press the space button or enter to proceed with the selected area. And C to cancel the selection. Using those coordinates we are going to select the particular selected area and crop it out and show the output. To crop the image with NumPy arrays,

Syntax: 

source_image[ start_row : end_row, start_col : end_col]

In which we should pass the value of the starting and ending pixel values of the image. Finally, we are going to show the cropped image and destroy the windows.

Program: Program to select and crop an image.

Python3




import cv2
import numpy as np
  
  
# Read image
image = cv2.imread("image.png")
  
# Select ROI
r = cv2.selectROI("select the area", image)
  
# Crop image
cropped_image = image[int(r[1]):int(r[1]+r[3]), 
                      int(r[0]):int(r[0]+r[2])]
  
# Display cropped image
cv2.imshow("Cropped image", cropped_image)
cv2.waitKey(0)

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!