filter.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "image.h"
  21. #include "misc.h"
  22. #include "convolve.h"
  23. #include "imconv.h"
  24. #define WIDTH 4.0
  25. /* normalize mask so it integrates to one */
  26. static void normalize(std::vector<float> &mask) {
  27. int len = mask.size();
  28. float sum = 0;
  29. for (int i = 1; i < len; i++) {
  30. sum += fabs(mask[i]);
  31. }
  32. sum = 2*sum + fabs(mask[0]);
  33. for (int i = 0; i < len; i++) {
  34. mask[i] /= sum;
  35. }
  36. }
  37. /* make filters */
  38. #define MAKE_FILTER(name, fun) \
  39. static std::vector<float> make_ ## name (float sigma) { \
  40. sigma = std::max(sigma, 0.01F); \
  41. int len = (int)ceil(sigma * WIDTH) + 1; \
  42. std::vector<float> mask(len); \
  43. for (int i = 0; i < len; i++) { \
  44. mask[i] = fun; \
  45. } \
  46. return mask; \
  47. }
  48. MAKE_FILTER(fgauss, exp(-0.5*square(i/sigma)));
  49. /* convolve image with gaussian filter */
  50. static image<float> *smooth(image<float> *src, float sigma) {
  51. std::vector<float> mask = make_fgauss(sigma);
  52. normalize(mask);
  53. image<float> *tmp = new image<float>(src->height(), src->width(), false);
  54. image<float> *dst = new image<float>(src->width(), src->height(), false);
  55. convolve_even(src, tmp, mask);
  56. convolve_even(tmp, dst, mask);
  57. delete tmp;
  58. return dst;
  59. }
  60. /* convolve image with gaussian filter */
  61. image<float> *smooth(image<uchar> *src, float sigma) {
  62. image<float> *tmp = imageUCHARtoFLOAT(src);
  63. image<float> *dst = smooth(tmp, sigma);
  64. delete tmp;
  65. return dst;
  66. }
  67. /* compute laplacian */
  68. static image<float> *laplacian(image<float> *src) {
  69. int width = src->width();
  70. int height = src->height();
  71. image<float> *dst = new image<float>(width, height);
  72. for (int y = 1; y < height-1; y++) {
  73. for (int x = 1; x < width-1; x++) {
  74. float d2x = imRef(src, x-1, y) + imRef(src, x+1, y) -
  75. 2*imRef(src, x, y);
  76. float d2y = imRef(src, x, y-1) + imRef(src, x, y+1) -
  77. 2*imRef(src, x, y);
  78. imRef(dst, x, y) = d2x + d2y;
  79. }
  80. }
  81. return dst;
  82. }
  83. #endif