Correlation using OpenCV

 This tutorial covers the correlation operator in OpenCV. The slides relevant to this tutorial can be downloaded from here. The source code for this tutorial can be accessed from here. The result files that we will discuss can be downloaded from here

Unzip the result and source zip files to a folder on your system and run the following commands: "cmake ..; make; ./homework". We have unzipped the source code into the folder /vision/practice/. You should see the following output:

acv@acv-vm:~/vision/practice/bin$ cmake ..; make; ./homework
-- 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


These files are used to submit homework for the ACV course.

Note - run the code from the "bin" folder:

Usage is as follows:

./homework rlerow inputimage.jpeg output_folder/ 

./homework dilate inputimage.jpeg output_folder/ 

./homework erode inputimage.jpeg output_folder/ 

./homework correlationDemo

Run the following code: "./homework correlationDemo". You should see the following files in your /tmp folder:

/tmp/inImage_modified.png         /tmp/result_correlation.png
/tmp/kernelTemplate_modified.png  /tmp/result_overlay.png
The file "/tmp/result_overlay.png" must be exactly similar to the results file in "sample_results/result_1/result_overlay.png". Notice the image "sample_results/result_1/inImage_modified.png". This is a modified version of the input image "../sample_inputs/convolution/pcb_3.png". This image appears darker because we are correcting the image for the mean value in line no 24 of "CorrelationDemo.h". Comment this line, save the file and run the following commands: "make; ./homework correlationDemo". You should see the following lines in your command prompt: 


  
acv@acv-vm:~/vision/practice/bin$ make; ./homework correlationDemo
Scanning dependencies of target homework
[ 16%] Building CXX object CMakeFiles/homework.dir/src/CorrelationDemo.cpp.o
[ 33%] Building CXX object CMakeFiles/homework.dir/src/homework.cpp.o
[ 50%] Linking CXX executable homework
[100%] Built target homework
acv@acv-vm:~/vision/practice/bin$ 
The result file "/tmp/result_overlay.png" must be exactly similar to the results file in "sample_results/result_2/result_overlay.png". The component has not been detected correctly. The two result files are compared below. The first image has the correct result, while the image below it is the wrong result.
The correlation operator moves over the input image to different locations row i=0 to Height, column, j=0 to Width. At each location, the result is computed as a weighted sum of the input image with the template image according to the correlation forumla shown below.
Due to the weighted sum, uniformly bright areas in the input image get a high value irrespective of the template image used. Ensuring that the input and template image are normalized about the mean gives the correct result. You can experiment with other images given in the input set. Change line no 5 in CorrelationDemo.cpp to the following.

  char inpImagePath[1024] = "../sample_inputs/convolution/pcb_3_blanked_out.png";
    
Save the file and run the following command:

  
acv@acv-vm:~/vision/practice/bin$ make; ./homework correlationDemo
Scanning dependencies of target homework
[ 16%] Building CXX object CMakeFiles/homework.dir/src/CorrelationDemo.cpp.o
[ 33%] Linking CXX executable homework
[100%] Built target homework
acv@acv-vm:~/vision/practice/bin$ 
The result generated in /tmp/result_overlay.png" must be exactly similar to the results file in "sample_results/result_3/result_overlay.png". It is shown below:
Notice that the component has been matched with a white colored patch. Remove the comment from line no 24 of "CorrelationDemo.h". Save the file and run the following commands: "make; ./homework correlationDemo". You should see the following lines in your command prompt: 


  
acv@acv-vm:~/vision/practice/bin$ make; ./homework correlationDemo
Scanning dependencies of target homework
[ 16%] Building CXX object CMakeFiles/homework.dir/src/CorrelationDemo.cpp.o
[ 33%] Building CXX object CMakeFiles/homework.dir/src/homework.cpp.o
[ 50%] Linking CXX executable homework
[100%] Built target homework
acv@acv-vm:~/vision/practice/bin$ 
The result file generated in The file "/tmp/result_overlay.png" must be exactly similar to the results file in "sample_results/result_4/result_overlay.png". The result files are shown below for comparison. The first image is the mean-corrected input image. It is generated in line no 14 of CorrelationDemo.cpp. The lower image is the result from correlation matching :
Change the input image by modifying line no 5 of CorrelationDemo.cpp to the following:

  char inpImagePath[1024] = "../sample_inputs/convolution/pcb_2.png";
   
Save the file and compile and run the code using following commands:

acv@acv-vm:~/vision/practice/bin$ make; ./homework correlationDemo
Scanning dependencies of target homework
[ 16%] Building CXX object CMakeFiles/homework.dir/src/CorrelationDemo.cpp.o
[ 33%] Linking CXX executable homework
[100%] Built target homework
acv@acv-vm:~/vision/practice/bin$ 
 
The result file generated in The file "/tmp/result_overlay.png" must be exactly similar to the results file in "sample_results/result_5/result_overlay.png". It is reproduced below for reference. The component being matched is not present in the input image. Correlation has picked up the closest match.
However, this does not always happen. Sometimes correlation is unable to locate the nearest match for a template. For example, change the template image to "pcb_component_2.png" by modifying the line 6 in CorrelationDemo.cpp:

  char templateImagePath[1024] = "../sample_inputs/convolution/pcb_component_2.png";
  
The result file generated in The file "/tmp/result_overlay.png" must be exactly similar to the results file in "sample_results/result_6/result_overlay.png". The component has NOT been detected even though very similar components are present on the input image. If you change the input image to "pcb_3.png", the component is detected. These results are in "sample_results/result_7/result_overlay.png". The plain vanilla correlation implemented here is not robust to scale and rotation variation.

Comments

Popular posts from this blog

Discrete Wavelet Transform

Geometric image transforms

Testing image capture with high speed cameras