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

[WIP] Adding feature 'Gaussian Filter' #446

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions example/gaussian_blur.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Copyright 2020 Laxmikant Suryavanshi <laxmikantsuryavanshi@hotmail.com>
//
// 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/jpeg.hpp>
#include <boost/gil/image_processing/filter.hpp>

using namespace boost::gil;

int main()
{
rgb8_image_t img;
read_image("test.jpg",img, jpeg_tag{});
rgb8_image_t img_out(img.dimensions());

// performing gaussian smoothening on image
boost::gil::gaussian_filter(const_view(img), view(img_out), 5, 1.0f);
write_view("gaussian_blur.jpg", view(img_out), jpeg_tag{});

return 0;
}
27 changes: 27 additions & 0 deletions include/boost/gil/extension/numeric/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,33 @@ constexpr std::size_t kernel_2d_fixed<T, Size>::static_size;

} //namespace detail

template <typename T = float, typename Allocator = std::allocator<T>>
kernel_1d<T, Allocator> get_1d_kernel_from(
detail::kernel_2d<T, Allocator> const& kernel)
{
std::vector<T, Allocator> values(kernel.size());
for (std::size_t x = 0; x < kernel.size(); x++)
values[x] = std::sqrt(kernel.at(x, x));
return kernel_1d<T, Allocator>(values.begin(), values.size(), (kernel.size() / 2));
}

template <typename T = float, typename Allocator = std::allocator<T>>
detail::kernel_2d<T, Allocator> get_2d_kernel_from(
kernel_1d<T, Allocator> const& kernel)
{
auto side_length = kernel.size();
std::vector<T, Allocator> values(side_length * side_length);
for (std::size_t y = 0; y < side_length; y++)
{
for (std::size_t x = 0; x < side_length; x++)
{
values[y * side_length + x] = kernel[y] * kernel[x];
}
}
return detail::kernel_2d<T, Allocator>(values.begin(), side_length * side_length,
side_length / 2, side_length / 2);
}

/// \brief reverse a kernel
//template <typename Kernel>
//inline Kernel reverse_kernel(Kernel const& kernel)
Expand Down
26 changes: 26 additions & 0 deletions include/boost/gil/image_processing/filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <boost/gil/extension/numeric/algorithm.hpp>
#include <boost/gil/extension/numeric/kernel.hpp>
#include <boost/gil/extension/numeric/convolve.hpp>
#include <boost/gil/image_processing/numeric.hpp>

#include <boost/gil/image.hpp>
#include <boost/gil/image_view.hpp>
Expand Down Expand Up @@ -135,6 +136,31 @@ void median_filter(SrcView const& src_view, DstView const& dst_view, std::size_t
}
}

template <typename SrcView, typename DstView>
void gaussian_filter(
SrcView const& src_view,
DstView const& dst_view,
std::size_t kernel_size,
double sigma,
boundary_option option = boundary_option::extend_zero
)
{
gil_function_requires<ImageViewConcept<SrcView>>();
gil_function_requires<MutableImageViewConcept<DstView>>();
static_assert(color_spaces_are_compatible
<
typename color_space_type<SrcView>::type,
typename color_space_type<DstView>::type
>::value, "Source and destination views must have pixels with the same color space");

auto gaussian_kernel_1d = generate_1d_gaussian_kernel(kernel_size, sigma);

detail::convolve_1d
<
pixel<float, typename SrcView::value_type::layout_t>
>(src_view, gaussian_kernel_1d, dst_view, option);
}

}} //namespace boost::gil

#endif // !BOOST_GIL_IMAGE_PROCESSING_FILTER_HPP
21 changes: 21 additions & 0 deletions include/boost/gil/image_processing/numeric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,27 @@ inline detail::kernel_2d<T, Allocator> generate_gaussian_kernel(std::size_t side
return detail::kernel_2d<T, Allocator>(values.begin(), values.size(), middle, middle);
}

template <typename T = float, typename Allocator = std::allocator<T>>
inline kernel_1d<T, Allocator> generate_1d_gaussian_kernel(std::size_t side_length, double sigma)
{
if (side_length % 2 != 1)
throw std::invalid_argument("kernel dimensions should be odd");

const double denominator = 2 * sigma * sigma;
auto middle = side_length / 2;
std::vector<T, Allocator> values(side_length);
for (std::size_t x = 0; x <= middle; x++)
{
const auto delta_x = middle - x;
const double power = (delta_x * delta_x) / denominator;
const double numerator = std::exp(-power);
const float value = static_cast<float>(numerator / std::sqrt(denominator * M_PI));
values[x] = value;
values[side_length - 1 - x] = value;
}
return kernel_1d<T, Allocator>(values.begin(), values.size(), middle, middle);
}

/// \brief Generates Sobel operator in horizontal direction
/// \ingroup ImageProcessingMath
///
Expand Down
60 changes: 60 additions & 0 deletions test/core/image_processing/gaussian_blur.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Copyright 2020 Laxmikant Suryavanshi <laxmikantsuryavanshi@hotmail.com>
//
// 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)
//
#define BOOST_TEST_MODULE gil/test/core/image_processing/gaussian_filter
#include "unit_test.hpp"

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

namespace gil = boost::gil;

const float kernel[] =
{
0.241971, 0.398942, 0.241971
};

std::uint8_t img[] =
{
0, 0, 0, 0, 0,
0, 100, 100, 100, 0,
0, 100, 100, 100, 0,
0, 100, 100, 100, 0,
0, 0, 0, 0, 0
};

std::uint8 output[] =
{
5, 15, 21, 15, 5,
15, 41, 56, 41, 15,
21, 56, 77, 56, 21,
15, 41, 56, 41, 15,
5, 15, 21, 15, 5
};

BOOST_AUTO_TEST_SUITE(filter)

BOOST_AUTO_TEST_CASE(gaussian_filter_with_default_parameters)
{
gil::gray8c_view_t src_view =
gil::interleaved_view(5, 5, reinterpret_cast<const gil::gray8_pixel_t*>(img), 5);

gil::image<gil::gray8_pixel_t> temp_img(src_view.width(), src_view.height());
typename gil::image<gil::gray8_pixel_t>::view_t temp_view = view(temp_img);
gil::gray8_view_t dst_view(temp_view);

gil::gaussian_filter(src_view, dst_view, 3, 1.0f);

gil::gray8c_view_t out_view =
gil::interleaved_view(5, 5, reinterpret_cast<const gil::gray8_pixel_t*>(output), 5);

BOOST_TEST(gil::equal_pixels(out_view, dst_view));
}

BOOST_AUTO_TEST_SUITE_END()