CONVOLUTION: KERNALS FOR FEATURE EXTRACTION IN IMAGE PROCESSING

An image as simple as 200x200 is too much information for a Nueral network or a computer to understand. The machine learning algorithm like logic regression cannot process the 200x200 information of image, even if it does, the results may not be favourable at all.

The accuracy of Nueral Network totally depend upon the input we've given it. So in machine learning the NN's preprocess the input and extract necessary information from the input. Something advance such as CONVOLUTION NUERAL NETWORK(CNN) perform feature extraction.

In order to extract feature from an image we need convolution in image processing. Convolution is process in signal processing which is used to combine 2 signals. More about convolution in here.

Here the convolution in computer vision is for feature extraction. We extract important details from image like Edges,noise removal,etc...

The extraction of important data can be achieved by using a KERNAL. The kernal method is used for sharping,embrose,smoothing,high frequency removal,etc...

The kernal is an array consisting a 3x3 values mostly...The values vary depends upon our target achievement. Different applications uses different types of kernals. 

For example in this figure the kernal is used to extract the feature of An outline kernel (also called an "edge" kernel) is used to highlight large differences in pixel values. A pixel next to neighbor pixels with close to the same intensity will appear black in the new image while one next to neighbor pixels that differ strongly will appear white.
To see the different kernal and how it affect the image information can be found More about here.
In this post we are gonna implement our own kernal to an image using python and opencv.
The KERNAL also decreases the size of the image size keeping only important details only. 
For ex. if a image is 20x20 then after matrix multiplication of kernal we get 18x18 of an image. The kernal can be used to DATA POOLING.
In this example i'm implementing a new filter(Kernal) to get only edges and pic info only
depending on the kernal type the image output depends. Here my kernal changes a picture into small dots and keeps only a face edges & high details rest is converted into dots.
code:
import cv2
import numpy as np


def convolution_filter(gray_img, kernel):

    kernel_size = len(kernel)

    row = gray_img.shape[0] - kernel_size + 1
    col = gray_img.shape[1] - kernel_size + 1

    result = np.zeros(shape=(row, col))

    for i in range(row):
        for j in range(col):
            current = gray_img[i:i+kernel_size, j:j+kernel_size]
            multiplication = np.abs(np.sum(np.sum(current * kernel)))
            result[i, j] = multiplication

    return result


gray = cv2.imread("test.jpg",0)



k = [[0, -1, 0], [-1, 4, -1], [0, -1, 0]]

filtered = convolution_filter(gray, k)

cv2.imshow("in",gray)
cv2.imshow("out",filtered)
cv2.waitKey(0)

OUTPUT:

Comments

Popular posts from this blog

HACK AND CLONE CAR KEY FOB, MY EPIC FAILURE...!!

HACK HC-05(BLUETOOTH MODULE) FRIMWARE INTO HID FRIMWARE

secrets of arduino atmega328p adc