How to Count Objects in an Image using Python?

Featured Img Count Objects

Hello, there programming enthusiast! Today we are going to learn how to count objects in an image. To count the images one has to make use of computer vision libraries. There are tones of libraries available to achieve the aim of the tutorial.

But today in this tutorial, we will be making use of the cvlib library which is very simple, easy, and a high-level library in Python. If you don’t have the library installed yet, do the same by using the pip command.

Import Required Libraries to count objects in an image

We need a number of modules/libraries for counting the objects. The same is done in the code below.

import cv2
import numpy as np
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
from numpy.lib.polynomial import poly

We would require the following modules:

Loading and Viewing the Image

To load an image we will be making use of the imread function of the OpenCV library and pass the name of the image as a parameter. Make sure the image is in the same directory as the code file.

To view an image we will be using the imshow function of the matplotlib module. The code is shown below.

img = cv2.imread('image1.jpg')
img1 = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)

The output displayed on the screen is as follows.

Initial Loaded Image
Initial Loaded Image

Creating Boxes around various Objects

The code below will display the boxes around the objects in the image. First thing we will need is to make use of the detect_common_objects function and pass our image object to it.

The function will return the box, label and the count of the box to be generated around an object detected. To draw the box, we would need the draw_bbox function and pass the outputs received by the previous function to this function.

box, label, count = cv.detect_common_objects(img)
output = draw_bbox(img, box, label, count)

To display the output image, we would be using the imshow function again and the output come out to be pretty accurate. The code is shown below.

output = cv2.cvtColor(output,cv2.COLOR_BGR2RGB)
Output Image Counting Objects
Output Image Counting Objects

Count objects in the image

print("Number of objects in this image are " +str(len(label)))

To count the number of objects we will count the number of labels generated by simply using the len function on the labels list created and print the number of objects found in the picture. Here for this picture, there were 17 objects visible to the system.

Sample Output

The same concept was used for another image and the results are as follows. The initial image loaded is as follows.

Initial Loaded Image2
Initial Loaded Image2

After detecting the objects in the image the output looks something like this. The program detected the four dogs present in the image.

Output Image Counting Objects2
Output Image Counting Objects2


Congratulations! Now you can try out any random image and check out how many different objects are present in the image. Try it out yourself!

Thank you for reading!