当前位置:网站首页>Opencv computer vision learning (10) -- image transform (Fourier transform, high pass filter, low pass filter)

Opencv computer vision learning (10) -- image transform (Fourier transform, high pass filter, low pass filter)

2020-11-07 16:50:00 itread01

If you need to process the original drawing and code , Please move to the small knitting GitHub Address

   Portal : Please choose me

   If there is a mistake in the selection :https://github.com/LeBron-Jian/ComputerVisionPractice

   In digital image processing , There are two classical transformations that are widely used —— Fourier transform and Hough transform . among , Fourier transform mainly transforms the signal in time domain into signal in frequency domain , For image noise reduction , Image enhancement, etc , This article mainly studies Fourier transform , I'm going to learn Hough transform later .

   Let's learn about Fourier transform . It is said that Fourier analysis is more than a mathematical tool , It's also a way of thinking that can completely subvert a person's previous world outlook ( It comes from ( It is highly recommended to read this article ):https://zhuanlan.zhihu.com/p/19763358)

   The function of Fourier transform :

   For discrete signals like digital images , The frequency indicates the intensity of signal transformation or the speed of signal change . The higher the frequency , The transformation is more intense , The less frequency , The slower the signal is , Corresponding to the image , High frequency signal is often the edge signal and noise signal in the image , The low frequency signal includes the image contour and background light signal with frequent image changes .

   It should be noted that : There is no one-to-one correspondence between the points on the spectrum obtained by Fourier transform and the points on the original image .

  • High frequency : Transform the intensity of gray components , For example, borders
  • Low frequency : Transform slow grayscale components , For example, a sea

   Filter :

  • Low pass filter : Keep only the low frequencies , It will blur the image
  • High frequency filter : Keep only the high frequencies , Will enhance the details of the image

   If you don't understand , You can see a picture posted in the previous article , I'll stick it up here :

The relationship between fuzzy and filtering is shown in the figure below (https://www.zhihu.com/question/54918332/answer/142137732):

1, Frequency domain and its application in frequency domain data

1.1  What is the frequency domain

   Since we were born , The world we see runs through time , The stock trend , People's height , The trajectory of a car changes over time . This method of observing the dynamic world with time as a reference is called time domain analysis . And we take it for granted that , Everything in the world is changing with time , And never stop . But we can see the world in another way , You can find that the world is immutable , It's called the static world .

   Formal definition : Frequency domain is a coordinate system used to describe the frequency characteristics of signals . In Electronics , In control systems engineering and Statistics , The frequency domain diagram shows the amount of signal in each given frequency band in the frequency range .

   Here's an example :

   What do we ordinary people understand about music ? It may be as shown in the figure below :

   The picture above is our most common understanding of music , A vibration that changes with time . But I believe for musicians , The more intuitive understanding of music is like this :

    Let's simplify the two graphs , Using Baidu Encyclopedia sine function in the time and frequency domain performance as follows :

   In conclusion , In the time domain, we observe that the strings of a piano swing up and down , The same trend as above , And in the frequency domain , It's just eternal , It's the same as above and below . And one of the methods through time and frequency domain , It's Fourier analysis . Fourier analysis can be divided into Fourier series (Fourier Serie) And Fourier transform (Fourier Transformation).

1.2  Application of frequency domain data

Image denoising

   We can process images in frequency domain as needed , For example, when it is necessary to remove the noise in the image , We can design a low-pass filter , Remove the high frequency noise from the image , But it often suppresses the edge information of the image , That's why the image is blurred . Take mean filtering as an example , Convolute the image with the mean template , Everybody knows , Convolution in space , It's equivalent to multiplication in the frequency domain , The mean template has no high frequency signal in frequency domain , There's only one constant component , So the mean template is to do low-pass filtering on the image region . besides , The common Gaussian filter is also a low-pass filter , Because Gaussian functions are Fourier transformed , The distribution in frequency domain is still Gaussian distribution , As shown in the figure below , So it has a good filtering effect on high frequency information .

 

Image enhancement and sharpening

   Image enhancement needs to enhance the details of the image , The details of the image are often the high frequency part of the image , So enhancing the high frequency information in the image can achieve the purpose of image enhancement .

   The same purpose of image sharpening is to make the blurred image clearer , The main way is to enhance the edge of the image , In fact, it is to enhance the part of the image with sharp changes in gray level , So by enhancing the high frequency information in the image, we can enhance the image edge , In order to achieve the purpose of image sharpening . It can be seen from here , The edge and texture information of the image can be obtained by extracting the high frequency signal from the image .

2, Fourier transform principle

2.1 For example, analyze Fourier transform

   Fourier transform (Fourier Transform, Abbreviation FT) Often used in digital signal processing , Its purpose is to transform signals in the time domain into signals in the frequency domain . With different domains , The angle of understanding the same thing also changes , So in the time domain, it's not easy to deal with , In the frequency domain, we can deal with it more simply . At the same time , We can find some features which are not easy to detect in the frequency domain . Fourier's theorem states that “ Any continuous periodic signal can be expressed as ( Or infinite approximation ) The superposition of a series of sinusoidal signals ”.

    The following reference “Python + OpenCV Video processing course ”( The address is given at the end of the article ) One of the cases in , He converted the time domain angle of the beverage making process into the frequency domain angle .

    Draw the corresponding time and frequency graphs as follows :

    The Fourier formula is as follows , among w Frequency ,t It means time , Is a complex function . It represents the function in time domain as function in frequency domain f(t) Integral of .

   Fourier transform considers a periodic function ( signal ) Contains multiple frequency components , Any function ( signal ) f(t) By means of multiple periodic functions ( Or base function ) Additive synthesis . To understand from a physical point of view , Fourier transform is a special set of functions ( Trigonometric function ) Is an orthogonal basis , Linear transformation of the original function , The physical meaning is the projection of the primitive function in each group of basic functions . As shown in the figure below , It's made up of three sinusoidal curves :

   Fourier transform can be applied to image processing , After the image is transformed, the spectrum is obtained . From the frequency spectrum to show the intensity of gray level change in the image . The edge signal and noise signal in the image are often high frequency signals , And the image contour and background signals with frequent image transformation are often low-frequency signals . At this time, we can carry out relevant operations on the image , For example, image denoising , Image enhancement and sharpening, etc .

   The Fourier transform of two-dimensional image can be expressed by the following mathematical formula , among f It's the spatial domain (Spatial Domain) Value , F It's the frequency domain (Frequency Domain) value

2.2  The definition of two-dimensional Fourier transform

   In fact, the two-dimensional Fourier transform has been introduced , Here, learn more about .

   First, let's look at the continuous two-dimensional Fourier transform :

   Inverse transformation of continuous two-dimensional Fourier transform :

   Let's take a look at the discrete two-dimensional Fourier transform ( When we define the image size as M*N, Then the discrete Fourier transform of the function is given by the following equation ):

   The inverse discrete Fourier transform is given by the following equation :

   Make R and I Respectively F The real part and the imaginary part of , Then the Fourier spectrum , phase angle , Power spectrum ( Range ) It's defined as :

2.3  use FFT Computing two-dimensional discrete Fourier transforms

   Two dimensional discrete Fourier transform is defined as :

   Two dimensional discrete Fourier transform can be realized by two one-dimensional discrete Fourier transform :

  (1) Do one dimension N Point DFT( For each m Do it once , common M Time )

  (2) do M Ordered DFT( For each k Do it once , common N Time )

   These two discrete Fourier transforms can be obtained by fast algorithm , if M and N All are 2 Power , We can use base two FFT Algorithm , The number of multiplications required is :

   The number of discrete Fourier transforms is the number of times needed for direct multiplication (M+N)MN, When M and N It's more useful FFT Operations , It can save a lot of computation .

2.4  The physical meaning of image Fourier transform

   The frequency of an image is an indicator of the intensity of the grayscale transformation in the image , It's the gradient of gray in the plane space . Such as : A large area of desert in the image is a region of slow gray change , The corresponding frequency value is very low ; As for the edge area of the earth's surface property transformation, it is a region of intense gray level change in the image , The corresponding frequency value is higher . Fourier transform has obvious physical meaning in practice , Set up f It's an analog signal with limited energy , Then its Fourier transform means f The spectrum of . In a purely mathematical sense , Fourier transform transforms a function into a series of periodic functions . In terms of physical effects , Fourier transform is to transform images from spatial domain to frequency domain , The inverse transformation is to transform the image from frequency domain to spatial domain . In other words , The physical meaning of Fourier transform is to transform the image gray distribution function into the image frequency distribution function .

   Inverse Fourier transform is to transform the frequency distribution function of image into gray distribution function , Image ( An uncompressed lattice ) It's made up of pairs in continuous space ( Real space ) The sampling on the gets a series of sets , Usually a two-dimensional matrix is used to represent the points in space , Write it down as z=f(x, y). And because space is three-dimensional , The image is two-dimensional , Therefore, the relationship between objects in space on another dimension must be represented by gradient , In this way, we can know the corresponding relationship of objects in three-dimensional space by observing images .

   The bright spots we see in the Fourier spectrum , Its meaning refers to the difference between a certain point in the image and the field point , The size of the gradient . That is, the frequency of the point ( It can be understood in this way , The low frequency part of the image refers to the low gradient point , The high frequency part is the opposite ). In general , If the gradient is large, the brightness of the point is strong , Otherwise, the brightness of this point is weak . By looking at the spectrum after Fourier transform , It's also called a power map , We can see the energy distribution of the image directly : If there are more dark dots in the spectrum , So the actual image is softer ( Because there is little difference between each point and field , The gradient is relatively small ); conversely , If there are many bright points in the spectrum , Then the actual image must be sharp , Having a clear boundary and a large difference in pixels on both sides of the boundary .

   After the spectrum is shifted to the origin , It can be seen that the frequency distribution of the image is centered on the origin , Symmetrically distributed . By shifting the frequency spectrum to the center of the circle, the image frequency distribution can be clearly seen , There's another benefit , It can separate out periodic interference signals , Like sinusoidal interference . If a spectrum has sinusoidal interference , Shift the frequency to the origin and you can see , In addition to the center, there is another center , A set of symmetrically distributed highlights , This set is the interference noise . This can be very intuitive by placing a band stop filter in this position to eliminate interference .

   After having a general understanding of the above Fourier transform , Let's go through Numpy and OpenCV Learn the algorithm and operation code of image Fourier transform respectively .

2.5  The properties of two-dimensional Fourier transform

Separation

   Two dimensional discrete Fourier transform has the property of separation

   The main advantage of the separation property is that it can be obtained in two steps by means of a series of one-dimensional Fourier transforms . First step , Take a transformation along each line , Multiply the result by 1/N, Get a two-dimensional function ; The second step , Take the transformation along each column , And multiply the result by 1/N Got it. . This method is first, then . If you use the order of column first and then row , The results are the same .

   As shown in the picture :

   For inverse transformation f(x, y) It can be done in two similar steps :

  Translational

   The displacement property of Fourier transform and inverse transformation pair refers to :

   A Fourier transform by multiplying an exponential term and taking its product , Shift the origin of the frequency plane to . The same , Multiply the exponential term and take its inverse transformation , Displace the origin of the plane of the space domain to the point where N/2 When , The index term is :

   That is to say :

   So , use (x+y) Multiply it to move the origin of the Fourier transform to N*N The center of the frequency matrix , So you can see the whole spectrum . in addition , The translation of the pair does not affect the amplitude of its Fourier transform .

   Besides , Like the continuous two-dimensional Fourier transform , Two dimensional discrete Fourier transform also has periodicity , Conjugate symmetry , Linear , Rotation , Related theorem , Convolution theorem , Proportionality, etc . These properties are of great significance in the analysis and processing of images .

2.6  Image properties of two dimensional discrete Fourier transform

  1, The image is transformed by two-dimensional Fourier transform , The transformation sparse matrix has the following properties : If the origin of the commutative matrix is at the center , The spectrum energy is concentrated around the center of the transform sparse matrix . If the cloud top of the two-dimensional Fourier transform matrix used is in the upper left corner , Then the energy of the image signal will be concentrated on the four corners of the coefficient matrix . This is determined by the properties of the two-dimensional Fourier transform itself . At the same time, it shows that the energy of a stream of image is concentrated in the low frequency region .

  2, The area where the gray level of the image changes slowly , Corresponding to the low frequency component after its transformation ; The area where the image gray level changes step by step , Corresponding to the high frequency component after transformation . Except for particle noise , The edge of the image detail , The contour is the gray level transformation mutation area , They all have the characteristics of the transformed high frequency components .

3, The realization of Fourier transform

   Be careful : Whether it's numpy Realize , Or OpenCV Realize , The frequency of the result is 0 It's going to be in the upper left corner , It's usually a shift to a central position , It can be done by shift Transformation .

3.1 Numpy Implement Fourier transform

  Numpy Of FFT Packages provide functions np.fft.fft2() You can do fast Fourier transform on the signal , The function prototype is shown below ( The output of this function is a complex array complex ndarray)

fft2(a, s=None, axes=(-2, -1), norm=None)

    The meaning of the argument is :

  • a Represents the input image , An array of complex arrays
  • s Represents a sequence of integers , You can determine the size of the output array . Output optional shapes ( The length of each conversion axis ), among s[0] Represents the axis 0,s[1] Represents the axis 1. Correspondence fit(x,n) In function n, Along each axis , If the given shape is smaller than the input shape , The input will be cut out . If greater than, the input will be filled with zeros . If not given ’s’, Then use the edge ’axles’ The input shape of the specified axis
  • axes Represents a sequence of integers , Used to calculate FFT The optional shaft of . If not given , Then use the last two axes .“axes” The repeated index in indicates that multiple conversions are performed on the axis , A sequence of elements means performing one-dimensional FFT
  • norm Include None and ortho Two options , Normalization model ( See numpy.fft). The default value is none

  Numpy in fft Modules have many functions , The related functions are as follows :

# Computing one-dimensional Fourier transform 
numpy.fft.fft(a, n=None, axis=-1, norm=None)

# Computing two-dimensional Fourier transforms 
numpy.fft.fft2(a, n=None, axis=-1, norm=None)

# Calculation n Fourier transform of dimensions 
numpy.fft.fftn()

# Calculation n Fourier transform of dimensional real numbers 
numpy.fft.rfftn()

# Returns the sampling frequency of the Fourier transform 
numpy.fft.fftfreq()

# Will FFT The DC component in the output moves to the center of the spectrum 
numpy.fft.shift()

    The following code is passed through Numpy Implement Fourier transform , call np.fft.fft2() The frequency distribution is obtained by fast Fourier transform , Then call np.fft.fftshift() The function shifts the center to the middle , Finally passed Matplotlib Display effect picture .

# _*_ coding:utf-8 _*_
import cv2
import numpy as np
from matplotlib import pyplot as plt

#  Read images 
img = cv2.imread('irving.jpg', )
#  The image is grayed out 
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#  Fast Fourier transform algorithm to get the frequency distribution 
f = np.fft.fft2(img)

#  The default result center is in the upper left corner 
#  call fftshift() The function moves to the middle position 
fshift = np.fft.fftshift(f)

# fft  It turns out to be plural , The absolute result is the amplitude 
fimg = np.log(np.abs(fshift))

#  Show the results 
plt.subplot(121), plt.imshow(img, 'gray'), plt.title('original Fourier')
plt.axis('off')
plt.subplot(122), plt.imshow(fimg, 'gray'), plt.title('fourier Fourier')
plt.axis('off')
plt.show()

    The results are as follows :

    We can see from the above results that , On the left is the original image , On the right is the spectrum of frequency distribution , The closer to the center, the lower the frequency , The brighter ( The higher the gray value ) The greater the amplitude of the information at that frequency .

3.2  OpenCV Implement Fourier transform

  OpenCV The function of realizing Fourier transform in is cv2.dft(), He and Numpy The output is the same , But it's dual channel . The first channel is the real part of the result , The second channel is the imaginary part of the result , And the input image must first be converted to   np.float32 Format . The function prototype is as follows :

dst = cv2.dft(src, dst=None, flags=None, nonzeroRows=None)

    The meaning of the argument :

  • src Represents the input image , Need to pass np.float32 Conversion format
  • dst Represents the output image , Including output size and size flags Represents a conversion token , among :

    DFT _INVERSE Perform a reverse one-dimensional or two-dimensional transformation , Instead of the default forward conversion ;

    DFT _SCALE Represents the scaling result , By the number of array elements divided by it ;

    DFT _ROWS Performs a forward or reverse transformation of each individual row of the input matrix , This flag can convert multiple vectors at the same time , And can be used to reduce overhead to execute 3D And the transformation of higher dimensions ;

    DFT _COMPLEX_OUTPUT Execute 1D or 2D Forward conversion of real arrays , It's the fastest choice , Preset functions ;

    DFT _REAL_OUTPUT Perform the inverse transformation of one-dimensional or two-dimensional complex arrays , The result is usually a complex array of the same size , But if the input array has conjugate complex symmetry , Then the output is a real array

  • nonzeroRows When the argument is not zero , The function assumes that only nonzeroRows Enter the first row of the array ( Not set ) Or only the first of the output arrays ( Set ) Contains nonzero , So the function can handle the rest of the lines more efficiently , And save some time ; This technique is used for computing array cross correlation or use DFT Convolution is very useful

   Be careful : Since the output spectrum results in a complex number , Call required cv2.magnitude() Function transforms the double access result of Fourier transform into 0 To 255 The scope of . The function prototype is as follows :

cv2.magnitude(x, y, magnitude=None)

    The meaning of the argument is :

    • x For floating point X Seat mark value , The real part
    • y For floating point Y Seat mark value , The imaginary part
      The final output is the amplitude , namely :

   The complete code is as follows :

# _*_ coding:utf-8 _*_
import cv2
import numpy as np
from matplotlib import pyplot as plt

#  Read images 
img = cv2.imread('irving.jpg', )
#  The image is grayed out 
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#  Fourier transform 
# cv2.DFT_COMPLEX_OUTPUT  Perform the inverse transformation of one-dimensional or two-dimensional complex arrays , The result is usually a complex array of the same size 
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)

#  Move the low frequency of the spectrum from the top left to the center 
dft_shift = np.fft.fftshift(dft)

#  Spectrum image dual channel complex conversion to 0~255 Interval 
result = 10 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))

#  Show the image 
plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Input image')
plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(result, cmap='gray')
plt.title('Magnitude Spectrum')
plt.xticks([]), plt.yticks([])
plt.show()

    The results are as follows :

    On the left is the original image , On the right is the converted spectrum image , And make sure the low frequency is in the center .

4, The realization of inverse Fourier transform

   Be careful : Whether it's numpy Realize , Or OpenCV Realize , The frequency of the result is 0 It's going to be in the upper left corner , It's usually a shift to a central position , It can be done by shift Transformation .

4.1 Numpy Realize inverse Fourier transform

   Here's how Numpy Realize inverse Fourier transform , It's the inverse of Fourier transform , The process of converting a spectral image into an original image . It will be transformed into a spectrum by Fourier transform , And for high frequency ( Borders ) And low frequency ( Details ) Part of it , Then we need to use inverse Fourier transform to restore the original rendering . The image processing in frequency domain will be reflected in the inverse transformation image , In order to better image processing .

   The main functions used in image Fourier transform are as follows :

# Realize inverse Fourier transform of image , Returns a complex array 
numpy.fft.ifft2(a, n=None, axis=-1, norm=None)

#fftshit() The inverse of a function , It moves the center low frequency part of the spectrum image to the upper left corner 
numpy.fft.fftshift()

# Convert a complex number to 0 to 255 Scope 
iimg = numpy.abs( The result of inverse Fourier transform )

    The following code implements Fourier transform and inverse Fourier transform

# _*_ coding:utf-8 _*_
import cv2
import numpy as np
from matplotlib import pyplot as plt

#  Read images 
img = cv2.imread('irving.jpg', )
#  The image is grayed out 
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#  Fourier transform 
#  Fast Fourier transform algorithm to get the frequency distribution 
f = np.fft.fft2(img)
#  The default result center is in the upper left corner   call fftshift() The function moves to the middle position 
fshift = np.fft.fftshift(f)
# fft  It turns out to be plural , The absolute result is the amplitude 
rimg = np.log(np.abs(fshift))

#  Inverse Fourier transform 
ishift = np.fft.ifftshift(fshift)
iimg = np.fft.ifft2(ishift)
iimg = np.abs(iimg)


#  Show the results 
plt.subplot(131), plt.imshow(img, 'gray'), plt.title('original Fourier')
plt.axis('off')
plt.subplot(132), plt.imshow(rimg, 'gray'), plt.title('fourier Fourier')
plt.axis('off')
plt.subplot(133), plt.imshow(iimg, 'gray'), plt.title('inverse fourier Fourier')
plt.axis('off')
plt.show()

   The results are as follows :

4.2 OpenCV Realize inverse Fourier transform

   stay OpenCV in , Through the function cv2.idft() Function to achieve inverse Fourier transform , The returned result depends on the type and size of the original image , The original image can be a complex number or a real number , Also note that the input image needs to be converted to np.float32 Format , The function prototype is as follows :

dst = cv2.idft(src[, dst[, flags[, nonzeroRows]]])

    The meaning of the argument is :

  • src Represents the input image , Include real or plural numbers
  • dst Represents the output image
  • flags Represents a conversion token
  • nonzeroRows To be dealt with dst Number of lines , The rest of the lines are undefined ( Please refer to dft Examples of convolution in the description )

    The complete code is as follows :

# _*_ coding:utf-8 _*_
import cv2
import numpy as np
from matplotlib import pyplot as plt

#  Read images 
img = cv2.imread('irving.jpg', )
#  The image is grayed out 
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#  Fourier transform 
# cv2.DFT_COMPLEX_OUTPUT  Perform the inverse transformation of one-dimensional or two-dimensional complex arrays , The result is usually a complex array of the same size 
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
#  Move the low frequency of the spectrum from the top left to the center 
dft_shift = np.fft.fftshift(dft)
#  Spectrum image dual channel complex conversion to 0~255 Interval 
result = 10 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))

#  Inverse Fourier transform 
ishift = np.fft.ifftshift(dft_shift)
iimg = cv2.idft(ishift)
iresult = cv2.magnitude(iimg[:, :, 0], iimg[:, :, 1])


#  Show the image 
plt.subplot(131), plt.imshow(img, cmap='gray')
plt.title('Input image')
plt.xticks([]), plt.yticks([])
plt.subplot(132), plt.imshow(result, cmap='gray')
plt.title('Magnitude Spectrum')
plt.xticks([]), plt.yticks([])
plt.subplot(133), plt.imshow(iresult, cmap='gray')
plt.title('inverse Magnitude Spectrum')
plt.xticks([]), plt.yticks([])
plt.show()

    The result is as shown in the figure :

    The purpose of Fourier transform is not to observe the frequency distribution of images ( At least not the end ), More often, it's to filter the frequency , Image enhancement by modifying the frequency , Image denoising , Edge detection , Feature extraction , Compression encryption and other purposes .

   There are three ways to filter : Lowpass (low-pass), qualcomm (high-pass), Pass through (band-pass), Let's learn one by one .

5, High pass filtering

   A high pass filter is a filter that passes through a high frequency , Attenuate low frequencies and pass through high frequencies , Often used to enhance sharp details , But the contrast of the image will be reduced . The filter will detect an area of the image , According to the difference between the pixel and the surrounding pixel to enhance the brightness of the pixel . The picture below shows “Lena” The spectrum image of the graph , Its central area is the low frequency part .

   Then the center low frequency part is covered by a high pass filter , Will 255 The point of is transformed into 0, And keep the high frequency part , The processing process is shown in the figure below :

   High pass filter is mainly constructed by matrix setting , The core code is as follows :

rows, cols = img.shape
crow,ccol = int(rows/2), int(cols/2)
fshift[crow-30:crow+30, ccol-30:ccol+30] = 0

   Below oneself Baidu looks for a piece lena Graph , Do the , The code is as follows :

# _*_ coding:utf-8 _*_
import cv2
import numpy as np
import matplotlib.pyplot as plt

#  Read images 
img = cv2.imread('lena.jpg', 0)

#  Fourier transform 
fimg = np.fft.fft2(img)
fshift = np.fft.fftshift(fimg)

#  Set the high pass filter 
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2)
fshift[crow-30:crow+30, ccol-30:ccol+30] = 0

#  Inverse Fourier transform 
ishift = np.fft.ifftshift(fshift)
iimg = np.fft.ifft2(ishift)
iimg = np.abs(iimg)

#  Display original image and high pass filter processing image 
plt.subplot(121), plt.imshow(img, 'gray'), plt.title('gray Image')
plt.axis('off')
plt.subplot(122), plt.imshow(iimg, 'gray'), plt.title('Result Image')
plt.axis('off')
plt.show()

    The results are as follows :

    The picture on the left shows lena The gray scale of , The image on the right shows the edge contour image extracted by high pass filter , It is transformed into a spectrum image by Fourier transform , Set the low frequency part of the center to 0, And then the final output image is transformed by inverse Fourier transform .

6, Low pass filtering

   The so-called low-pass is to retain the low-frequency components in the image , Filter high-frequency components , Think of the filter as a fishing net , Want a low-pass filter , That is to turn all the signals in the high frequency area black , And all the low frequency areas are reserved . for example , In a picture of the prairie , The low frequency corresponds to a vast and uniform grassland , The gray component that represents the slow image transformation ; High frequency corresponds to edge information such as tigers in grassland images , Represents the grayscale component with faster image transformation , Due to the excessive intensity of grayscale .

   A low pass filter is a filter that passes through a low frequency , Attenuate high frequencies and pass through low frequencies , Often used in blurred images . Low frequency filter is the opposite of high pass filter , When the interpolation between a pixel and its surrounding pixels is less than a specific value , Smooth the brightness of the pixel , It is often used for denoising and blurring . Such as PS Gaussian blur in software , Is one of the common fuzzy filters , A low-pass filter that weakens high-frequency signals .

   The picture below shows lena The spectrum image of the graph , Its central area is the low frequency part . If you construct a low-pass filter , The low frequency part of the center of the spectrum image is reserved , Replace the rest with black 0, The processing process is shown in the figure , The final result is a blurred image .

   So , How to construct the filtered image ? As shown in the figure below , The filtered image is formed by low-pass filter and spectrum image . The center area of low-pass filter is white 255, Other areas are black 0.

   Low pass filter is mainly constructed by matrix setting , The core code is as follows :

rows, cols = img.shape
crow,ccol = int(rows/2), int(cols/2)
mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 1

    Through the low-pass filter will blur the image of the complete code as follows :

# _*_ coding:utf-8 _*_
import cv2
import numpy as np
import matplotlib.pyplot as plt

#  Read images 
img = cv2.imread('lena.jpg', 0)

#  Fourier transform 
# fimg = np.fft.fft2(img)
fimg = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
fshift = np.fft.fftshift(fimg)

#  Set the low pass filter 
rows, cols = img.shape
#  Center position 
crow, ccol = int(rows / 2), int(cols / 2)
mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1

#  The product of mask image and spectrum image 
f = fshift * mask
print(f.shape, fshift.shape, mask.shape)
# (199, 198, 2) (199, 198, 2) (199, 198, 2)


#  Inverse Fourier transform 
ishift = np.fft.ifftshift(f)
iimg = cv2.idft(ishift)
iimg = cv2.magnitude(iimg[:, :, 0], iimg[:, :, 1])

#  Display original image and high pass filter processing image 
plt.subplot(121), plt.imshow(img, 'gray'), plt.title('gray Image')
plt.axis('off')
plt.subplot(122), plt.imshow(iimg, 'gray'), plt.title('Result Image')
plt.axis('off')
plt.show()

   The results are as follows :

 

 

 

 

References :https://blog.csdn.net/Eastmount/article/details/89474405

https://blog.csdn.net/Eastmount/article/details/89645301

Python + OpenCV Video processing course (https://study.163.com/course/courseLearn.htm?courseId=1005317018#/learn/text?lessonId=1052508042&courseId=1005317018)

https://www.cnblogs.com/tenderwx/p/5245859.html

https://blog.csdn.net/u013921430/article/details/79934162?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_p

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢