Python implements notch filter, low-pass filter, Gaussian filter, Butterworth filter

In an image, its low-frequency component corresponds to the part of the image that changes slowly, corresponding to the general appearance and outline of the image, while its high-frequency component corresponds to the part of the image that changes rapidly, corresponding to the details of the image (the noise of the image is also a high-frequency component).

low pass filter

The low-frequency filter, as the name suggests, is to filter out or greatly attenuate the high-frequency components of the image and let the low-frequency components of the image pass. The low-frequency filter can smooth the image and remove the noise of the image, while the high-frequency filter, on the contrary, filters the low-frequency components, and through the high-frequency components, the purpose of sharpening the image can be achieved.
An ideal low-pass filter has very sharp filtering, while a Gaussian low-pass filter has very smooth filtering. The Butterworth low-pass filter is between the two. When the order of the Butterworth low-pass filter is high, it is close to the ideal low-pass filter, and when the order is low, it is close to the Gaussian low-pass filter.

The ideal low-pass filter passes all frequencies in a circle with the origin as the center and D0 as the radius, and cuts off all frequencies outside the circle. (The center of the circle has the lowest frequency and is the direct current (dc) component of the transformation).
Example: pandas is a NumPy-based tool created to solve data analysis tasks.

high pass filter

The low-frequency part close to the center of the spectrogram is discarded by the high-pass filter, and the high-frequency part far away from the center of the spectrogram is retained. Object boundaries are usually preserved. In fact, the sharpening of the image can often be achieved by using a high-pass filter, because the boundary needs to be strengthened when sharpening, and the boundary part is the high-frequency component. Set lh to 1 to represent the high-pass filter
insert image description here

notch filter

The reason why frequency-domain techniques are feasible for filtering out periodic noise is that periodic noise appears in the form of concentrated energy pulses at frequencies corresponding to periodic disturbances. One of the methods of filtering is selective filters (band stop, band pass, and notch). Images may contain noise during generation, transmission, or acquisition, and denoising is a commonly used method in image processing. Usually, filtering methods are used to remove noise, such as median filtering and mean filtering. However, such an algorithm is not suitable for processing long and narrow images such as characters, because the pixels of the characters themselves are likely to be removed during the filtering process.
A notch filter blocks or passes frequencies in the neighborhood of a pre-defined center frequency. Due to the symmetry of the Fourier transform, the notch filter must appear in a symmetrical form about the origin (if the notch filter is located at the origin the notch filter is itself). Likewise, it is also possible to obtain a notch bandpass filter corresponding to a notch bandpass filter rather than a notch filter where the frequencies contained in the notch region are known.

Each filter experiment

1. Ideal low-pass filter

code show as below:

def low_pass_filter(img, radius=80):
    rows, cols = img.shape
    center = int(rows/2), int(cols/2)

    mask = np.zeros((rows, cols, 2), np.uint8)
    x, y = np.ogrid[:rows, :cols]
    mask_area = (x-center[0])**2+(y-center[1])**2 <= radius*radius
    mask[mask_area] = 1
    return mask

insert image description here

2. Gaussian filter

code show as below:

def gaus_filter(img, radius=80):
    rows, cols = img.shape
    center = int(rows/2), int(cols/2)

    mask = np.zeros((rows, cols, 2), np.float32)
    for i in range(rows):
        for j in range(cols):
            dist = (i-center[0])**2+(j-center[1])**2
            mask[i, j] = np.exp(-0.5*dist/(radius**2))
    return mask

insert image description here

3. Butterworth filter

code show as below:

def bw_filter(img, radius=80, n=2):
    rows, cols = img.shape
    center = int(rows / 2), int(cols / 2)

    mask = np.zeros((rows, cols, 2), np.float32)
    for i in range(rows):
        for j in range(cols):
            dist = (i - center[0]) ** 2 + (j - center[1]) ** 2
            mask[i, j] = 1/(1+(dist/radius)**(n/2))
    return mask

---

4. Notch filter

code show as below:

def notch_filter(img, h, w):
    rows, cols = img.shape
    center = int(rows / 2), int(cols / 2)

    mask = np.zeros((rows, cols, 2), np.float32)
    for u in range(rows):
        for v in range(cols):
            mask[u,v]=0
    for u in range(rows):
        for v in range(cols):
            if abs(u - center[0]) < h and abs(v - center[1]) < w:
                mask[u, v] = 1

    return mask

---

Guess you like

Origin blog.csdn.net/qq_48068259/article/details/127096263