File: convolve2d.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (54 lines) | stat: -rw-r--r-- 1,908 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// Copyright 2019 Miral Shah <miralshah2211@gmail.com>
// Copyright 2019 Mateusz Loskot <mateusz at loskot dot net>
// Copyright 2021 Pranam Lashkari <plashkari628@gmail.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 <vector>
#include <iostream>
#include <boost/gil/image_processing/kernel.hpp>
#include <boost/gil/image_processing/convolve.hpp>
#include <boost/gil/extension/io/png.hpp>

#include <boost/gil/extension/io/jpeg.hpp>

using namespace boost::gil;
using namespace std;

// Convolves the image with a 2d kernel.

// Note that the kernel can be fixed or resizable:
// kernel_2d_fixed<float, N> k(elements, centre_y, centre_x) produces a fixed kernel
// kernel_2d<float> k(elements, size, centre_y, centre_x) produces a resizable kernel
// The size of the kernel matrix is deduced as the square root of the number of the elements (9 elements yield a 3x3 matrix)

// See also:
// convolution.cpp - Convolution with 2d kernels


int main()
{
    gray8_image_t img;
    read_image("src_view.png", img, png_tag{});
    gray8_image_t img_out(img.dimensions()), img_out1(img.dimensions());

    std::vector<float> v(9, 1.0f / 9.0f);
    detail::kernel_2d<float> kernel(v.begin(), v.size(), 1, 1);
    detail::convolve_2d(view(img), kernel, view(img_out1));

    write_view("out-convolve2d.png", view(img_out1), png_tag{});

    std::vector<float> v1(3, 1.0f / 3.0f);
    kernel_1d<float> kernel1(v1.begin(), v1.size(), 1);

    detail::convolve_1d<gray32f_pixel_t>(const_view(img), kernel1, view(img_out), boundary_option::extend_zero);
    write_view("out-convolve_option_extend_zero.png", view(img_out), png_tag{});

    if (equal_pixels(view(img_out1), view(img_out)))
      cout << "convolve_option_extend_zero" << endl;

    return 0;
}