convolution.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. #include <boost/gil.hpp>
  8. #include <boost/gil/extension/io/jpeg.hpp>
  9. #include <boost/gil/extension/numeric/kernel.hpp>
  10. #include <boost/gil/extension/numeric/convolve.hpp>
  11. // Example for convolve_rows() and convolve_cols() in the numeric extension
  12. int main() {
  13. using namespace boost::gil;
  14. rgb8_image_t img;
  15. read_image("test.jpg", img, jpeg_tag{});
  16. // Convolve the rows and the columns of the image with a fixed kernel
  17. rgb8_image_t convolved(img);
  18. // radius-1 Gaussian kernel, size 9
  19. float gaussian_1[]={0.00022923296f,0.0059770769f,0.060597949f,0.24173197f,0.38292751f,
  20. 0.24173197f,0.060597949f,0.0059770769f,0.00022923296f};
  21. /*
  22. // radius-2 Gaussian kernel, size 15
  23. float gaussian_2[]={
  24. 0.00048869418f,0.0024031631f,0.0092463447f,
  25. 0.027839607f,0.065602221f,0.12099898f,0.17469721f,
  26. 0.19744757f,
  27. 0.17469721f,0.12099898f,0.065602221f,0.027839607f,
  28. 0.0092463447f,0.0024031631f,0.00048869418f
  29. };
  30. //radius-3 Gaussian kernel, size 23
  31. float gaussian_3[]={
  32. 0.00016944126f,0.00053842377f,0.0015324751f,0.0039068931f,
  33. 0.0089216027f,0.018248675f,0.033434924f,0.054872241f,
  34. 0.080666073f,0.10622258f,0.12529446f,
  35. 0.13238440f,
  36. 0.12529446f,0.10622258f,0.080666073f,
  37. 0.054872241f,0.033434924f,0.018248675f,0.0089216027f,
  38. 0.0039068931f,0.0015324751f,0.00053842377f,0.00016944126f
  39. };
  40. //radius-4 Gaussian kernel, size 29
  41. float gaussian_4[]={
  42. 0.00022466264f,0.00052009715f,0.0011314391f,0.0023129794f,
  43. 0.0044433107f,0.0080211498f,0.013606987f,0.021691186f,
  44. 0.032493830f,0.045742013f,0.060509924f,0.075220309f,
  45. 0.087870099f,0.096459411f,0.099505201f,0.096459411f,0.087870099f,
  46. 0.075220309f,0.060509924f,0.045742013f,0.032493830f,
  47. 0.021691186f,0.013606987f,0.0080211498f,0.0044433107f,
  48. 0.0023129794f,0.0011314391f,0.00052009715f,0.00022466264f,
  49. };
  50. */
  51. kernel_1d_fixed<float,9> kernel(gaussian_1,4);
  52. convolve_rows_fixed<rgb32f_pixel_t>(const_view(convolved),kernel,view(convolved));
  53. convolve_cols_fixed<rgb32f_pixel_t>(const_view(convolved),kernel,view(convolved));
  54. write_view("out-convolution.jpg", view(convolved), jpeg_tag{});
  55. // This is how to use a resizable kernel
  56. kernel_1d<float> kernel2(gaussian_1,9,4);
  57. convolve_rows<rgb32f_pixel_t>(const_view(img),kernel2,view(img));
  58. convolve_cols<rgb32f_pixel_t>(const_view(img),kernel2,view(img));
  59. write_view("out-convolution2.jpg", view(img), jpeg_tag{});
  60. return 0;
  61. }