Machine Learning

Image Analysis and Processing - Python OpenCV Example

An overview of image analysis and computer vision and its real world applications.

Source:PERFICIENT

1. Introduction

Image analysis, also known as “computer vision” or image recognition, is the ability of computers to recognize attributes within an image.

Most computer vision techniques are inspired by human vision perception models. The techniques of processing and analysing images using computers, which have improved over the years, however still remain challenging.

The success of most of these techniques is dependent on the quality of the input images. Normally, there is a need to pre-process the input images in order to improve the quality.

Pre-processing can include noise removal, geometric correction, edges, and contrast enhancement. This is the equivalent of data cleansing/cleaning in data science. Image processing and analysis tasks include basic manipulations such as cropping, flipping, and rotating, and more complex operations such as image segmentation, classification, feature extraction, and image recognition.

Computers “see” images as arrays of pixels. Each pixel is represented by a 1 by 3 array containing numbers that indicate the RBG profile of that pixel.

2. Types of Image Processing and Uses

a. Image Segmentation

These are processing techniques that involve partitioning an image into multiple segments. That way, we can keep only the important segments for processing and analysis. It is very useful in identifying the boundaries and contours of objects of interest in the images.

One use of image segmentation includes determination of the severity of cancer, this is based on the shape of the cancerous cells.

Fig 1 illustrates how image segmentation is used to identify cancerous cells by highlighting their boundaries and revealing their shape.

image segmentation in medical field
Fig 1: Using image segmentation to identify cancerous cells | Source: Analyticsvidhya

Other uses of segmentation include aerial and satellite image interpretations, self-driving vehicles, and fingerprint/face/iris recognition.

b. Image Recognition

These are image processing techniques that involve the identification of objects or features in an image.

Image recognition is often used in conjunction with object detection. Image recognition identifies which object is in an image, whereas object recognition identifies the location of these objects in an image.

Image recognition is widely used for quality control in the production industry, as it can be used to inspect high volumes of products in a production line.

Fig 2 below illustrates how it can be used to check the quality of products on the production line at high speeds and with high accuracy.

Fig 2: Image recognition for quality control | Source: Matlab

Other uses of image recognition include medical or biological image processing (e.g. interpretation of X-ray images, blood/cellular microscope images) and security surveillance.

It has become so important that researchers estimate that the global market of image recognition is around $38.92 billion.

c. Feature Extractions

Feature extraction describes the process of extracting relevant shape information contained in the image so that the classification of the image is made easier.

In image processing, a feature extraction is a special form of dimension reduction where the primary objective is to obtain only the most relevant information from the original image and represent that information in a lower dimensionality space.

A good set of features will contain relevant information from the input data in order to perform the desired image classification. This is applied in several fields such as character recognition, document verification, extracting information from cheques, postal address reading, and script recognition.

d. Image Classification

Image classification refers to the labeling of images into one of a number of predefined classes.

The applications include automated image organization. For example, Google Photos uses this technique to recognize faces and categorize them so that you can view all your photos of a particular person.

Image classification has become so advanced that it can be used to identify faces within photos to determine sentiment, gender, age, and more. It can recognize multiple elements within a photo at the same time, including logos, faces, activities, objects, and scenes. Captions can be automatically generated to describe the image.

Image classification highly depends on other image processing techniques in order to work. Firstly, image pre-processing needs to be done to improve image quality, and secondly, image segmentation and object detection needs to occur to identify the object and its position. This is followed by feature extraction as the features provide the classification rules. Finally, image classification can be done. This can be supervised or unsupervised and requires the use of machine learning algorithms.

3. Image Recognition Example using OpenCV in Python

In OpenCV, image recognition is performed using the template matching function. One needs to supply an image of the template image which they want to find in the larger image. The template is slid pixel by pixel onto your image and a similarity metric is computed between your template image and the larger image.

In this example, we are going to identify all instances of the Elucidate AI logo on our website’s landing page.

Firstly, import the OpenCV module and a few other modules that will be required in the image recognition.

This is followed by loading the template image (Elucidate logo) and the main image that we want to analyse (Elucidate website homepage).

The main image is greyscaled to simplify the matching by removing information on colour.

import cv2
import numpy as np
from pylab import imshow, show
from matplotlib import pyplot as plt
# Loan main image

elucidate_rgb = cv2.imread('/home/brendon/Downloads/elucidatepage.jpg')

# Convert main image to grayscale

elucidate_gray = cv2.cvtColor(elucidate_rgb, cv2_COLOR_BGR2GRAY)

# load template as gray image

template = cv2.imread('/home/brendon/Downloads/elucidatelogo.png',0)

The matchTemplate function is used to match the template to the main image.

In this example, the TM_CCOEFF_NORMED (normalized correlation coefficient) method is used for the matching.

The higher the coefficient, the more likely the match. Information on other methods can be found here. Of all the points identified as potential matches, we retain only those where the normalized coefficient is greater than the threshold of 0.8. For all the regions meeting the criteria, the x and y coordinates bordering that region are stored.

#Define a similarity threshold  that needs to be met for a pixel to be 
#considered a match

template_matching_threshold = 0.8

#template matching

template_matching = cv2.matchTemplate(template, hoodie_gray, cv2.TM_CCOEFF_NORMED)

#From the matched pixels.Keep only those that meet the threshold

matched_pixels = np.where(template_matching >= template_matching_threshold)

#store the height and width of the template image

template_width,template_height = template.shape[::-1]
#obtain the x,y coordinates for the matched pixels meeting the threshold conditions

detections = []

count = 0 #initialize the count

for (x, y) in zip(matched_pixels[1], matched_pixels[0]):

count = count +1

match = { "TOP_LEFT_X": x,
"TOP_LEFT_Y": y,
"BOTTOM_RIGHT_X": x + template_width,
"BOTTOM_RIGHT_Y": y + template_height,
"MATCH_VALUE": template_matching[y, x],
"LABEL": "MATCH_{}".format(count),
"COLOR": (255,0,0) }

detections.append(match)

Rectangles are added around the regions of interest on a copy of the main image.

#Make a copy of original image

#Plot a rectangle around the dectected match using the coordinates
#that meet threshold

image_with_detections = hoodie_rgb.copy()

for detection in detections:

cv2.rectangle(image_with_detections,
(detection["TOP_LEFT_X"], detection["TOP_LEFT_Y"]),
(detection["BOTTOM_RIGHT_X"], detection["BOTTOM_RIGHT_Y"]),
detection["COLOR"],1)

cv2.putText(image_with_detections,
f"{detection['LABEL']} ",
(detection["TOP_LEFT_X"] + 2, detection["TOP_LEFT_Y"] + 20),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
detection["COLOR"],
1,
cv2.LINE_AA,)

The final image shows the Elucidate AI logo in the top left corner, which has been identified as a match to the template and has been marked by the red rectangle.

fig = plt.figure()

fig.set_size_inches(25,25)

imshow(image_with_detections)
Fig 6: Final output

4. Conclusion

In this short read, we have looked at the importance of image processing or computer vision in the digital world that we live in. The application of computer vision is continuously growing as more organizations start recognizing the value that it brings.

Enjoyed this read?

Stay up to date with the latest AI news, strategies, and insights sent straight to your inbox!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
LET’S TRY!

Don’t wait get started now!

Build a custom website without the stress, easily customised to suit you.