[OpenCV complete routine] 83 Frequency domain low-pass filtering case: printed text character repair
Welcome to pay attention "100 complete OpenCV routines" Series, continuously updating
Welcome to pay attention "Python Xiaobai's OpenCV learning course" Series, continuously updating
3.5 low pass filtering in frequency domain: character restoration of printed text
Low pass filtering technology is mainly used in printing and publishing industry. This section presents the practical application of low-pass filtering in frequency domain: character restoration of printed text. The case comes from figure 4.48 of P194 of Gonzalez's digital image processing (Fourth Edition).
The original image is a sample of a low resolution text image. We will encounter such text in fax, copy or historical documents. The text in the picture does not have stains, creases and tears, but the characters in the text are distorted due to insufficient resolution, many of which are broken. One way to solve this problem is to bridge these small gaps by blurring the input image.
The routine shows how to use different D 0 D_0 The repair effect of D0 # Gaussian low-pass filter after simple processing. Of course, this is only one step of repair. Next, other processing, such as threshold processing and thinning processing, will be carried out. These methods will be discussed in the follow-up content.
In order to facilitate future use, the routine encapsulates the optimal extended fast Fourier transform and Gaussian low-pass filter as functions.
Routine 8.22: GLPF low-pass filtering case: text character recognition
# OpenCVdemo08.py # Demo08 of OpenCV # 8. Image frequency domain filtering # Copyright 2021 Youcans, XUPT # Crated: 2021-12-15 # 8.22: GLPF low-pass filtering case: text character recognition def gaussLowPassFilter(shape, radius=10): # Gaussian low pass 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 = 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 # (1) Read original image imgGray = cv2.imread("../images/Fig0419a.tif", flags=0) # flags=0 read as grayscale image rows, cols = imgGray.shape[:2] # The height and width of the picture plt.figure(figsize=(10, 6)) plt.subplot(2, 3, 1), plt.title("Original"), plt.axis('off'), plt.imshow(imgGray, cmap='gray') # (2) Fast Fourier transform dftImage = dft2Image(imgGray) # Fast Fourier transform (rPad, cPad, 2) rPadded, cPadded = dftImage.shape[:2] # Fast Fourier transform size, original image size optimization print("dftImage.shape:{}".format(dftImage.shape)) D0 = [10, 30, 60, 90, 120] # radius for k in range(5): # (3) Construct Gaussian low pass filter lpFilter = gaussLowPassFilter((rPadded, cPadded), radius=D0[k]) # (5) Modify Fourier transform in frequency domain: Fourier transform point multiplication low-pass filter dftLPfilter = np.zeros(dftImage.shape, dftImage.dtype) # Size of fast Fourier transform (optimized size) for j in range(2): dftLPfilter[:rPadded, :cPadded, j] = dftImage[:rPadded, :cPadded, j] * lpFilter # (6) The inverse Fourier transform is performed on the low-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(dftLPfilter, idft, cv2.DFT_REAL_OUTPUT + cv2.DFT_INVERSE + cv2.DFT_SCALE) # (7) 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) # (8) 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] imgLPF = result.astype(np.uint8) imgLPF = imgLPF[:rows, :cols] plt.subplot(2,3,k+2), plt.title("GLPF rebuild(n={})".format(D0[k])), plt.axis('off') plt.imshow(imgLPF, cmap='gray') print("image.shape:{}".format(imgGray.shape)) print("lpFilter.shape:{}".format(lpFilter.shape)) print("dftImage.shape:{}".format(dftImage.shape)) plt.tight_layout() plt.show()
(end of this section)
Copyright notice:
youcans@xupt Original works, reprint must be marked with the original link: [OpenCV complete routine] 82 Butterworth low pass filter in frequency domain
Copyright 2021 youcans, XUPT
Crated: 2022-1-25
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 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