segmentation
All Data Structures Namespaces Files Functions Variables Modules Pages
imutil.h
1 /*
2 Copyright (C) 2006 Pedro Felzenszwalb
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 /* some image utilities */
20 
21 #ifndef IMUTIL_H
22 #define IMUTIL_H
23 
24 #include "image.h"
25 #include "misc.h"
26 
27 /* compute minimum and maximum value in an image */
28 template <class T>
29 void min_max(image<T> *im, T *ret_min, T *ret_max) {
30  int width = im->width();
31  int height = im->height();
32 
33  T min = imRef(im, 0, 0);
34  T max = imRef(im, 0, 0);
35  for (int y = 0; y < height; y++) {
36  for (int x = 0; x < width; x++) {
37  T val = imRef(im, x, y);
38  if (min > val)
39  min = val;
40  if (max < val)
41  max = val;
42  }
43  }
44 
45  *ret_min = min;
46  *ret_max = max;
47 }
48 
49 /* threshold image */
50 template <class T>
51 image<uchar> *threshold(image<T> *src, int t) {
52  int width = src->width();
53  int height = src->height();
54  image<uchar> *dst = new image<uchar>(width, height);
55 
56  for (int y = 0; y < height; y++) {
57  for (int x = 0; x < width; x++) {
58  imRef(dst, x, y) = (imRef(src, x, y) >= t);
59  }
60  }
61 
62  return dst;
63 }
64 
65 #endif
66