convolve.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. /* convolution */
  16. #ifndef CONVOLVE_H
  17. #define CONVOLVE_H
  18. #ifdef NICE_USELIB_OPENMP
  19. #include <omp.h>
  20. #endif
  21. #include <vector>
  22. #include <algorithm>
  23. #include <cmath>
  24. #include "segmentation/felzenszwalb/image.h"
  25. namespace felzenszwalb {
  26. /* convolve src with mask. dst is flipped! */
  27. static void convolve_even(image<float> *src, image<float> *dst,
  28. std::vector<float> &mask) {
  29. int width = src->width();
  30. int height = src->height();
  31. int len = mask.size();
  32. #pragma omp parallel for
  33. for (int y = 0; y < height; y++) {
  34. for (int x = 0; x < width; x++) {
  35. float sum = mask[0] * imRef(src, x, y);
  36. for (int i = 1; i < len; i++) {
  37. sum += mask[i] *
  38. (imRef(src, std::max(x - i, 0), y) +
  39. imRef(src, std::min(x + i, width - 1), y));
  40. }
  41. imRef(dst, y, x) = sum;
  42. }
  43. }
  44. }
  45. /* convolve src with mask. dst is flipped! */
  46. static void convolve_odd(image<float> *src, image<float> *dst,
  47. std::vector<float> &mask) {
  48. int width = src->width();
  49. int height = src->height();
  50. int len = mask.size();
  51. #pragma omp parallel for
  52. for (int y = 0; y < height; y++) {
  53. for (int x = 0; x < width; x++) {
  54. float sum = mask[0] * imRef(src, x, y);
  55. for (int i = 1; i < len; i++) {
  56. sum += mask[i] *
  57. (imRef(src, std::max(x - i, 0), y) -
  58. imRef(src, std::min(x + i, width - 1), y));
  59. }
  60. imRef(dst, y, x) = sum;
  61. }
  62. }
  63. }
  64. }//namespace
  65. #endif