Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added all standard morphological transformations #541

Merged
merged 28 commits into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2b550c2
Added all standard morphological transformations
meshtag Jan 12, 2021
48436fc
Improved comments and some other things
meshtag Jan 14, 2021
14900be
Applied adviced changes
meshtag Jan 17, 2021
53d92b2
Applied adviced changes
meshtag Jan 17, 2021
6ac3102
Should handle grayscale dilation/erosion
meshtag Jan 20, 2021
7e779a9
Checking
meshtag Jan 21, 2021
d3897e7
Added test cases and improved code structure
meshtag Jan 21, 2021
defef60
Added command line control
meshtag Jan 22, 2021
22dee61
Added command line control
meshtag Jan 22, 2021
595bc54
Merge branch 'develop' of https://github.com/boostorg/gil into morpho…
meshtag Jan 27, 2021
07aa74b
Rectified some things
meshtag Jan 27, 2021
ec6a135
Rectified some more things
meshtag Jan 27, 2021
76886fd
Improved comments
meshtag Jan 27, 2021
7497ed8
Improved comments
meshtag Jan 27, 2021
8852996
Improved doxygen comments and added more test cases
meshtag Jan 28, 2021
b4045ff
Improved compatibility for builds and rectifying whitespace use
meshtag Jan 29, 2021
8769f91
Minor improvement in comments
meshtag Jan 29, 2021
9b10bfe
Did clang formatting
meshtag Jan 29, 2021
69c7cb8
pushed enum class inside namespace 'detail' and some other things
meshtag Feb 1, 2021
dc10edc
Should handle multichannel images
meshtag Feb 2, 2021
d393ab4
Clang formatting attempt
meshtag Feb 8, 2021
9e48d48
got rid of if/else comparators for target_element
meshtag Feb 8, 2021
2a27083
handle merge conflict
meshtag Feb 8, 2021
67bf920
Adds morphology.hpp declaration in boost/gil.hpp
meshtag Feb 8, 2021
985ea1e
Fix newline
meshtag Feb 8, 2021
2c5405c
Merge branch 'develop' of https://github.com/boostorg/gil into morpho…
meshtag Feb 10, 2021
f365ee2
(std::max)(a, b) instead of std::max(a, b)
meshtag Feb 10, 2021
5fe3371
Improved Formatting
meshtag Feb 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ local sources =
histogram.cpp
interleaved_ptr.cpp
mandelbrot.cpp
morphology.cpp
packed_pixel.cpp
resize.cpp
sobel_scharr.cpp
Expand Down
127 changes: 127 additions & 0 deletions example/morphology.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//
// Copyright 2021 Prathamesh Tagore <[email protected]>
//
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/gil.hpp>
#include <boost/gil/extension/io/png.hpp>
#include <iostream>
#include <map>
#include <string>
#include <vector>

// Default structuring element is SE = [1,1,1]
// |1,1,1|
// [1,1,1]
// SE(1,1)(center pixel) is the one which coincides with the currently
// considered pixel of the image to be convolved. The structuring element can be
// easily changed by the user.
namespace gil = boost::gil;
int main(int argc, char **argv) {
std::map<std::string, bool> operations;
if (argc < 4 || argc > 11) {
throw std::invalid_argument(
"Wrong format of command line arguments.\n"
"Correct format is <Input_image.png> <Output_image_template>"
" <operation1> <operation2> <operation3> <operation4> <operation5> "
"<operation6>"
" <operation7> <operation8>\n");
// User has to enter atleast one operation and they can enter maximum 8
// operations considering binary conversion to be an
// operation.Output_image_template argument is the common component which
// will be added in all output images followed by a hyphen and the operation
// name.
// Example :
// ./example_morphology morphology_original.png out black_hat top_hat
// morphological_gradient dilation erosion opening closing binary
// Order of arguments entered will not matter with the exception of binary
// operation used for binary morphological operations.If binary is entered
// through the command line, it will always be the first operation to be
// applied.
return -1;
} else {
for (int i = 3; i < argc; ++i)
operations[argv[i]] = true;
}
gil::gray8_image_t img;
gil::read_image(argv[1], img, gil::png_tag{});

// Image can be converted to a binary format with high value as 255 and low
// value as 0 by using the threshold operator . This can be used for binary
// morphological operations . Convenient threshold for binary conversion may
// be chosen by the user.
if (operations["binary"]) {
threshold_binary(view(img), view(img), 170, 255);
std::string name = argv[2];
name += "-binary.png";
gil::write_view(name, view(img), gil::png_tag{});
}

std::vector<float> ker_vec(9, 1.0f); // Structuring element
gil::detail::kernel_2d<float> ker_mat(ker_vec.begin(), ker_vec.size(), 1, 1);
gil::gray8_image_t img_out_dilation(img.dimensions()),
img_out_erosion(img.dimensions()), img_out_opening(img.dimensions());
gil::gray8_image_t img_out_closing(img.dimensions()),
img_out_mg(img.dimensions()), img_out_top_hat(img.dimensions());
gil::gray8_image_t img_out_black_hat(img.dimensions());

// Do not pass empty input image views in functions defined below for
// morphological operations to avoid errors.
if (operations["dilation"]) {
// dilate(input_image_view,output_image_view,structuring_element,iterations)
dilate(view(img), view(img_out_dilation), ker_mat, 1);
std::string name = argv[2];
name += "-dilation.png";
gil::write_view(name, view(img_out_dilation), gil::png_tag{});
}

if (operations["erosion"]) {
// erode(input_image_view,output_image_view,structuring_element,iterations)
erode(view(img), view(img_out_erosion), ker_mat, 1);
std::string name = argv[2];
name += "-erosion.png";
gil::write_view(name, view(img_out_erosion), gil::png_tag{});
}

if (operations["opening"]) {
// opening(input_image_view,output_image_view,structuring_element)
opening(view(img), view(img_out_opening), ker_mat);
std::string name = argv[2];
name += "-opening.png";
gil::write_view(name, view(img_out_opening), gil::png_tag{});
}

if (operations["closing"]) {
// closing(input_image_view,output_image_view,structuring_element)
closing(view(img), view(img_out_closing), ker_mat);
std::string name = argv[2];
name += "-closing.png";
gil::write_view(name, view(img_out_closing), gil::png_tag{});
}

if (operations["morphological_gradient"]) {
// morphological_gradient(input_image_view,output_image_view,structuring_element)
morphological_gradient(view(img), view(img_out_mg), ker_mat);
std::string name = argv[2];
name += "-morphological_gradient.png";
gil::write_view(name, view(img_out_mg), gil::png_tag{});
}

if (operations["top_hat"]) {
// top_hat(input_image_view,output_image_view,structuring_element)
top_hat(view(img), view(img_out_top_hat), ker_mat);
std::string name = argv[2];
name += "-top_hat.png";
gil::write_view(name, view(img_out_top_hat), gil::png_tag{});
}

if (operations["black_hat"]) {
// black_hat(input_image_view,output_image_view,structuring_element)
black_hat(view(img), view(img_out_black_hat), ker_mat);
std::string name = argv[2];
name += "-black_hat.png";
gil::write_view(name, view(img_out_black_hat), gil::png_tag{});
}
}
Binary file added example/morphology_original.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions include/boost/gil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include <boost/gil/image_processing/histogram_matching.hpp>
#include <boost/gil/image_processing/hough_parameter.hpp>
#include <boost/gil/image_processing/hough_transform.hpp>
#include <boost/gil/image_processing/morphology.hpp>
#include <boost/gil/image_processing/numeric.hpp>
#include <boost/gil/image_processing/scaling.hpp>
#include <boost/gil/image_processing/threshold.hpp>
Expand Down
Loading