Python OpenCV - selectroi() Function
Last Updated :
03 Jan, 2023
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)
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 coordinate of the top-left point of the selected area and width and height of the ROI (region of interest) so we are storing it in a variable called "r". Which is basically the top-left pixel position+width and height of selected rectangle in an image and the output array in order of [Top_Left_X, Top_Left_Y, Width, Height]
top_left_y = top_left_row = y1
top_left_x = top_left_col = x1

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:


Similar Reads
Python OpenCV - setWindowTitle() Function Python OpenCV setWindowTitle() method used for giving the title of the windows. It takes 2 parameters that are windows name and the title that needs to be given. Both the parameters are expected to be of string type. Syntax: cv2.setWindowTitle( winname, title )Â Parameters: winname: windows nametitl
1 min read
Python OpenCV - resizeWindow() Function resizeWindow() method in Python OpenCV is used to resize window displaying images/videos to a specific size. The specified window size is for images excluding toolbars. This only works for created windows having flags other than CV_WINDOW_AUTOSIZE. Syntax: cv2.resizeWindow(window_name, width, height
1 min read
Python OpenCV - waitKeyEx() Function Python OpenCv waitKeyEx() method is similar to waitKey() method but it also returns the full key code. The key code which is returned is implementation-specific and depends on the used backend: QT/GTK/Win32/etc. Syntax: cv2.waitKey(delay) Parameters: delay: The time in milliseconds after which windo
2 min read
Python OpenCV - waitKey() Function waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed. Example: Pythonim
2 min read
Python OpenCV - startWindowThread() Function This article will discuss how to use the python OpenCV startWindowThread() function. Do you want to display images and videos using a simplified interface through an OpenCV code? Then, you must check out the OpenCV startWindowsThread() function, which lets you use the high GUI windows, i.e., a simpl
3 min read
Python OpenCV Cheat Sheet The Python OpenCV Cheat Sheet is your complete guide to mastering computer vision and image processing using Python. It's designed to be your trusty companion, helping you quickly understand the important ideas, functions, and techniques in the OpenCV library. Whether you're an experienced developer
15+ min read
Python OpenCV - Canny() Function Canny edge detection algorithm is used in computer vision for identifying edges within an image. It helps in highlighting boundaries which are important for tasks like object detection and image segmentation. In this article, we will see how OpenCV's built-in Canny() function detects edges in an ima
3 min read
OpenCV Tutorial in Python OpenCV, short for Open Source Computer Vision Library, is an open-source computer vision and machine learning software library. Originally developed by Intel, it is now maintained by a community of developers under the OpenCV Foundation.OpenCVis a huge open-source library for computer vision, machin
3 min read
Python OpenCV - namedWindow() Function Python OpenCV namedWindow() method is used to create a window with a suitable name and size to display images and videos on the screen. The image by default is displayed in its original size, so we may need to resize the image for it to fit our screen. Created windows are referred by their names and
3 min read
Getting Started with Python OpenCV Computer Vision is one of the techniques from which we can understand images and videos and can extract information from them. It is a subset of artificial intelligence that collects information from digital images or videos. Python OpenCV is the most popular computer vision library. By using it, o
15+ min read