Find a specific object in an image using OpenCV in Python

In this tutorial, we will learn to find a specific object in an image using OpenCV in Python Programming.

OpenCV is an open-source Python library for Computer Vision, Machine Learning, Image processing, and Artificial Intelligence. One can process images and videos in real-time to detect objects, faces, facial expressions, and even Optical Character Recognition(OCR).

We will be detecting cars in an image using the OpenCV library. For car detection, we need to download cars.xml file from here.

To install the OpenCV library, open your Command Prompt / Terminal and write:

pip install opencv-python

Lets Code: Detect a specific object from an image in Python

Input Image:

specific object from an image in Python

Importing OpenCV and matplotlib library

#Importing cv2 and matplotlib libraries
import cv2
from cv2 import CascadeClassifier
from matplotlib import pyplot as plt

Opening image using OpenCV library. By default, OpenCV opens up images in BGR format. So it’s important to convert BGR image into grayscale and RGB format.

# Opening/Reading image
img = cv2.imread("cars.jpg")

# OpenCV opens up image in BGR format, covert BGR to grayscale and RBG
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

Creating Cascade Classifier for car detection. detectMultiScale() is a function of cascade classifier, which is used to detect objects of different sizes from the input image.

# Creating CascadeClassifier for car detection
car_cascade = cv2.CascadeClassifier('cars.xml')

car_img = car_cascade.detectMultiScale(img_gray,1.1,4)

Count the number of cars, If there are no cars in the image, the code will terminate. cv2.rectangle() function is used to customize the properties of the rectangle enclosing the detected object. We can set the color and thickness of the rectangle.

# Count number of cars, If there are no cars in the image, code will terminate
number_of_cars = len(car_cascade)

if number_of_cars != 0:
  
  # Runs when number of cars are more than 1
  for (x, y, w, h) in car_cascade:
    
    # Draw green rectangle around the detected car
    cv2.rectangle(img_rgb, (x, y),
          (x + w, y + h),
          (0, 255, 0), 2)

Plotting the output image

# Show output image with detected cars 
plt.subplot(1, 1, 1)
plt.imshow(img_rgb)
plt.show()

Combining all code

#Importing cv2 and matplotlib libraries
import cv2
from cv2 import CascadeClassifier
from matplotlib import pyplot as plt

# Opening/Reading image
img = cv2.imread("cars.jpg")

# OpenCV opens up image in BGR format, covert BGR to grayscale and RBG
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Creating CascadeClassifier for car detection
car_cascade = cv2.CascadeClassifier('cars.xml')

car_img = car_cascade.detectMultiScale(img_gray,1.1,4)

# Count number of cars, If there are no cars in the image, code will terminate
number_of_cars = len(car_cascade)

if number_of_cars != 0:
  
  # Runs when number of cars are more than 1
  for (x, y, w, h) in car_cascade:
    
    # Draw green rectangle around the detected car
    cv2.rectangle(img_rgb, (x, y),
          (x + w, y + h),
          (0, 255, 0), 2)

    
# Show output image with detected cars 
plt.subplot(1, 1, 1)
plt.imshow(img_rgb)
plt.show()


Output:

Find a specific object in an image using OpenCV in Python

Thus we have detected all the cars from an image in Python using OpenCV.

Leave a Reply

Your email address will not be published.