imutil.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. /* some image utilities */
  16. #ifndef IMUTIL_H
  17. #define IMUTIL_H
  18. #include "segmentation/felzenszwalb/image.h"
  19. #include "segmentation/felzenszwalb/misc.h"
  20. namespace felzenszwalb {
  21. /* compute minimum and maximum value in an image */
  22. template <class T>
  23. void min_max(image<T> *im, T *ret_min, T *ret_max) {
  24. int width = im->width();
  25. int height = im->height();
  26. T min = imRef(im, 0, 0);
  27. T max = imRef(im, 0, 0);
  28. for (int y = 0; y < height; y++) {
  29. for (int x = 0; x < width; x++) {
  30. T val = imRef(im, x, y);
  31. if (min > val)
  32. min = val;
  33. if (max < val)
  34. max = val;
  35. }
  36. }
  37. *ret_min = min;
  38. *ret_max = max;
  39. }
  40. /* threshold image */
  41. template <class T>
  42. image<uchar> *threshold(image<T> *src, int t) {
  43. int width = src->width();
  44. int height = src->height();
  45. image<uchar> *dst = new image<uchar>(width, height);
  46. for (int y = 0; y < height; y++) {
  47. for (int x = 0; x < width; x++) {
  48. imRef(dst, x, y) = (imRef(src, x, y) >= t);
  49. }
  50. }
  51. return dst;
  52. }
  53. }//namespace
  54. #endif