# Apply different Kernels

#### In Covolutional Neural Network (CNN), the images pass through different type of filters, in purpose to extract features.
#### In this simple notebook, I tried to apply different kind of kernels (filters), as an attempt to totally absorb the concept of convolution, and to know how CNN extracts its own features from images! 


```python
from PIL import Image
from numpy import array
import matplotlib.pyplot as plt
from scipy import signal
from scipy import misc
import numpy as np
import cv2
```


```python
img = cv2.imread("images/cat.jpg",0)
img.shape
```




    (190, 265)



#### This function takes the image's martix, and the kernel's as well, the convolve them using 'signal.convolve2d'


```python
def DisplayImagesBeforeAfterConvolution(image, kernel):
    plt.figure(figsize=(10,10))
    plt.subplot(121),plt.imshow(image),plt.title('Original')
    plt.xticks([]), plt.yticks([])
    
    filtered_image1 = signal.convolve2d(image,kernel)
    
    plt.subplot(122),plt.imshow(filtered_image1),plt.title('Filtered')
    plt.xticks([]), plt.yticks([])
    plt.show()
    
```

### 1- Horizontal edge detection kernel
\begin{equation}
  \begin{bmatrix}
    -1 & -2 & -1 \\
    0 & 0 & 0 \\
    1 & 2 & 1
  \end{bmatrix}
  \label{eq:aeqn}
\end{equation}


```python
kernel1 = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])

```


```python
DisplayImagesBeforeAfterConvolution(img, kernel1)
```

### 2- Vertical edge detection kernel
\begin{equation}
  \begin{bmatrix}
    -1 & 0 & 1 \\
    -2 & 0 & 2 \\
    -1 & 0 & 1
  \end{bmatrix}
  \label{eq:aeqn}
\end{equation}


```python
kernel2= np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
DisplayImagesBeforeAfterConvolution(img, kernel2)
```

### 3- Inverse 
\begin{equation}
  \begin{bmatrix}
    -1 & -1 & -1 \\
    -1 & -1 & -1 \\
    -1 & -1 & -1
  \end{bmatrix}
  \label{eq:aeqn}
\end{equation}


```python
kernel4= np.array([[-1,-1,-1],[-1,-1,-1],[-1,-1,-1]])
DisplayImagesBeforeAfterConvolution(img, kernel4)
```

### 4- Sharpness kernel
\begin{equation}
  \begin{bmatrix}
    0 & -1 & 0 \\
    -1 & 5 & -1 \\
    0 & -1 & 0
  \end{bmatrix}
  \label{eq:aeqn}
\end{equation}


```python
kernel4= np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
DisplayImagesBeforeAfterConvolution(img, kernel4)
```

### 5- Smoothness kernel
\begin{equation}
  \begin{bmatrix}
    1/9 & 1/9 & 1/9 \\
    1/9 & 1/9 & 1/9 \\
    1/9 & 1/9 & 1/9
  \end{bmatrix}
  \label{eq:aeqn}
\end{equation}


```python
kernel5= np.array([[1,1,1],[1,1,1],[1,1,1]])*1/9
DisplayImagesBeforeAfterConvolution(img, kernel5)
```

### 6- Random Karnel


```python
kernel6= np.array([[-4,5,-6],[-2,-3,14],[-6,1,-8]])*1/9
DisplayImagesBeforeAfterConvolution(img, kernel)
```


```python

```

7723cb97bcca87df68eed2fa0291f46aeaf2a3e2
ipynb
Jupyter Notebook
1-Kernels.ipynb
zuhaalfaraj/working-with-images
241a5d99a9d121535433f221b3385ead90d64879
BSD-2-Clause
2021-02-27T21:58:36.000Z
2021-02-27T21:58:36.000Z
1-Kernels.ipynb
zuhaalfaraj/working-with-images
241a5d99a9d121535433f221b3385ead90d64879
BSD-2-Clause
1-Kernels.ipynb
zuhaalfaraj/working-with-images
241a5d99a9d121535433f221b3385ead90d64879
BSD-2-Clause
Qwen/Qwen-72B
1. YES
2. YES