Cropping Images with OpenCV

Samuel Ozechi
4 min readJul 23, 2024

--

OpenCV reads images as a stack of Numpy arrays that hold values of the image pixel at each position. The Numpy array format of read images allows images to be cropped using Numpy slice operations. Slice operations involve returning only the regions of interest in a sequence. We would recall slice operations as used in lists, tuples and other sequence types as described in this article.

Here are the steps to crop images with OpenCV

  1. First, we load and display the original image.
# import required packages
import cv2
import matplotlib.pyplot as plt

# load and display the original image
image = cv2.imread('Flag_of_Ghana.png')
cv2.imshow("Original", image)
cv2.waitKey(0)

Output:

To show how to crop out portions of an image with OpenCV, I will be cropping out the star, and colour (red, yellow and green) regions from the image. We would use slicing operations to crop out regions of the image as earlier stated. The slices are defined by the start and end values of both axes (horizontal (X) and vertical(Y)) in the format :

image[startY: endY, startx:endX]

This will return a Numpy array (of the image pixels) for the desired region that we want to crop out. How then do we determine the start and end values of the X and Y axes for our region of interest?

These values (startX, endX, startY, endY) are simply coordinate values that map out the region of interest. These values are not displayed alongside the image in OpenCV, but we can use other applications (such as Photoshop, Corel Draw, Paint etc.) or in our case python visualization libraries (such as Matplotlib) to display images with their horizontal and vertical coordinates.

As always, this is better understood in practice. Let’s display our image using matplotlib.pyplot from which we can retrieve the coordinates that map out our region of interest for cropping.

# get the coordinates of the star
# # read and plot the image with matplotlib
plt_flag = plt.imread('Flag_of_Ghana.png')
plt.imshow(plt_flag)
plt.show()

Notice we read the image again before displaying it with Matplotlib, this is because Matplotlib and OpenCV return different colour forms for the image (RGB for Matplotlib against BGR for OpenCV). You could read the OpenCV image object straightaway with the Matplotlib to see the difference.

Let’s see the result of the above script.

Output:

The plt.imshow function returns the read image along with the coordinates values for the x and y axes.

Next, we retrieve the values of the start and end coordinates of our region of interest (star). Below is a plot of how to trace out the values of the star region.

Trace of the values of the star region

From the image we can retrieve the coordinates (startY(y1), endY(y2), startX(x1), endX(x2)). Better still, the figure returned by pyplot.imshow displays the coordinates of any point in the image when using the cursor to locate any point in the image.

Now we can define our start and end coordinates for both axes and crop out the star.

3. Define the coordinates for the region of interest and crop out the region.

# from the plot
# define coordinates that map out star
startX, startY = 244, 142
endX, endY = 395, 285

# # crop out the star using coordinates
star = image[startY:endY, startX:endX]
print(star.shape)
cv2.imshow("Star", star)
cv2.waitKey(0)

Output:

The cropped star portion of the flag

The image is cropped when the coordinates are used to slice the original image array, returning the area of interest. We can go on to repeat the steps above to crop out other regions.

Crop out the red region

# crop out the red region using coordinates
startX, startY = 0, 0
endX, endY = 640, 140

# crop out red region using coordinates
red = image[startY:endY, startX:endX]
cv2.imshow("red region", red)
cv2.waitKey(0)

Output:

The cropped red portion of the flag

We can follow the same steps to crop out the green region.

Script: Crop out the green region

# define the coordinates that map out the green region
startX, startY = 0, 285
endX, endY = 640, 425

# crop out green using coordinates
green = image[startY:endY, startX:endX]
cv2.imshow("Green", green)
cv2.waitKey(0)

Output:

The cropped green portion of the flag

Finally, we can apply the steps to crop out the yellow portion of the flag.

# define the coordinates that map out the yellow region
startX, startY = 0, 143
endX, endY = 640, 282

# crop out the yellow region using the coordinates
yellow = image[startY:endY, startX:endX]
cv2.imshow("yellow region", yellow)
cv2.waitKey(0)

Output:

The cropped yellow portion of the flag

Summary

I have shown the steps required to crop images using slice operations. I also showed how to get the coordinate values that map out the region of interest.

--

--

No responses yet