imutil.h 1.6 KB

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