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 9 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
130 changes: 130 additions & 0 deletions example/morphology.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//
// 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/extension/io/png.hpp>
#include <boost/gil/image_processing/morphology.hpp>
meshtag marked this conversation as resolved.
Show resolved Hide resolved
#include <vector>
#include<iostream>
#include<map>
#include<string>
meshtag marked this conversation as resolved.
Show resolved Hide resolved
//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.
meshtag marked this conversation as resolved.
Show resolved Hide resolved

int main(int argc,char **argv)
{
std::map<std::string,bool>operations;
meshtag marked this conversation as resolved.
Show resolved Hide resolved
if(argc < 4 || argc > 11)
{
std::cout<<"Wrong format of command line arguments.\n";
std::cout<<"Correct format is <Input_image.png> <Output_image_template>"
meshtag marked this conversation as resolved.
Show resolved Hide resolved
" <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 original.png out black_hat top_hat morphological_gradient dilation
// erosion opening closing
//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]];
meshtag marked this conversation as resolved.
Show resolved Hide resolved
}
using namespace boost::gil;
gray8_image_t img;
read_image(argv[1], img, png_tag{});

//Image can be converted to a binary format with high value as 255 and low value as 0
//by using the following 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";
write_view(name, view(img), png_tag{});
}

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

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

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

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

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

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

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

if(operations["black_hat"])
{
//black_hat(input_image_view,structuring_element)
black_hat(view(img),view(img_out_black_hat),ker_mat);
std::string name = argv[2];
name += "-black_hat.png";
write_view( name, view(img_out_black_hat), png_tag{});
}
}
Binary file added example/original.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
193 changes: 193 additions & 0 deletions include/boost/gil/image_processing/morphology.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
//
// 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)
//

#ifndef BOOST_GIL_IMAGE_PROCESSING_MORPHOLOGY_HPP
#define BOOST_GIL_IMAGE_PROCESSING_MORPHOLOGY_HPP

#include <boost/gil/extension/numeric/kernel.hpp>

#include <boost/gil/gray.hpp>
#include <boost/gil/algorithm.hpp>
#include <boost/gil/image_view.hpp>
#include <boost/gil/image_processing/threshold.hpp>

namespace gil = boost::gil;
meshtag marked this conversation as resolved.
Show resolved Hide resolved
namespace boost{
namespace gil{
enum class morphological_operations
meshtag marked this conversation as resolved.
Show resolved Hide resolved
{
dilate,
erode,
opening,
closing,
morphological_gradient,
top_hat,
black_hat
};
namespace detail{
template <typename SrcView, typename DstView, typename Kernel>
void morph_impl(SrcView const& src_view, DstView const& dst_view, Kernel const& kernel,const int identifier)
mloskot marked this conversation as resolved.
Show resolved Hide resolved
{
int flip_ker_row, flip_ker_col, row_boundary, col_boundary;
meshtag marked this conversation as resolved.
Show resolved Hide resolved
typename channel_type<typename SrcView::value_type>::type max_overlapped_element,min_overlapped_element;
for (std::ptrdiff_t view_row = 0; view_row < src_view.height(); ++view_row)
{
for (std::ptrdiff_t view_col = 0; view_col < src_view.width(); ++view_col)
{
max_overlapped_element = (std::numeric_limits<typename channel_type<SrcView>::type>::min)();
min_overlapped_element = (std::numeric_limits<typename channel_type<SrcView>::type>::max)();
meshtag marked this conversation as resolved.
Show resolved Hide resolved
for (std::size_t kernel_row = 0; kernel_row < kernel.size(); ++kernel_row)
{
flip_ker_row = kernel.size() - 1 - kernel_row; // row index of flipped kernel

for (std::size_t kernel_col = 0; kernel_col < kernel.size(); ++kernel_col)
{
flip_ker_col = kernel.size() - 1 - kernel_col; // column index of flipped kernel

// index of input signal, used for checking boundary
row_boundary = view_row + (kernel.center_y() - flip_ker_row);
col_boundary = view_col + (kernel.center_x() - flip_ker_col);

// ignore input samples which are out of bound
if (row_boundary >= 0 && row_boundary < src_view.height() &&
col_boundary >= 0 && col_boundary < src_view.width())
{
if(src_view(col_boundary, row_boundary) > max_overlapped_element)
max_overlapped_element = src_view(col_boundary, row_boundary);
if(src_view(col_boundary, row_boundary) < min_overlapped_element)
min_overlapped_element = src_view(col_boundary, row_boundary);
meshtag marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
if(identifier)//identifier = 1 for dilation
dst_view(view_col, view_row) = max_overlapped_element;
else //identifier = 0 for erosion
dst_view(view_col, view_row) = min_overlapped_element;
}
}
}

template <typename SrcView, typename DstView,typename Kernel>
void morph(SrcView const& src_view, DstView const& dst_view,Kernel const& ker_mat,const int identifier)
{
using namespace boost::gil;
meshtag marked this conversation as resolved.
Show resolved Hide resolved
BOOST_ASSERT(ker_mat.size() != 0 && src_view.dimensions() == dst_view.dimensions());
gil::gray8_image_t intermediate_img(src_view.dimensions());
mloskot marked this conversation as resolved.
Show resolved Hide resolved
for (std::size_t i = 0; i < src_view.num_channels(); i++)
{
morph_impl(
nth_channel_view(src_view, i),
nth_channel_view(view(intermediate_img), i),
meshtag marked this conversation as resolved.
Show resolved Hide resolved
ker_mat,identifier
);
}
gil::transform_pixels(view(intermediate_img),dst_view,[](const gil::gray8_pixel_t& p){return p[0];});
}

//This function calculates the difference between pixel values of first image_view and second
//image_view.This function can be used for rgb as well as grayscale images.
template <typename SrcView, typename DstView ,typename DiffView>
void difference_impl(SrcView const& src_view, DstView const& dst_view, DiffView const& diff_view)
{
for (std::ptrdiff_t view_row = 0; view_row < src_view.height(); ++view_row)
for (std::ptrdiff_t view_col = 0; view_col < src_view.width(); ++view_col)
diff_view(view_col, view_row) = src_view(view_col,view_row) - dst_view(view_col,view_row);
}

template <typename SrcView, typename DstView, typename DiffView>
void difference(SrcView const& src_view, DstView const& dst_view , DiffView const& diff_view)
{

BOOST_ASSERT(src_view.dimensions() == dst_view.dimensions());

for (std::size_t i = 0; i < src_view.num_channels(); i++)
{
difference_impl(
nth_channel_view(src_view, i),
nth_channel_view(dst_view, i),
nth_channel_view(diff_view, i)
);
}
}
}//namespace boost::gil::detail

// Dilation:Give the maximum overlapped value to the pixel overlapping with the center element of
//structuring element.We can vary the number of times dilation happens by varying the
// argument 'iterations' in the dilate function.
template <typename SrcView, typename DstView,typename Kernel>
void dilate(SrcView const& src_view, DstView const& dst_view,Kernel const& ker_mat,const int iterations)
{
boost::gil::transform_pixels(src_view,dst_view,[](const boost::gil::gray8_pixel_t& p){return p[0];});
meshtag marked this conversation as resolved.
Show resolved Hide resolved
for(int i=0;i<iterations;++i)
morph(dst_view,dst_view,ker_mat,1);
}

//Erosion:Give the minimum overlapped value to the pixel overlapping with the center element of
//structuring element.We can vary the number of times erosion happens by varying the
//argument 'iterations' in the erode function.
template <typename SrcView, typename DstView,typename Kernel>
void erode(SrcView const& src_view, DstView const& dst_view,Kernel const& ker_mat,const int iterations)
{
boost::gil::transform_pixels(src_view,dst_view,[](const boost::gil::gray8_pixel_t& p){return p[0];});
for(int i=0;i<iterations;++i)
meshtag marked this conversation as resolved.
Show resolved Hide resolved
morph(dst_view,dst_view,ker_mat,0);
}

//Opening:Opening is just another name of erosion followed by dilation.
//It is useful in removing noise.
template <typename SrcView, typename DstView,typename Kernel>
void opening(SrcView const& src_view, DstView const& dst_view,Kernel const& ker_mat)
{
erode(src_view,dst_view,ker_mat,1);
dilate(dst_view,dst_view,ker_mat,1);
}

//Closing:Closing is reverse of Opening, Dilation followed by Erosion.
//It is useful in closing small holes inside the foreground objects, or small black points
//on the object.
template <typename SrcView, typename DstView,typename Kernel>
void closing(SrcView const& src_view, DstView const& dst_view,Kernel const& ker_mat)
{
dilate(src_view,dst_view,ker_mat,1);
erode(dst_view,dst_view,ker_mat,1);
}

//Morphological Gradient:It is the difference between dilation and erosion of an image.
//The result will look like the outline of the object.
template <typename SrcView, typename DstView, typename Kernel>
void morphological_gradient(SrcView const& src_view, DstView const& dst_view, Kernel const& ker_mat)
{
using namespace boost::gil;
gray8_image_t int_dilate(src_view.dimensions()),int_erode(src_view.dimensions());
dilate(src_view,view(int_dilate),ker_mat,1);
erode(src_view,view(int_erode),ker_mat,1);
difference(view(int_dilate),view(int_erode),dst_view);
}

//Top Hat:It is the difference between input image and Opening of the image.
template <typename SrcView, typename DstView, typename Kernel>
void top_hat(SrcView const& src_view, DstView const& dst_view, Kernel const& ker_mat)
{
using namespace boost::gil;
gray8_image_t int_opening(src_view.dimensions());
opening(src_view,view(int_opening),ker_mat);
difference(src_view,view(int_opening),dst_view);
}

//Black Hat:It is the difference between the closing of the input image and input image.
template <typename SrcView, typename DstView, typename Kernel>
void black_hat(SrcView const& src_view, DstView const& dst_view, Kernel const& ker_mat)
{
using namespace boost::gil;
gray8_image_t int_closing(src_view.dimensions());
closing(src_view,view(int_closing),ker_mat);
difference(view(int_closing), src_view,dst_view);
}
}
}//namespace boost::gil
#endif //BOOST_GIL_IMAGE_PROCESSING_MORPHOLOGY_HPP
1 change: 1 addition & 0 deletions test/core/image_processing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# http://www.boost.org/LICENSE_1_0.txt)
#
foreach(_name
morphology
threshold_binary
threshold_truncate
threshold_otsu)
Expand Down
Loading