filter.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Copyright (C) 2006 Pedro Felzenszwalb
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /* simple filters */
  16. #ifndef FILTER_H
  17. #define FILTER_H
  18. #include <vector>
  19. #include <cmath>
  20. #include "segmentation/felzenszwalb/image.h"
  21. #include "segmentation/felzenszwalb/misc.h"
  22. #include "segmentation/felzenszwalb/convolve.h"
  23. #include "segmentation/felzenszwalb/imconv.h"
  24. #define WIDTH 4.0
  25. namespace felzenszwalb{
  26. /* normalize mask so it integrates to one */
  27. static void normalize(std::vector<float> &mask) {
  28. int len = mask.size();
  29. float sum = 0;
  30. for (int i = 1; i < len; i++) {
  31. sum += fabs(mask[i]);
  32. }
  33. sum = 2*sum + fabs(mask[0]);
  34. for (int i = 0; i < len; i++) {
  35. mask[i] /= sum;
  36. }
  37. }
  38. /* make filters */
  39. #define MAKE_FILTER(name, fun) \
  40. static std::vector<float> make_ ## name (float sigma) { \
  41. sigma = std::max(sigma, 0.01F); \
  42. int len = (int)ceil(sigma * WIDTH) + 1; \
  43. std::vector<float> mask(len); \
  44. for (int i = 0; i < len; i++) { \
  45. mask[i] = fun; \
  46. } \
  47. return mask; \
  48. }
  49. MAKE_FILTER(fgauss, exp(-0.5*square(i/sigma)));
  50. /* convolve image with gaussian filter */
  51. static image<float> *smooth(image<float> *src, float sigma) {
  52. std::vector<float> mask = make_fgauss(sigma);
  53. normalize(mask);
  54. image<float> *tmp = new image<float>(src->height(), src->width(), false);
  55. image<float> *dst = new image<float>(src->width(), src->height(), false);
  56. convolve_even(src, tmp, mask);
  57. convolve_even(tmp, dst, mask);
  58. delete tmp;
  59. return dst;
  60. }
  61. /* convolve image with gaussian filter */
  62. image<float> *smooth(image<uchar> *src, float sigma) {
  63. image<float> *tmp = imageUCHARtoFLOAT(src);
  64. image<float> *dst = smooth(tmp, sigma);
  65. delete tmp;
  66. return dst;
  67. }
  68. /* compute laplacian */
  69. static image<float> *laplacian(image<float> *src) {
  70. int width = src->width();
  71. int height = src->height();
  72. image<float> *dst = new image<float>(width, height);
  73. #pragma omp parallel for
  74. for (int y = 1; y < height-1; y++) {
  75. for (int x = 1; x < width-1; x++) {
  76. float d2x = imRef(src, x-1, y) + imRef(src, x+1, y) -
  77. 2*imRef(src, x, y);
  78. float d2y = imRef(src, x, y-1) + imRef(src, x, y+1) -
  79. 2*imRef(src, x, y);
  80. imRef(dst, x, y) = d2x + d2y;
  81. }
  82. }
  83. return dst;
  84. }
  85. }//namespace
  86. #endif