# Laplacian filtering and combining of the methods

**Author: Uzhva Denis Romanovich**

**Lecturer: Soloviev Igor Pavlovich**

## Laplacian filtering
### Theory

Laplacian filtering is a type of spatial filtering aiming to sharpen an image.
Unlike box or weighted average filtering, which use the concept of an integral, laplacian is based on a derivatives.
Concerning the fact that we process discrete images, the derivatives thus become differences.
E.g., consider the 2-dimensional laplacian operator:
$$
\begin{equation}
\nabla^2 = \frac{\partial^2}{\partial x^2} + \frac{\partial^2}{\partial y^2}.
\tag{1}
\end{equation}
$$
Let $A \in \overline{0, 255}^{M \times N}$ be an $M \times N$ matrix of the image, containing values of intensity of corresponding pixels.
Let also $\widehat{A} \in \overline{0, 255}^{M + 2a \times N + 2b}$ be a padded $A$, i.e. $\widehat{A}_{i+a, j + a} = A_{i, j}$.
Then the discrete form of the laplacian would be defined as follows:
$$
\begin{equation}
\nabla^2 \widehat{A}_{i, j} = \widehat{A}_{i+1, j} + \widehat{A}_{i, j+1} + \widehat{A}_{i-1, j} + \widehat{A}_{i, j-1} - 4 \widehat{A}_{i, j},
\tag{2}
\end{equation}
$$
where $i \in \overline{2, M-1}$ and $j \in \overline{2, N-1}$.
Another definition is the following:
$$
\begin{equation}
\nabla^2 \widehat{A}_{i, j} = \widehat{A}_{i+1, j} + \widehat{A}_{i, j+1} + \widehat{A}_{i-1, j} + \widehat{A}_{i, j-1} + \widehat{A}_{i+1, j+1} + \widehat{A}_{i-1, j+1} + \widehat{A}_{i-1, j+1} + \widehat{A}_{i-1, j-1} - 8 \widehat{A}_{i, j},
\tag{3}
\end{equation}
$$
and the main advantage of it over the equation (2) is that it considers diagonals.

Finally, the result of application of the laplacian can be expressed by the following formula:
$$
\begin{equation}
B = A + c \nabla^2 \widehat{A},
\tag{4}
\end{equation}
$$
so that the result is the original image plus the extracted edges (which we want to highlight) times constant $c$, which may vary.

### Code

#### Laplacian filter


```python
import numpy as np


def apply_lapl(img, filt, c=1.):
    h = img.shape[0]
    w = img.shape[1]
    ker_size = filt.shape[0]
    pad_size = (ker_size - 1) // 2
    
    edg_ext = np.zeros_like(img, dtype=float)
   
    img_padded = np.zeros((h + 2 * pad_size, w + 2 * pad_size, channels))
    img_padded[pad_size:-pad_size, pad_size:-pad_size, :] = img
    
    for i in range(ker_size):
        for j in range(ker_size):
            ker_multiplier = filt[i, j]
            edg_ext = img_new + ker_multiplier * img_padded[i:i+h, j:j+h]
                
    edg_ext = c * edg_ext
    
    img_new = np.zeros_like(img, dtype=float)
    img_new = img + edg_ext
    img_new = 255 * img_new / np.max(img_new)
    img_new = img_new.astype(np.uint8)
    
    return img_new, edg_ext
```

#### Make laplacian kernel


```python
import numpy as np


def get_laplacian_kernel(diag=False):
    kernel = np.zeros((3, 3))
    if diag:
        kernel[:, :] = 1
        kernel[1, 1] = -8
    else:
        kernel[1, :] = 1
        kernel[:, 1] = 1
        kernel[1, 1] = -4
            
    return kernel
```

### Results

#### Processing + visualization

First of all, we need to load images for the further processing:


```python
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image


# http://pages.cs.wisc.edu/~lizhang/projects/autoexpo/content/text.html
img_1 = Image.open('./text1.png')
# http://pages.cs.wisc.edu/~lizhang/projects/autoexpo/content/text.html
img_2 = Image.open('./text2.png')
```

63c314aba537d26c53dcae28cf624edcb76523fe
ipynb
Jupyter Notebook
Task4 - Laplacian filtering and combining of the methods/Laplacian filtering and combining of the methods.ipynb
denisuzhva/Algorithms-of-Images-Analysis-and-Classification
469dbf2adb363bb544d9c71cf9353eb87790513b
MIT
2020-05-29T15:17:02.000Z
2020-05-29T15:17:02.000Z
Task4 - Laplacian filtering and combining of the methods/Laplacian filtering and combining of the methods.ipynb
mortarsynth/Algorithms-of-Images-Analysis-and-Classification
469dbf2adb363bb544d9c71cf9353eb87790513b
MIT
Task4 - Laplacian filtering and combining of the methods/Laplacian filtering and combining of the methods.ipynb
mortarsynth/Algorithms-of-Images-Analysis-and-Classification
469dbf2adb363bb544d9c71cf9353eb87790513b
MIT
Qwen/Qwen-72B
1. YES
2. YES