Change language

# Python | Two-way filtration

|

Double-sided filter is used to smooth images and reduce noise while preserving edges . This article — using a mean -median-mode-in-python-without-libraries/">median filter. However, these convolutions often result in the loss of important border information as they blur everything, whether it is noise or border. To counter this problem, a nonlinear bidirectional filter was introduced.

## Gaussian Blur

Gaussian blur can be formulated as follows:

Here, is the result in the p pixel, and the RHS is essentially the sum of all the q pixels weighted by Gaussian functions.  pixel intensity q .

A two-way filter can be formulated as follows:

Here normalization factor and range weight are new terms added to the previous equation.  indicates the spatial extent of the kernel, that is, the size of the neighborhood, and indicates the minimum edge amplitude. This ensures that only pixels with intensity values ​​similar to those of the center pixel are counted for blur, while maintaining abrupt intensity changes. The smaller the value the sharper the edges. As tends to infinity, the equation tends to Gaussian blur.

OpenCV has a function two-way filtering with the following arguments:

1. d: the diameter of each pixel neighborhood.
2. sigmaColor: value in color space. The higher the value, the more colors will blend together.
3. sigmaColor: value in coordinate space. The higher the value, the more pixels will blend together, given that their colors are within the sigmaColor range.

Code:
Input: noisy image.

Code: Implement Two-Way Filtering

 ` import ` ` cv2 `   ` # Read the image. ` ` img ` ` = ` ` cv2.imread (` ` ’taj.jpg’ ` `) ` ` `  ` # Apply a two-sided filter with d = 15, ` ` # sigmaColor = sigmaSpace = 75. ` ` bilateral ` ` = ` ` cv2.bilateralFilter (img, ` ` 15 ` `, ` ` 75 ` `, ` ` 75 ` `) `   ` # Save the output. ` ` cv2.imwrite (` ` ’taj_bilateral.jpg’ ` `, bilateral) `

Bilateral filter output

Comparison with mean and mean -median-mode-in-python-without-libraries/">median filters
Below is the output of the mean filter (` cv2.blur (img, (5, 5)) `).

Below is the output of the mean -median-mode-in-python-without-libraries/">median filter (` cv2.mean -median-mode-in-python-without-libraries/">medianBlur (img, 5) `).

Below is the result of the Gaussian filter (` cv2. GaussianBlur (img, (5, 5), 0) `).

It is easy to see that all these noise canceling filters are contaminating the edges, while Bilateral filtering keeps them.

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries