Frequency Domain Filtering Using OpenCV
Tags: python, opencv, jupyter
Published: 2021-10-20 09:15:10

### What we need to get started with OpenCv...
> We need to import few libraries given below and are available in **Google Colab**, independent installations may be required for other platforms.

---
**1**. Imports required 
```python
import cv2
from google.colab.patches import cv2_imshow
import numpy as np
import matplotlib.pyplot as plt
```
---
**2**. Next we import an image and use a simple Edge Preserving Filter.

> Domain Filtering - Frequency Domain Filters are used for smoothing and sharpening of image by removal of high or low frequency components. Sometimes it is possible of removal of very high and very low frequency.

```python
#read image
src = cv2.imread(r'/content/P.png', cv2.IMREAD_UNCHANGED)
DF = cv2.edgePreservingFilter(src, flags=1, sigma_s=60, sigma_r=0.4)
print("Domain Filter - Cartoonify")
cv2_imshow(numpy.hstack((src, DF)))
```

![Cartoonify Filter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/foqv2sr6ossdganhjftp.png)

---

**3**. A- Smoothing of Image and other Domain Filters.

> Frequency Domain Filters are used for smoothing and sharpening of images by removal of high or low-frequency components.
Gaussian blur (also known as Gaussian smoothing) is the result of blurring an image by a Gaussian function.

```python
#apply guassian blur on src image
dst = cv2.GaussianBlur(src,(5,5),cv2.BORDER_DEFAULT)
print("Gaussian Smoothing")
cv2_imshow(numpy.hstack((src, dst)))
```
![Gaussian Smoothing Blur](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qh94dli1w926e6sw80rh.png)

---

Mean Filter 

```python
kernel = np.ones((10,10),np.float32)/25   
dst2 = cv2.filter2D(src,-1,kernel)
print("Mean Filter")
cv2_imshow(numpy.hstack((src, dst2)))
```

![Mean Filter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/actagrouchi2ri5pyxod.png)

---

Median Filter

```css
#Median Filter
dst3 = cv2.medianBlur(src,5)
print("Median Filter")
cv2_imshow(numpy.hstack((src, dst3)))
```

![Median Filter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnbhpzpb5y4lwqqhun56.png)

---

**4.** B- Sharpening Filter -

Bilateral filter 

```python
#Bilateral filter 
print("Bilateral Filter")
dst4 = cv2.bilateralFilter(src, 60, 60, 60) 
cv2_imshow(numpy.hstack((src, dst4)))
```

![Bilateral Filer Sharpening](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pwvragq704lumrhykb2s.png)

---

**5.** C- Frequency Band Filter 

Low Pass 

```python
#low pass filter
Lp = cv2.filter2D(src,-1, kernel)
Lp = src - Lp
print("Low Pass")
cv2_imshow(numpy.hstack((src, Lp)))
```

![Low Pass](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mx6w2k45rkxx35ya5ahy.png)

---

High Pass

```python
Hp = src - dst
filtered = filtered + 127*numpy.ones(src.shape, numpy.uint8)
print("High Pass")
cv2_imshow(numpy.hstack((src, Hp)))
```

![High Pass](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srcy2f9c55uy5i4umyy9.png)

---

Frequency filters process an image in the frequency domain. The image is Fourier transformed, multiplied with the filter function and then re-transformed into the spatial domain. Attenuating high frequencies results in a smoother image in the spatial domain, attenuating low frequencies enhances the edges.

Any frequency filter can be implemented in the spatial domain and, if there exists a simple kernel for the desired filter effect.

If you missed the first part

**LINK**
{% link https://dev.to/alcatraz714/opencv-image-processing-using-python-6ma %}

Thanks for Reading...