Convolutions using Python?


Image recognition used to be done using much simpler methods such as linear regression and comparison of similarities. The results were obviously not very good, even the simple task of recognizing hand-written alphabets proved difficult. Convolution neural networks (CNNs) are supposed to be a step up from what we traditionally do by offering a computationally cheap method of loosely simulating the neural activities of a human brain when it perceives images.

Convolutional neural network overview

Very similar to how we recognise different objects, computer algorithms need to go through millions of images before it is able to generalize the input and make judgement on image it has never seen before.

Computers see every image as 2D arrays of numbers, known as pixels. To identify objects in images, computer use special kind of Artificial Neural Network: a Convolutional Neural Network (CNN). CNN comes from, one of the most important operations in the network: convolution.

In convolutional neural network, each layers are organised into width, height and depth. Convolution which is the main part of CNNs, combine two functions to create a third function(merge two sets of information). It means consider an (image) convolution where we simply multiply two matrices element-wise followed by a sum.

A convolution is a three step procedure in image processing −

  • We take the input image.

  • Kernel matrix that we are going to apply to the input image.

  • And the final image to store the output of the input image convolved with the kernel.

Below is small implementation of sobel filter using opencv which is an image processing filter for edge detection.

Example Code

import cv2
import numpy as np

img = cv2.imread('beach.jpg')
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY).astype(float)

edge_x = cv2.Sobel(img,cv2.CV_64F,1,0,ksize = 3)
edge_y = cv2.Sobel(img,cv2.CV_64F,0,1,ksize = 3)
edge = np.sqrt(edge_x ** 2 + edge_y ** 2)

# image can be normalized to
# fit into 0..255 color space
cv2.imwrite('beach_convol.jpg', edge)

Input Image

Output Image

Updated on: 30-Jul-2019

202 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements