Object Detection using OpenCV
By: Neupane Parlad
Tags: Data Science, Opencv, Computer Science, Image Processing

Photo by timJ on Unsplash

Object detection is an important part of image processing. The autonomous vehicle has to detect lanes, road surfaces, other vehicles, people, signs, and signals, etc. We live in such a dynamic world, and everything is constantly changing. While ago, a friend gave me a PDF file, told me to locate and get a specific value in PDFs. The application of Object Detection is everywhere. Here is another example, Data scientists are using Object Detection to identify the planet disease from the vegetable leaf.

A few weeks ago, I was doing research on Deep Learning and Computer Vision on autonomous cars. This article includes some of the interesting things from my research. Feature Detection is one of the tasks of Object Detection. So, What is feature detection? For Humans, We understand the pattern, shape, size, color, lengths, and others to identify the object. It’s somewhat similar to computers as well. The Feature is something we train our model to know if it finds one. A Feature can be anything like shape, edge, length, etc., and also combinations of all of them. In one of my previous projects about DeepFake detection, I used MSE (Mean Square Error), PSNR (peak signal-to-noise ratio), SSIM (Structural Similarity Index), and histogram as a feature to identify DeepFake images form real images.

A feature can be anything unique that can be found in general. A good feature has to be repeatable and extensible. For example, suppose the goal is to detect dogs from the large set of images, which also contains the images of cats and other animals.

Image by Gerhard G. from Pixabay

Going back to the previous statement about feature has to be something unique and need to be presented in the majority of data. If we have a majority of images similar to the above two images, What could be the bad Feature here?

A bad feature, in this case, is ear size. Our understanding was the size of the dog ear is larger in general. But that is not the cause in our sample images. In the first image, the dog ear is a similar size to a cat or even smaller. If we use ear size as the feature to train our model only using these two images, we will have 50% true negative or false positive. This brings another important point. Which is if you want to have higher success in your model, you should pick the feature carefully. A tale can’t be a good feature to separate either because it’s not visible clearly. Size is also not a good choice.

Our goal here is to identify another object, such as a truck on the road. We can use a technique like Harris Corner Detection or canny edge detection to detect the edges. We need to separate cars, pedestrians, signs from the images. We can use OpenCV to identify trucks specifically.

import cv2 cv2.matchTemplate()

Template Matching simply a technique that slides the input image over the template image and compares the template images and input image under a template image. It returns a grayscale image, and each pixel denotes how many neighbors of that pixels matches with a template. There are numerous template matching methods available in OpenCV. Here is the mathematical formula for the correlation coefficient.

Once the match is found in both images, it will singles the bright point. OpenCV official docs have details with code examples here. Let’s find the truck on the road.

import cv2

import numpy as np

import matplotlib.image as mpimg from matplotlib import pyplot as plt

%matplotlib inline image_color = cv2.imread('actual_truck.jpg')

plt.imshow(image_color)

We read the image from the file. We are going to locate the truck in this image.

Original Image Source: Photo by Ernesto Leon on Unsplash

Image height and width

Convert Image to Gray Scale

The reason to use grayscale is to make an image as simple as possible. There is no need for a multicolored image. Colors add complexity to the image and increase the signal-to-noise ratio.

image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY)

plt.imshow(image_gray, cmap = 'gray')

Creating a template image

This is our template image. OpenCV usage this image collects the feature and locates the truck.

import cv2

import numpy as np

import matplotlib.image as mpimg

from matplotlib import pyplot as plt

%matplotlib inline image_color = cv2.imread('sample_truck.jpg')

x= 235

y = 350

h = 200

w = 150

cropped=image_color[y:y+h, x:x+w]

plt.imshow(cropped)

status = cv2.imwrite('t.jpg', cropped)

print("Image written to file-system : ",status)

Performing template matching

# Perform template matching using OpneCV

result = cv2.matchTemplate(image_gray, template, cv2.TM_CCOEFF_NORMED)

print(result.shape)

plt.imshow(result)

Locate truck

min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

top_left = max_loc

bottom_right = (top_left[0] + w, top_left[1] + h)

cv2.rectangle(image_color, top_left, bottom_right, (10,10,255), 5)

plt.imshow(image_color)

Conclusion

In this article, we cover what image processing and its application are. We discuss features and some bad feature examples. Choosing a bad feature will result in a false positive, or true negative. Then we discuss about cv2.matchTemplate(). Later we use template matching to identify similar trucks on the road.