Blame view

3rdparty/boost_1_81_0/libs/gil/example/convolution.cpp 3.28 KB
977ed18d   Hu Chunming   提交三方库
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  //
  // Copyright 2005-2007 Adobe Systems Incorporated
  // Copyright 2021 Pranam Lashkari <plashkari628@gmail.com>
  //
  // Distributed under 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/jpeg.hpp>
  #include <boost/gil/image_processing/kernel.hpp>
  #include <boost/gil/image_processing/convolve.hpp>
  
  // Convolves the image with a Gaussian kernel.
  
  // Note that the kernel can be fixed or resizable:
  // kernel_1d_fixed<float, N> k(matrix, centre) produces a fixed kernel
  // kernel_1d<float> k(matrix, size, centre) produces a resizable kernel
  
  // Work can be done row by row and column by column, as in this example,
  // using the functions convolve_rows and convolve_cols (or their _fixed counterpart)
  // but the header boost/gil/image_processing/convolve.hpp also offers the function convolve_1d which combines the two.
  
  // See also:
  // convolve2d.cpp - Convolution with 2d kernels
  
  int main() {
      using namespace boost::gil;
  
      rgb8_image_t img;
      read_image("test.jpg", img, jpeg_tag{});
  
      // Convolve the rows and the columns of the image with a fixed kernel
      rgb8_image_t convolved(img);
  
      // radius-1 Gaussian kernel, size 9
      float gaussian_1[]={0.00022923296f,0.0059770769f,0.060597949f,0.24173197f,0.38292751f,
                          0.24173197f,0.060597949f,0.0059770769f,0.00022923296f};
      /*
      // radius-2 Gaussian kernel, size 15
      float gaussian_2[]={
          0.00048869418f,0.0024031631f,0.0092463447f,
          0.027839607f,0.065602221f,0.12099898f,0.17469721f,
          0.19744757f,
          0.17469721f,0.12099898f,0.065602221f,0.027839607f,
          0.0092463447f,0.0024031631f,0.00048869418f
      };
      //radius-3 Gaussian kernel, size 23
      float gaussian_3[]={
          0.00016944126f,0.00053842377f,0.0015324751f,0.0039068931f,
          0.0089216027f,0.018248675f,0.033434924f,0.054872241f,
          0.080666073f,0.10622258f,0.12529446f,
          0.13238440f,
          0.12529446f,0.10622258f,0.080666073f,
          0.054872241f,0.033434924f,0.018248675f,0.0089216027f,
          0.0039068931f,0.0015324751f,0.00053842377f,0.00016944126f
      };
      //radius-4 Gaussian kernel, size 29
      float gaussian_4[]={
          0.00022466264f,0.00052009715f,0.0011314391f,0.0023129794f,
          0.0044433107f,0.0080211498f,0.013606987f,0.021691186f,
          0.032493830f,0.045742013f,0.060509924f,0.075220309f,
          0.087870099f,0.096459411f,0.099505201f,0.096459411f,0.087870099f,
          0.075220309f,0.060509924f,0.045742013f,0.032493830f,
          0.021691186f,0.013606987f,0.0080211498f,0.0044433107f,
          0.0023129794f,0.0011314391f,0.00052009715f,0.00022466264f,
      };
      */
  
      kernel_1d_fixed<float,9> kernel(gaussian_1,4);
      convolve_rows_fixed<rgb32f_pixel_t>(const_view(convolved),kernel,view(convolved));
      convolve_cols_fixed<rgb32f_pixel_t>(const_view(convolved),kernel,view(convolved));
      write_view("out-convolution.jpg", view(convolved), jpeg_tag{});
  
      // This is how to use a resizable kernel
      kernel_1d<float> kernel2(gaussian_1,9,4);
      convolve_rows<rgb32f_pixel_t>(const_view(img),kernel2,view(img));
      convolve_cols<rgb32f_pixel_t>(const_view(img),kernel2,view(img));
      write_view("out-convolution2.jpg", view(img), jpeg_tag{});
  
      return 0;
  }