[OpenCV complete routine] 86 Application of frequency domain filtering: fingerprint image processing

Posted by phpian on Tue, 01 Feb 2022 01:30:48 +0100

[OpenCV complete routine] 86 Application of frequency domain filtering: fingerprint image processing

Welcome to pay attention "100 complete OpenCV routines" Series, continuously updating
Welcome to pay attention "Python Xiaobai's OpenCV learning course" Series, continuously updating


4. High pass filter in frequency domain

Image edge and other sharp changes of gray level are related to high-frequency components, so image sharpening can be realized by high pass filtering in frequency domain. High pass filtering attenuates the low-frequency components in the Fourier transform without interfering with the high-frequency information.

Simply, by subtracting the transfer function of the low-pass filter from 1 in the frequency domain, the corresponding high-pass filter transfer function can be obtained:
H H P ( u , v ) = 1 − H L P ( u , v ) H_{HP}(u,v) = 1- H_{LP}(u,v) HHP​(u,v)=1−HLP​(u,v)
Where, H H P ( u , v ) H_{HP}(u,v) HHP​(u,v), H L P ( u , v ) H_{LP}(u,v) HLP (u,v) represents the transfer functions of high pass filter and low-pass filter respectively.

The transfer function of Gaussian high pass filter (GHPF) is:
H ( u , v ) = 1 − e − D 2 ( u , v ) / 2 D 0 2 H(u,v)=1-e^{-D^2 (u,v)/2D_0^2} H(u,v)=1−e−D2(u,v)/2D02​

Routine 8.25: fingerprint image processing (high pass filtering + threshold processing)

(1) Optimal extended fast Fourier transform;
(2) Construct a Gaussian low pass filter;
(3) Modify Fourier transform in frequency domain: Fourier transform point multiplication Gaussian high pass filter;
(4) Performing an inverse Fourier transform on the high pass Fourier transform;
(5) Threshold processing to obtain a sharpened image.

# OpenCVdemo08.py
# Demo08 of OpenCV
# 8. Image frequency domain filtering
# Copyright 2021 Youcans, XUPT
# Crated: 2021-12-15

    # 8.25: fingerprint image processing (high pass filtering + threshold processing)
    def gaussHighPassFilter(shape, radius=10):  # Gaussian Highpass Filter 
        # Gaussian filter:# Gauss = 1/(2*pi*s2) * exp(-(x**2+y**2)/(2*s2))
        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 = 1 - np.exp(- (D ** 2) / (2 *D0**2))
        return kernel

    def dft2Image(image):  #Optimal extended fast Fourier transform
        # Centralized 2D array f (x, y) * - 1 ^ (x + y)
        mask = np.ones(image.shape)
        mask[1::2, ::2] = -1
        mask[::2, 1::2] = -1
        fImage = image * mask  # f(x,y) * (-1)^(x+y)

        # Optimal DFT expansion size
        rows, cols = image.shape[:2]  # The height and width of the original picture
        rPadded = cv2.getOptimalDFTSize(rows)  # Optimal DFT expansion size
        cPadded = cv2.getOptimalDFTSize(cols)  # For fast Fourier transform

        # Edge extension (complement 0), fast Fourier transform
        dftImage = np.zeros((rPadded, cPadded, 2), np.float32)  # Edge expansion of the original image
        dftImage[:rows, :cols, 0] = fImage  # Edge expansion, 0 on the lower and right sides
        cv2.dft(dftImage, dftImage, cv2.DFT_COMPLEX_OUTPUT)  # fast Fourier transform 
        return dftImage


    def imgHPFilter(image, D0=50):  #Image high pass filtering
        rows, cols = image.shape[:2]  # The height and width of the picture
        # fast Fourier transform 
        dftImage = dft2Image(image)  # Fast Fourier transform (rPad, cPad, 2)
        rPadded, cPadded = dftImage.shape[:2]  # Fast Fourier transform size, original image size optimization

        # Construct Gaussian low pass filter
        hpFilter = gaussHighPassFilter((rPadded, cPadded), radius=D0)  # Gaussian Highpass Filter 

        # Modify Fourier transform in frequency domain: Fourier transform point multiplication high pass filter
        dftHPfilter = np.zeros(dftImage.shape, dftImage.dtype)  # Size of fast Fourier transform (optimized size)
        for j in range(2):
            dftHPfilter[:rPadded, :cPadded, j] = dftImage[:rPadded, :cPadded, j] * hpFilter

        # The inverse Fourier transform is performed on the high pass Fourier transform and only the real part is taken
        idft = np.zeros(dftImage.shape[:2], np.float32)  # Size of fast Fourier transform (optimized size)
        cv2.dft(dftHPfilter, idft, cv2.DFT_REAL_OUTPUT + cv2.DFT_INVERSE + cv2.DFT_SCALE)

        # Centralized 2D array g (x, y) * - 1 ^ (x + y)
        mask2 = np.ones(dftImage.shape[:2])
        mask2[1::2, ::2] = -1
        mask2[::2, 1::2] = -1
        idftCen = idft * mask2  # g(x,y) * (-1)^(x+y)

        # Intercept the upper left corner, the size is equal to the input image
        result = np.clip(idftCen, 0, 255)  # Truncation function, limiting the value to [0255]
        imgHPF = result.astype(np.uint8)
        imgHPF = imgHPF[:rows, :cols]
        return imgHPF


    imgGray = cv2.imread("../images/Fig0457a.tif", flags=0)  # flags=0 read as grayscale image
    rows, cols = imgGray.shape[:2]  # The height and width of the picture
    imgHPF = imgHPFilter(imgGray, D0=50)
    imgThres = np.clip(imgHPF, 0, 1)

    plt.figure(figsize=(10, 5))
    plt.subplot(131), plt.imshow(imgGray, 'gray'), plt.title('origial'), plt.xticks([]), plt.yticks([])
    plt.subplot(132), plt.imshow(imgHPF, 'gray'), plt.title('GaussHPF'), plt.xticks([]), plt.yticks([])
    plt.subplot(133), plt.imshow(imgThres, 'gray'), plt.title('Threshold'), plt.xticks([]), plt.yticks([])
    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-1-30


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)
[complete] OpenCV 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 Image sharpening Laplacian 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

Topics: Python OpenCV Algorithm Computer Vision image processing