[OpenCV complete routine] 90 Frequency domain notch filter
Welcome to pay attention "100 complete OpenCV routines" Series, continuously updating
Welcome to pay attention "Python Xiaobai's OpenCV learning course" Series, continuously updating
5.2 Notch Filter
Notch filter blocks or passes through the frequency in the rectangular neighborhood of the predetermined frequency, which is an important selective filter.
Notch filter is a filter that can rapidly attenuate the input signal at a certain frequency point to achieve the filtering effect of hindering the passage of this frequency signal. Notch filter is a kind of band stop filter. Its stop band is very narrow, and the starting order must be more than second order (including second order).
The zero phase shift filter must give symmetry to the origin (the center of the frequency rectangle), so the center is ( u 0 , v 0 ) (u_0,v_0) The notch filter transfer function of (u0, v0) is ( − u 0 , − v 0 ) (-u_0, -v_0) (− u0, − v0) position must have a corresponding notch.
The transfer function of the notch band stop filter can be constructed by the product of the high pass filter whose center has been shifted to the center of the notch filter:
H
N
R
(
u
,
v
)
=
∏
k
=
1
Q
H
k
(
u
,
v
)
H
−
k
(
u
,
v
)
H_{NR}(u,v) = \prod_{k=1}^Q H_k(u,v) H_{-k}(u,v)
HNR(u,v)=k=1∏QHk(u,v)H−k(u,v)
The distance calculation formula of the filter is:
D
k
(
u
,
v
)
=
(
u
−
M
/
2
−
u
k
)
2
+
(
v
−
N
/
2
−
v
k
)
2
D
−
k
(
u
,
v
)
=
(
u
−
M
/
2
+
u
k
)
2
+
(
v
−
N
/
2
+
v
k
)
2
D_k(u,v) = \sqrt{(u-M/2-u_k)^2 + (v-N/2-v_k)^2} \\ D_{-k}(u,v) = \sqrt{(u-M/2+u_k)^2 + (v-N/2+v_k)^2}
Dk(u,v)=(u−M/2−uk)2+(v−N/2−vk)2
D−k(u,v)=(u−M/2+uk)2+(v−N/2+vk)2
Therefore, the third-order Butterworth notch band stop filter is:
H
N
R
(
u
,
v
)
=
∏
k
=
1
3
[
1
1
+
[
D
0
k
/
D
k
(
u
,
v
)
]
n
]
[
1
1
+
[
D
−
k
/
D
k
(
u
,
v
)
]
n
]
H_{NR}(u,v) = \prod_{k=1}^3 [\frac{1}{1+[D_{0k}/D_k(u,v)]^n}] [\frac{1}{1+[D_{-k}/D_k(u,v)]^n}]
HNR(u,v)=k=1∏3[1+[D0k/Dk(u,v)]n1][1+[D−k/Dk(u,v)]n1]
The transfer function of notch bandpass filter can be obtained by Notch bandstop filter:
H
N
P
(
u
,
v
)
=
1
−
H
N
R
(
u
,
v
)
H_{NP}(u,v) = 1 - H_{NR}(u,v)
HNP(u,v)=1−HNR(u,v)
Routine 8.29 uses notch filtering to delete moire patterns in digital printed images
This example uses notch filtering to reduce the moire ripple in the digital printed image.
Moire pattern is the expression of beat difference principle. After the superposition of two equal amplitude sine waves with similar frequencies, the signal amplitude will change according to the difference between the two frequencies. If the spatial frequency of the pixel of the photosensitive element is close to the spatial frequency of the stripe in the image, the moire will be generated.
Notch filtering is to selectively modify the local region of DFT. The typical processing method is interactive operation. Directly use the mouse to select the rectangular region in the Fourier spectrum and find the maximum point as (uk,vk). In order to simplify the program, this routine deletes the mouse interaction part and only retains the filtering process after (uk,vk).
# OpenCVdemo08.py # Demo08 of OpenCV # 8. Image frequency domain filtering # Copyright 2021 Youcans, XUPT # Crated: 2021-12-30 # Routine 8.29 uses notch filtering to delete moire patterns in digital printed images def gaussLowPassFilter(shape, radius=10): # Gaussian low pass filter u, v = np.mgrid[-1:1:2.0 / shape[0], -1:1:2.0 / shape[1]] D = np.sqrt(u ** 2 + v ** 2) D0 = radius / shape[0] kernel = np.exp(- (D ** 2) / (2 * D0 ** 2)) return kernel def butterworthNRFilter(shape, radius=9, uk=60, vk=80, n=2): # Butterworth notch band stop filter M, N = shape[1], shape[0] u, v = np.meshgrid(np.arange(M), np.arange(N)) Dm = np.sqrt((u - M // 2 - uk) ** 2 + (v - N // 2 - vk) ** 2) Dp = np.sqrt((u - M // 2 + uk) ** 2 + (v - N // 2 + vk) ** 2) D0 = radius n2 = n * 2 kernel = (1 / (1 + (D0 / (Dm + 1e-6)) ** n2)) * (1 / (1 + (D0 / (Dp + 1e-6)) ** n2)) return kernel def imgFrequencyFilter(img, lpTyper="GaussLP", radius=10): normalize = lambda x: (x - x.min()) / (x.max() - x.min() + 1e-8) # (1) Edge fill imgPad = np.pad(img, ((0, img.shape[0]), (0, img.shape[1])), mode="reflect") # (2) Centralization: F (x, y) * - 1 ^ (x + y) mask = np.ones(imgPad.shape) mask[1::2, ::2] = -1 mask[::2, 1::2] = -1 imgPadCen = imgPad * mask # (3) Fourier transform fft = np.fft.fft2(imgPadCen) # (4) Construct the frequency domain filter transfer function: if lpTyper == "GaussLP": print(lpTyper) freFilter = gaussLowPassFilter(imgPad.shape, radius=60) elif lpTyper == "GaussHP": freFilter = gaussLowPassFilter(imgPad.shape, radius=60) elif lpTyper == "ButterworthNR": print(lpTyper) freFilter = butterworthNRFilter(imgPad.shape, radius=9, uk=60, vk=80, n=2) # Butterworth notch band stop filter elif lpTyper == "MButterworthNR": print(lpTyper) BNRF1 = butterworthNRFilter(imgPad.shape, radius=9, uk=60, vk=80, n=2) # Butterworth notch band stop filter BNRF2 = butterworthNRFilter(imgPad.shape, radius=9, uk=-60, vk=80, n=2) BNRF3 = butterworthNRFilter(imgPad.shape, radius=9, uk=60, vk=160, n=2) BNRF4 = butterworthNRFilter(imgPad.shape, radius=9, uk=-60, vk=160, n=2) freFilter = BNRF1 * BNRF2 * BNRF3 * BNRF4 else: print("Error of unknown filter") return -1 # (5) Modify Fourier transform in frequency domain: Fourier transform point multiplication filter transfer function freTrans = fft * freFilter # (6) Inverse Fourier transform ifft = np.fft.ifft2(freTrans) # (7) Decentralized inverse transform image M, N = img.shape[:2] mask2 = np.ones(imgPad.shape) mask2[1::2, ::2] = -1 mask2[::2, 1::2] = -1 ifftCenPad = ifft.real * mask2 # (8) Intercept the upper left corner, the size is equal to the input image imgFilter = ifftCenPad[:M, :N] imgFilter = np.clip(imgFilter, 0, imgFilter.max()) imgFilter = np.uint8(normalize(imgFilter) * 255) return imgFilter # Using notch filtering to delete moire patterns in digital printed images # (1) Read original image img = cv2.imread("../images/Fig0464a.tif", flags=0) # flags=0 read as grayscale image fig = plt.figure(figsize=(10, 5)) plt.subplot(141), plt.title("Original"), plt.axis('off'), plt.imshow(img, cmap='gray') # (2) Image Gaussian low pass filtering imgGLPF = imgFrequencyFilter(img, lpTyper="GaussLP", radius=30) # Image Gaussian low pass filtering plt.subplot(142), plt.title("GaussLP filter"), plt.axis('off'), plt.imshow(imgGLPF, cmap='gray') # (3) Image Butterworth notch band stop filtering imgBNRF = imgFrequencyFilter(img, lpTyper="ButterworthNR", radius=9) plt.subplot(143), plt.title("ButterworthNR filter"), plt.axis('off'), plt.imshow(imgBNRF, cmap='gray') # (4) Superimposed Butterworth notch band stop filtering imgSBNRF = imgFrequencyFilter(img, lpTyper="MButterworthNR", radius=9) plt.subplot(144), plt.title("Superimposed BNRF"), plt.axis('off'), plt.imshow(imgSBNRF, cmap='gray') plt.tight_layout() plt.show()
(end of this section)
Copyright notice:
youcans@xupt Original works, reprint must be marked with the original link
Copyright 2021 youcans, XUPT
Crated: 2022-2-1
Welcome to pay attention "100 complete OpenCV routines" Series, continuously updating
Welcome to pay attention "Python Xiaobai's OpenCV learning course" Series, continuously updating
[OpenCV complete routine] 01 Image reading (cv2.imread)
[OpenCV complete routine] 02 Image saving (cv2.imwrite)
[OpenCV complete routine] 03 Image display (cv2.imshow)
[OpenCV complete routine] 04 Displaying images with matplotlib (plt.imshow)
[OpenCV complete routine] 05 Image properties (np.shape)
[OpenCV complete routine] 06 Pixel editing (img.itemset)
[OpenCV complete routine] 07 Image creation (np.zeros)
[OpenCV complete routine] 08 Copy of image (np.copy)
[OpenCV complete routine] 09 Image clipping (cv2.selectROI)
[OpenCV complete routine] 10 Image mosaic (np.hstack)
[OpenCV complete routine] 11 Split of image channel (cv2.split)
[OpenCV complete routine] 12 Merging of image channels (cv2.merge)
[OpenCV complete routine] 13 Image addition (cv2.add)
[OpenCV complete routine] 14 Image and scalar addition (cv2.add)
[OpenCV complete routine] 15 Weighted addition of images (cv2.addWeight)
[OpenCV complete routine] 16 Image addition of different sizes
[OpenCV complete routine] 17 Gradient switching between two images
[OpenCV complete routine] 18 Mask addition of image
[OpenCV complete routine] 19 Circular mask of image
[OpenCV complete routine] 20 Bitwise operation of image
[OpenCV complete routine] 21 Image overlay
[OpenCV complete routine] 22 Add non Chinese text to the image
[OpenCV complete routine] 23 Add Chinese text to image
[OpenCV complete routine] 23 Add Chinese text to image
[OpenCV complete routine] 24 Affine transformation of image
[OpenCV complete routine] 25 Image Translation
[OpenCV complete routine] 26 Rotation of the image (centered on the origin)
[OpenCV complete routine] 27 Rotation of the image (centered on any point)
[OpenCV complete routine] 28 Image rotation (right angle rotation)
[OpenCV complete routine] 29 Image flip (cv2.flip)
[OpenCV complete routine] 30 Zoom of image (cv2.resize)
[OpenCV complete routine] 31 Image pyramid (cv2.pyrDown)
[OpenCV complete routine] 32 Image twist (stagger)
[OpenCV complete routine] 33 Composite transformation of image
[OpenCV complete routine] 34 Projection transformation of image
[OpenCV complete routine] 35 Projection transformation of image (boundary filling)
[OpenCV complete routine] 36 Conversion between rectangular coordinates and polar coordinates
[OpenCV complete routine] 37 Gray processing and binary processing of image
[OpenCV complete routine] 38 Inverse color transformation of image (image inversion)
[OpenCV complete routine] 39 Linear transformation of image gray
[OpenCV complete routine] 40 Image piecewise linear gray scale transformation
[OpenCV complete routine] 41 Gray level transformation of image (gray level layering)
[OpenCV complete routine] 42 Gray level transformation of image (bit plane layering)
[OpenCV complete routine] 43 Gray scale transformation of image (logarithmic transformation)
[OpenCV complete routine] 44 Gray scale transformation of image (gamma transformation)
[OpenCV complete routine] 45 Gray histogram of image
[OpenCV complete routine] 46 Histogram equalization
[OpenCV complete routine] 47 Image enhancement histogram matching
[OpenCV complete routine] 48 Image enhancement - color histogram matching
[OpenCV complete routine] 49 Image enhancement - local histogram processing
[OpenCV complete routine] 50 Image enhancement - histogram statistics image enhancement
[OpenCV complete routine] 51 Image enhancement histogram backtracking
[OpenCV complete routine] 52 Image correlation and convolution
[OpenCV complete routine] 53 SciPy realizes two-dimensional image convolution
[opencv complete routine] 54 OpenCV to realize image two-dimensional convolution
[OpenCV complete routine] 55 Separable convolution kernel
[OpenCV complete routine] 56 Low pass box filter
[OpenCV complete routine] 57 Low pass Gaussian filter
[OpenCV complete routine] 58 Nonlinear filtering median filtering
[OpenCV complete routine] 59 Nonlinear filtering bilateral filtering
[OpenCV complete routine] 60 Nonlinear filtering - joint bilateral filtering
[OpenCV complete routine] 61 Guided filter
[OpenCV complete routine] 62 Image sharpening - passivation masking
[OpenCV complete routine] 63 Laplacian image sharpening operator
[OpenCV complete routine] 64 Image sharpening -- Sobel operator
[OpenCV complete routine] 65 Image sharpening -- Scharr operator
[OpenCV complete routine] 66 Low pass / high pass / band stop / band pass of image filtering
[OpenCV complete routine] 67 Comprehensive application of image enhancement in spatial domain
[OpenCV complete routine] 68 Comprehensive application of image enhancement in spatial domain
[OpenCV complete routine] 69 Fourier coefficients of continuous aperiodic signals
[OpenCV complete routine] 70 Fourier transform of one-dimensional continuous function
[OpenCV complete routine] 71 Sampling of continuous functions
[OpenCV complete routine] 72 One dimensional discrete Fourier transform
[OpenCV complete routine] 73 Two dimensional continuous Fourier transform
[OpenCV complete routine] 74 Anti aliasing of image
[OpenCV complete routine] 75 Implementation of image Fourier transform with numpy
[OpenCV complete routine] 76 OpenCV realizes image Fourier transform
[OpenCV complete routine] 77 OpenCV implements fast Fourier transform
[OpenCV complete routine] 78 Fundamentals of image filtering in frequency domain
[OpenCV complete routine] 79 Basic steps of image filtering in frequency domain
[OpenCV complete routine] 80 Detailed steps of image filtering in frequency domain
[OpenCV complete routine] 81 Gaussian low pass filter in frequency domain
[OpenCV complete routine] 82 Butterworth low pass filter in frequency domain
[OpenCV complete routine] 83 Low pass filtering in frequency domain: character restoration of printed text
[OpenCV complete routine] 84 The high pass filter is obtained from the low pass filter
[OpenCV complete routine] 85 Application of high pass filter in frequency domain
[OpenCV complete routine] 86 Application of frequency domain filtering: fingerprint image processing
[OpenCV complete routine] 87 Frequency domain passivation masking
[OpenCV complete routine] 88 Laplace high pass filter in frequency domain
[OpenCV complete routine] 89 Transfer function of band stop filter
[OpenCV complete routine] 90 Frequency domain notch filter