Python OpenCV: Getting region of interest

In this tutorial we will learn how to get and display a region of interest from an image, using Python and OpenCV.

Introduction

In this tutorial we will learn how to get and display a region of interest from an image, using Python and OpenCV.

In some cases, it might make sense to only apply some type of operation only to a portion of an image. Thus, it is important to understand how we can extract a region of interest of an original image.

Note that we are not going to apply any sort of algorithm to extract parts of an image that have some special feature, but rather simply define a part of the image that we want to obtain by specifying the coordinates of that region.

This tutorial was tested with version 4.0.0 of OpenCV and version 3.7.2 of Python.

The code

As usual, we will start by including the cv2 module.

import cv2

Followed by that, we are going to read our test image with a call to the imread function. As input, we need to pass the path to the file in the file system.

originalImage = cv2.imread('C:/Users/N/Desktop/testImg.png')

The image we are going to read is the one shown below in figure 1. As can be seen, it’s an image with some rectangles and a text in the middle.

For illustration purposes, we will assume that our region of interest (ROI) is the text.

Regarding the x coordinates, this region is located more or less between x = 240 and x = 430 and regarding y coordinates it is between y = 230 and y = 310. These are the coordinates we are going to use to extract the region of interest.

Testing image, to extract a region of interest.
Figure 1 – Testing image.

Recall from previous tutorials that, when we read an image with the imread function, we obtain a ndarray. Thus, we can use slicing to obtain our region of interest. You can read more about numpy indexing here.

In terms of notation, it is as simple as:

slicedImage = originalImage[y1:y2, x1:x2]

In other words, it means that we want all the pixels between the coordinates y1 and y2 and x1 and x2. For our case, taking in consideration the region of interest we have mentioned before, we get:

slicedImage = originalImage[230:310, 240:430]

To finalize, we will display both the original image and the sliced image (region of interest) in two different windows.

cv2.imshow("Original Image", originalImage)
cv2.imshow("Sliced Image", slicedImage)

cv2.waitKey(0)
cv2.destroyAllWindows()

The final code can be seen below.

import cv2

originalImage = cv2.imread('C:/Users/N/Desktop/testImg.png')

slicedImage = originalImage[230:310, 240:430]

cv2.imshow("Original Image", originalImage)
cv2.imshow("Sliced Image", slicedImage)

cv2.waitKey(0)
cv2.destroyAllWindows()

Testing the code

To test the code, simply run it in a tool of your choice. In my case, I’ll be using IDLE, a Python IDE.

You should get a result similar to figure 2. As can be seen, we have obtained both the original image and the region of interest, as expected.

Output of the program, showing both the original image and the region of interest (ROI)
Figure 2 – Output of the code, showing the original image and the region of interest.

One Reply to “Python OpenCV: Getting region of interest”

Leave a Reply