# Image processing - image filtering

image filtering

## Noise

Additive noise generally refers to thermal noise, shot noise, etc. their relationship with the signal is additive. Noise exists whether there is a signal or not.

Gaussian white noise includes thermal noise and shot noise. In communication channel testing and modeling, Gaussian noise is used as additive white noise to generate additive white Gaussian noise. Additive Gaussian white noise is only one kind of white noise, and Poisson white noise, etc. in the field of communication, additive Gaussian white noise refers to a noise signal whose spectrum components obey uniform distribution (i.e. white noise) and amplitude obey Gaussian distribution. It is named because of its additivity, Gaussian distribution of amplitude and white noise.

The multiplicative noise is generally caused by the imperfect channel. The relationship between them and the signal is multiplication. If the signal is in it, it will not be in it if the signal is not in it.

In general communication, additive randomness is regarded as the background noise of the system;

The multiplicative randomness is caused by the time variability (such as fading or Doppler) or nonlinearity of the system.

## spatial filtering

Spatial domain filtering can be used for nonlinear filtering, but frequency domain filtering can not be used for nonlinear filtering

|Image filtering||| |-|-|-| |Spatial | linear filtering | mean filtering| |-|-|-| ||Nonlinear filtering | median filtering| |||Bilateral filtering| |||| ||||

### Filter template

Image filtering template:

Linear average filtering:
1|0 1 0 |
-|1 1 1 |
5|0 1 0 |

Image sharpening template:

Sharpening filtering: image sharpening is generally realized through differential operation

|-1  0  1|
|-1  0  1|
|-1  0  1|

| 1  1  1|
| 0  0  0|
|-1 -1 -1|

Directional filter sobel operator

x Axis:
|-1  0  1|
|-2  0  2|
|-1  0  1|

y Axis:
|-1 -2 -1|
| 0  0  0|
| 1  2  1|

Scharr operator

x Axis:
|-3  0  3|
|-10 0 10|
|-3  0  3|

y Axis:
|-3 -10 -3|
| 0  0   0|
| 3  10  3|

### Linear spatial filtering

Linear spatial filtering refers to that the output value of a pixel is a linear combination of pixel values in the neighborhood of the pixel In linear filtering, the filter template is also called convolution template

#### Template convolution

The main steps of template convolution include the following steps,

1) Traverse the template in the image, and coincide the center of the template with the position of each pixel;

2) Multiplying each coefficient of the template by the pixel value corresponding to the template;

3) Add all the products and assign the sum result to the pixel corresponding to the center of the template

#### Continuation

The rows or columns of the template will go beyond the image, so the extension method is often used to solve the outer boundary problem. There are four common methods: zero filling, repetition, symmetry and circulation.

Zeroing is to expand the image by zeroing around the image boundary;

Repetition refers to extending the image by copying the value of the outer boundary around the image boundary; Symmetry refers to extending the image by mirroring the value of the outer boundary around the image boundary; Loop refers to extending the image as a period of two-dimensional periodic function around the image boundary.

#### Gaussian filtering

The Gaussian filter width (which determines the smoothness) is determined by the parameter σ Characterized, and σ The relationship between peace and smoothness is very simple σ The larger, the wider the frequency band of the Gaussian filter and the better the smoothness. By adjusting the smoothness parameter σ

Gaussian distribution: h(x,y)=e^-(\frac{x^2+y^2}{2a^2})

#### Bilateral filtering

A nonlinear filtering method is a compromise processing that combines the spatial proximity and pixel similarity of the image. It is a non iterative smoothing filtering method with edge preserving. With the increase of the distance between the central pixel and the gray difference, the weight coefficient of the neighborhood pixel decreases gradually

Advantages: good edge performance and good filtering of low-frequency information

Disadvantages: unable to process high-frequency information

Assume that the Gaussian function expression is as follows:

W_ij=\frac{1}{K_i}e^-\frac{(x_j-x_i)^2}{σ^2_G}

K is the normalized constant, W is the weight, and the weight is only related to the spatial distance between pixels.

Bilateral filter:

W_ij=\frac{1}{K_i}e^-\frac{(x_j-x_i)^2}{σ^2_G}e^-\frac{(I_j-I_i)^2}{σ^2_r}

#### median filtering

Median filter is a kind of statistical sorting filter. Median filter has good effect on salt and pepper noise; The filtered image has good edge information and clear edge |Statistical sorting filter||| |-|-|-| |Maximum filtering | effectively filter out pepper noise (black) | find the most bright spot and brighten the picture| |Minimum filtering | effectively filter salt noise (white) | find the darkest point and darken the picture| |Adaptive median filter | effectively filter out salt and pepper noise | passivate image and remove noise|

Operation steps:

1) Traverse the template through the image

2) Sort the gray values of pixels in the neighborhood corresponding to the template

3) Find the intermediate value and assign it to the pixel corresponding to the center of the template

### Matlab function

Matlab filter correlation functions mainly include imfilter() and fsspecial ().

imfilter() is a filter operation

Fsspecial() is to build a custom two-dimensional filter for use by the imfilter() function

#### imfilter() function

Define g = imfilter(f, w, option1, option2,...)

Of which:

f: Image to be filtered w: Filter template option1, option2: optional

The options are:

(1) Boundary item: when traversing and processing boundary elements, you need to supplement elements around the image boundary in advance

 Parameters:X--Represents a specific number, which is used by default0supplement
symmetric--Mirror boundary element
replicate--Duplicate boundary pixels
circular--Periodically fill boundary content

(2) Size item: the boundary is expanded before processing the image, which is one circle larger than the original image. This item outputs the image size Parameter: same -- the output image has the same size as the input image full -- the size of the output image is the same as that of the extended boundary image, that is, one circle larger than the original image (3) Mode item: filtering process selection Parameter: corr -- correlation filtering process conv -- convolution correlation process (4)

#### Fsspecial() function

Define H = fsspecial (type, parameters)

Of which:

Type: filter type

Parameter: average -- average template disk -- circular neighborhood average template guassian -- Gaussian template laplacian -- laplacian template log -- Gauss Laplace template prewitt--prewitt horizontal edge detection operator sobel--Sobel horizontal edge detection operator

## spectrum shaping

Image enhancement can be used. Firstly, the image is transformed from spatial domain to frequency domain through Fourier transform, frequency domain processing, and inverse Fourier transform to spatial domain

|||| |-|-|-| ||||

### Mean filtering

void meanFilter (unsigned char* corrupted, unsigned char* smooth, int width, int height)
{

memcpy ( smooth, corrupted, width*height*sizeof(unsigned char) );

for (int j=1;j<height-1;j++)
{
for (int i=1;i<width-1;i++)
{
smooth [ j*width+i ] = (    corrupted [ (j-1)*width+(i-1) ] + corrupted [ (j-1)*width+i] + corrupted [ (j-1)*width+(i+1) ] +
corrupted [ j*width+(i-1) ]     + corrupted [ j*width+i]     + corrupted [ j*width+(i+1) ] +
corrupted [ (j+1)*width+(i-1) ] + corrupted [ (j+1)*width+i] + corrupted [ (j+1)*width+(i+1) ] ) / 9;
}
}
}  

### median filtering

void medianFilter (unsigned char* corrupted, unsigned char* smooth, int width, int height)
{

memcpy ( smooth, corrupted, width*height*sizeof(unsigned char) );
for (int j=1;j<height-1;j++)
{
for (int i=1;i<width-1;i++)
{
int k = 0;
unsigned char window[9];
for (int jj = j - 1; jj < j + 2; ++jj)
for (int ii = i - 1; ii < i + 2; ++ii)
window[k++] = corrupted[jj * width + ii];
//   Order elements (only half of them)
for (int m = 0; m < 5; ++m)
{
int min = m;
for (int n = m + 1; n < 9; ++n)
if (window[n] < window[min])
min = n;
//   Put found minimum element in its place
unsigned char temp = window[m];
window[m] = window[min];
window[min] = temp;
}
smooth[ j*width+i ] = window[4];
}
}
} 

Added by Megienos on Fri, 14 Jan 2022 03:41:30 +0200