Skip to content

Commit

Permalink
[Port to C++] Morphological Operations (SRA-VJTI#76)
Browse files Browse the repository at this point in the history
* added code for erosion and dilation

* added code for opening, closing and gradient

* added readme

* removed template files

* added assets folder

* changed path for images

* Change naming scheme

* Rename main.cpp to relevenat 'algo'.cpp
* Modify Makefile accordingly

* Major changes
1] Generalised modules for any size kernel
2] Modified file structure
3] Added module for padding of images

* Add new image to assets folder

* Add dependencies to Makefiles

* Take image path as commandline argument

* Remove redundant files

* - Add table of contents
- Break long paragraphs in smaller sections.

* - Remove redundant function from helper.hpp
- Add comments

* Rename Assets->assets
Rename Readme.md -> README.md

* Revert Makefile changes

* Change format of helper.hpp
- made separate src and include folder

* Modify Makefile

* Change path of include file

* Rename files
- helper.cpp -> morphology.cpp
- helper.hpp -> morphology.hpp

* Change image path

* Change helper -> morphology

---------

Co-authored-by: Smit1603 <[email protected]>
  • Loading branch information
2 people authored and Alqama Shaikh committed Mar 23, 2023
1 parent 9f929fe commit c4b0211
Show file tree
Hide file tree
Showing 26 changed files with 563 additions and 0 deletions.
47 changes: 47 additions & 0 deletions 4_cv_basics/6_morphology/1_erosion/1_erosion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>

#include "../include/morphology.hpp"

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
if ( argc != 2 )
{
std::cout <<"usage: ./output <Image_Path>\n";
return -1;
}

// Reading the Image
Mat source_image = imread(argv[1], IMREAD_GRAYSCALE);

// Check if the image is created successfully or not
if (!source_image.data)
{
cout << "Could not open or find the image\n";
return 0;
}

// creating container for output image according to size and type of source image
Mat output_image{source_image.size(), source_image.type()};

// Applying erosion on source image
int kernel_size = 3;
output_image = erosion(source_image, output_image, kernel_size);

// Displaying both source and output image
namedWindow("source", WINDOW_NORMAL);
imshow("source", source_image);

namedWindow("output", WINDOW_NORMAL);
imshow("output", output_image);

waitKey();

return 0;
}
5 changes: 5 additions & 0 deletions 4_cv_basics/6_morphology/1_erosion/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output: 1_erosion.cpp ../include/morphology.hpp ../src/morphology.cpp
g++ 1_erosion.cpp ../src/morphology.cpp -o output `pkg-config --cflags --libs opencv4`

clean:
rm output
47 changes: 47 additions & 0 deletions 4_cv_basics/6_morphology/2_dilation/2_dilation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>

#include "../include/morphology.hpp"

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
if ( argc != 2 )
{
std::cout <<"usage: ./output <Image_Path>\n";
return -1;
}

// Reading the Image
Mat source_image = imread(argv[1], IMREAD_GRAYSCALE);

// Check if the image is created successfully or not
if (!source_image.data)
{
std::cout << "Could not open or find the image\n";
return 0;
}

// creating container for output image according to size and type of source image
Mat output_image{source_image.size(), source_image.type()};

// Applying dilation on source image
int kernel_size = 3;
output_image = dilation(source_image, output_image, kernel_size);

// Displaying both source and output image
namedWindow("source", WINDOW_NORMAL);
imshow("source", source_image);

namedWindow("output", WINDOW_NORMAL);
imshow("output", output_image);

waitKey();

return 0;
}
5 changes: 5 additions & 0 deletions 4_cv_basics/6_morphology/2_dilation/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output: 2_dilation.cpp ../include/morphology.hpp ../src/morphology.cpp
g++ 2_dilation.cpp ../src/morphology.cpp -o output `pkg-config --cflags --libs opencv4`

clean:
rm output
51 changes: 51 additions & 0 deletions 4_cv_basics/6_morphology/3_opening/3_opening.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>

#include "../include/morphology.hpp"

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
if ( argc != 2 )
{
std::cout <<"usage: ./output <Image_Path>\n";
return -1;
}

// Reading the Image
Mat source_image = imread(argv[1], IMREAD_GRAYSCALE);

// Check if the image is created successfully or not
if (!source_image.data) {
std::cout << "Could not open or find the image\n";
return 0;
}

// creating container for output image according to size and type of source image
Mat temp1{source_image.size(), source_image.type()};
Mat output_image{source_image.size(), source_image.type()};

//Applying erosion on source image
int kernel_size_erosion = 3;
temp1 = erosion(source_image, temp1,kernel_size_erosion);

// Applying dilation on erroded image
int kernel_size_dilation = 3;
output_image = dilation(temp1, output_image,kernel_size_dilation);

//Displaying both source and output image
namedWindow("source", WINDOW_NORMAL);
imshow("source", source_image);

namedWindow("output", WINDOW_NORMAL);
imshow("output", output_image);

waitKey();

return 0;
}
5 changes: 5 additions & 0 deletions 4_cv_basics/6_morphology/3_opening/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output: 3_opening.cpp ../include/morphology.hpp ../src/morphology.cpp
g++ 3_opening.cpp ../src/morphology.cpp -o output `pkg-config --cflags --libs opencv4`

clean:
rm output
51 changes: 51 additions & 0 deletions 4_cv_basics/6_morphology/4_closing/4_closing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>

#include "../include/morphology.hpp"

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
if ( argc != 2 )
{
std::cout <<"usage: ./output <Image_Path>\n";
return -1;
}

// Reading the Image
Mat source_image = imread(argv[1], IMREAD_GRAYSCALE);

// Check if the image is created successfully or not
if (!source_image.data) {
std::cout << "Could not open or find the image\n";
return 0;
}

// creating container for output image according to size and type of source image
Mat temp1{source_image.size(), source_image.type()};
Mat output_image{source_image.size(), source_image.type()};

//Applying dilation on source image
int kernel_size_dilation = 3;
temp1 = dilation(source_image, temp1,kernel_size_dilation);

// Applying erosion on dilated image
int kernel_size_erosion = 3;
output_image = erosion(temp1, output_image,kernel_size_erosion);

//Displaying both source and output image
namedWindow("source", WINDOW_NORMAL);
imshow("source", source_image);

namedWindow("output", WINDOW_NORMAL);
imshow("output", output_image);

waitKey();

return 0;
}
5 changes: 5 additions & 0 deletions 4_cv_basics/6_morphology/4_closing/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output: 4_closing.cpp ../include/morphology.hpp ../src/morphology.cpp
g++ 4_closing.cpp ../src/morphology.cpp -o output `pkg-config --cflags --libs opencv4`

clean:
rm output
56 changes: 56 additions & 0 deletions 4_cv_basics/6_morphology/5_gradient/5_gradient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>

#include "../include/morphology.hpp"

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
if (argc != 2)
{
std::cout << "usage: ./output <Image_Path>\n";
return -1;
}

// Reading the Image
Mat source_image = imread(argv[1], IMREAD_GRAYSCALE);

// Check if the image is created successfully or not
if (!source_image.data)
{
std::cout << "Could not open or find the image\n";
return 0;
}

// creating container for output image according to size and type of source image
Mat erod{source_image.size(), source_image.type()};
Mat dill{source_image.size(), source_image.type()};
Mat gradient{source_image.size(), source_image.type()};

// Applying erosion on source image
int kernel_size_erosion = 3;
erod = erosion(source_image, erod, kernel_size_erosion);

// Applying dilation on source image
int kernel_size_dilation = 3;
dill = dilation(source_image, dill, kernel_size_dilation);

// Taking difference of erroded and dilated image to get gradient
gradient = difference(dill, erod, gradient);

//Displaying both source and output image
namedWindow("source", WINDOW_NORMAL);
imshow("source", source_image);

namedWindow("gradient", WINDOW_NORMAL);
imshow("gradient", gradient);

waitKey();

return 0;
}
5 changes: 5 additions & 0 deletions 4_cv_basics/6_morphology/5_gradient/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output: 5_gradient.cpp ../include/morphology.hpp ../src/morphology.cpp
g++ 5_gradient.cpp ../src/morphology.cpp -o output `pkg-config --cflags --libs opencv4`

clean:
rm output
Empty file.
Empty file.
Loading

0 comments on commit c4b0211

Please sign in to comment.