Designing Gaussian, Averaging, and Fourier frequency domain filters using Opencv
This tutorial covers the design of various types of filters using Opencv. Source code and input images for this tutorial can be downloaded from here. The result images are here. A set of slides explaining the theory behind these processes is here.
After unzipping the code, run the commands as shown below. The output should also be similar to what is shown.
acv@acv-vm:~/vision/practice/bin$ cmake ..; make; ./homework correlationDemo
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
-- Found OpenCV: /usr/local (found version "4.4.0")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/acv/vision/practice/bin
Scanning dependencies of target homework
[ 16%] Building CXX object CMakeFiles/homework.dir/src/CorrelationDemo.cpp.o
[ 33%] Building CXX object CMakeFiles/homework.dir/src/ImageUtilities.cpp.o
[ 50%] Building CXX object CMakeFiles/homework.dir/src/Morphology.cpp.o
[ 66%] Building CXX object CMakeFiles/homework.dir/src/homework.cpp.o
[ 83%] Building CXX object CMakeFiles/homework.dir/src/util/LogManager.cpp.o
[100%] Linking CXX executable homework
[100%] Built target homework
acv@acv-vm:~/vision/practice/bin$
The results will be in the /tmp folder, and the files should be following ones:
acv@acv-vm:~/vision/practice/bin$ ls /tmp/*png
/tmp/filter_averaging_result.png
/tmp/filter_gaussian_result.png
/tmp/filter_input.png
/tmp/fourierHighpassDemo_0inputImage.png
/tmp/fourierHighpassDemo_1Filter.png
/tmp/fourierHighpassDemo_2coeffWithoutFilter.png
/tmp/fourierHighpassDemo_3coeffWithFilter.png
/tmp/fourierHighpassDemo_4outputImage.png
/tmp/fourierLowpassDemo_0inputImage.png
/tmp/fourierLowpassDemo_1Filter.png
/tmp/fourierLowpassDemo_2coeffWithoutFilter.png
/tmp/fourierLowpassDemo_3coeffWithFilter.png
/tmp/fourierLowpassDemo_4outputImage.png
/tmp/fourier_noise_HighpassDemo_0inputImage.png
/tmp/fourier_noise_HighpassDemo_1Filter.png
/tmp/fourier_noise_HighpassDemo_2coeffWithoutFilter.png
/tmp/fourier_noise_HighpassDemo_3coeffWithFilter.png
/tmp/fourier_noise_HighpassDemo_4outputImage.png
/tmp/fourier_noise_LowpassDemo_0inputImage.png
/tmp/fourier_noise_LowpassDemo_1Filter.png
/tmp/fourier_noise_LowpassDemo_2coeffWithoutFilter.png
/tmp/fourier_noise_LowpassDemo_3coeffWithFilter.png
/tmp/fourier_noise_LowpassDemo_4outputImage.png
/tmp/fourier_rotated_HighpassDemo_0inputImage.png
/tmp/fourier_rotated_HighpassDemo_1Filter.png
/tmp/fourier_rotated_HighpassDemo_2coeffWithoutFilter.png
/tmp/fourier_rotated_HighpassDemo_3coeffWithFilter.png
/tmp/fourier_rotated_HighpassDemo_4outputImage.png
/tmp/fourier_rotated_LowpassDemo_0inputImage.png
/tmp/fourier_rotated_LowpassDemo_1Filter.png
/tmp/fourier_rotated_LowpassDemo_2coeffWithoutFilter.png
/tmp/fourier_rotated_LowpassDemo_3coeffWithFilter.png
/tmp/fourier_rotated_LowpassDemo_4outputImage.png
/tmp/image_with_salt_pepper_noise.png
/tmp/sobel_input.png
/tmp/sobel_result.png
acv@acv-vm:~/vision/practice/bin$
Lines 48-55 in the file homework.cpp run the demo code. These are reproduced below. Lines 48-51 create an image with salt and pepper noise. Lines 53-55 run the filter, edge detection and fourier analysis demos.
char inpImagePath[1024] = "../samples/lecture_samples/convolution/temple_top_gray.png";
Mat inImage = imread(inpImagePath, IMREAD_GRAYSCALE);
CorrelationDemo::addSaltPepperNoiseToImage(inImage, 5, 0);
imwrite("/tmp/image_with_salt_pepper_noise.png", inImage);
CorrelationDemo::demoFilters();
CorrelationDemo::demoEdgeDetection();
CorrelationDemo::demoFourier();
The result of applying averaging and Gaussian filters with a filter size of 5x5 is shown below.
Most of the code is self-explanatory. A few aspects to note:
- The result of Fourier series analysis needs to be shifted so that one can view the frequencies are centered at the middle of the image. This is accomplised by shifting the result of DFT (line 257 of CorrelationDemo.h).
- Fourier series analysis has both a magnitude and phase component. The code outputs only the magnitude of the coefficients.
- A gradient map is generated in the function CorrelationDemo::demoEdgeDetection. The code uses vertical and horizontal edges detected using sobel filters. Edges in an image can be in many directions. There are also other filters like Scharr, Prewitt. You are encouraged to experiment with other edge filters to generate a gradient map.
Comments
Post a Comment