Morphological operations using OpenCV

This tutorial will discuss examples of B/W morphological operations using OpenCV.  The code used for this tutorial can be downloaded from here.

When you unzip the file from above, you will see the following directory and file structure:

bin  CMakeLists.txt  sample_inputs  sample_results  src

Change to the bin/ folder and compile the code using the commands "cmake ../" and "make". You should see a result as below:

acv@acv-vm:~/vision/practice2/bin$ cmake ..
-- 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/practice2/bin
acv@acv-vm:~/vision/practice2/bin$ make
Scanning dependencies of target homework
[ 20%] Building CXX object CMakeFiles/homework.dir/src/ImageUtilities.cpp.o
[ 40%] Building CXX object CMakeFiles/homework.dir/src/Morphology.cpp.o
[ 60%] Building CXX object CMakeFiles/homework.dir/src/homework.cpp.o
[ 80%] Building CXX object CMakeFiles/homework.dir/src/util/LogManager.cpp.o
[100%] Linking CXX executable homework
[100%] Built target homework
Run erosion by typing the following command in the "bin" folder: ./homework erode ../sample_inputs/bw_alphabets_with_noise1.png /tmp/. The result file /tmp/erodeResult.png should be exactly the same as the image given in "/sample_results/erodeResult_1.png". 


 Run dilation by typing the following command in the "bin" folder: ./homework dilate ../sample_inputs/bw_alphabets_with_noise2.png /tmp/. The result file /tmp/dilateResult.png should be exactly the same as the image given in "sample_results/dilateResult_1.png". 


 Let us try the opening algorithm. Run the following commands one after the other:

./homework erode ../sample_inputs/bw_alphabets_with_noise1.png /tmp/
./homework dilate /tmp/erodeResult.png /tmp/
The result of running the first command will be stored in image "/tmp/erodeResult.png". This image should be exactly similar to "sample_results/opening_1_erosion.png". The result of running the second command is the final result of opening. This should be stored in /tmp/dilateResult.png and should be exactly similar to the file "sample_results/opening_1_dilation.png". 


 Let us try the closing algorithm. Run the following commands one after the other:

./homework dilate ../sample_inputs/bw_alphabets_with_noise2.png /tmp/
./homework erode /tmp/dilateResult.png  /tmp/
The result of running the first command will be stored in image "/tmp/dilateResult.png". This image should be exactly similar to "sample_results/closing_1_dilation.png". The result of running the second command is the final result of opening. This should be stored in /tmp/erodeResult.png and should be exactly similar to the file "sample_results/closing_1_erosion.png".



Let us now change the structuring element that is used in erosion and dilation. Comment line no 60 in the function "Morphology::erodeImage" of Morphology.cpp. Comment line no 77 in the function "Morphology::dilateImage" in file Morphology.cpp. Add the following lines between lines 62 and 67 and lines 86-91. These lines are changing the structuring elements that are used in the dilate and the erode function.

unsigned short arr[3][3] = {{0, 1, 0},
	{0, 1, 0},
	{0, 1, 0}
	};

Mat element = ImageUtilities::getMatFromArray(3, 3, (unsigned short*) arr);
Go to the bin/ folder, and compile the code using the make command, and execute the command "make; ./homework erode ../sample_inputs/bw_alphabets_with_noise1.png /tmp/". The result will be stored in image "/tmp/erodeResult.png". This image should be exactly similar to "sample_results/erodeResult_2.png". 


To try out dilation with the new structuring element, execute the command "./homework dilate ../sample_inputs/bw_alphabets_with_noise2.png /tmp/". The result will be stored in image "/tmp/erodeResult.png". This image should be exactly similar to "sample_results/dilateResult_2.png".


After changing the structuring element, the opening and closing operators will also give very different results. Run the same commands that we used earlier for opening and closing. i.e.: 

./homework erode ../sample_inputs/bw_alphabets_with_noise1.png /tmp/
./homework dilate /tmp/erodeResult.png /tmp/
This is the opening algorithm. The result of the second command is stored in /tmp/dilateResult.png, and should be exactly similar to "sample_results/opening_2_dilation.png".  If you run the closing commands, i.e.: 

./homework dilate ../sample_inputs/bw_alphabets_with_noise2.png /tmp/
./homework erode /tmp/dilateResult.png  /tmp/

the result will be stored in /tmp/erodeResult.png and should be exactly similar to the file: "sample_results/closing_2_erosion.png"

The source code will all changes described above can be downloaded from here.

Comments

Popular posts from this blog

Discrete Wavelet Transform

Geometric image transforms

Testing image capture with high speed cameras