BRL-CAD
stat.c
Go to the documentation of this file.
1 /* S T A T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libicv/stat.c
21  *
22  * This file contains image statistics and histogram routines.
23  *
24  */
25 
26 #include "bu/magic.h"
27 #include "bu/malloc.h"
28 #include "icv.h"
29 
30 HIDDEN size_t **
31 icv_init_bins(icv_image_t* img, int n_bins)
32 {
33  int c;
34  int i;
35  size_t **bins;
36 
37  bins = (size_t**) bu_malloc(sizeof(size_t*)*img->channels, "icv_init_bins : Histogram Bins");
38  for (c = 0; c <= img->channels; c++) {
39  bins[c] = (size_t*) bu_malloc(sizeof(size_t)*n_bins, "icv_init_bins : Histogram Array for Channels");
40  for (i = 0; i < n_bins; i++) {
41  bins[c][i] = 0;
42  }
43  }
44  return bins;
45 }
46 
47 
48 size_t **
49 icv_hist(icv_image_t* img, int n_bins)
50 {
51  long int i;
52  int j;
53  double *data;
54  int temp;
55  long int size;
56  size_t **bins;
57  size = img->width*img->height;
58  data = img->data;
59 
60  ICV_IMAGE_VAL_PTR(img);
61 
62  bins = icv_init_bins(img, n_bins);
63 
64  for (i = 0; i <= size; i++) {
65  for (j = 0; j < img->channels; j++) {
66  temp = (*data++)*n_bins;
67  bins[j][temp]++;
68  }
69  }
70  return bins;
71 }
72 
73 
74 double *
76 {
77  double *data = NULL;
78  size_t size;
79  double *max; /**< An array of size channels. */
80  int i;
81 
82  ICV_IMAGE_VAL_PTR(img);
83 
84  max = (double *)bu_malloc(sizeof(double)*img->channels, "max values");
85 
86  for (i = 0; i < img->channels; i++)
87  max[i] = 0.0;
88 
89  data = img->data;
90 
91  for (size = img->width*img->height; size>0; size--)
92  for (i = 0; i < img->channels; i++)
93  if (max[i] > *data++)
94  max[i] = *(data-1);
95 
96  return max;
97 }
98 
99 
100 double *
102 {
103  double *data = NULL;
104 
105  double *sum; /**< An array of size channels. */
106  int i;
107  size_t size,j;
108 
109  ICV_IMAGE_VAL_PTR(img);
110 
111  sum = (double *)bu_malloc(sizeof(double)*img->channels, "sum values");
112 
113  for (i = 0; i < img->channels; i++)
114  sum[i] = 0.0;
115 
116  data = img->data;
117  size = (size_t)img->width*img->height;
118 
119  for (j = 0; j < size; j++)
120  for (i = 0; i < img->channels; i++)
121  sum[i] += *data++;
122 
123  return sum;
124 }
125 
126 
127 double *
129 {
130  double *mean;
131  size_t size;
132  int i;
133 
134  ICV_IMAGE_VAL_PTR(img);
135 
136  mean = icv_sum(img); /**< receives sum from icv_image_sum*/
137  size = (size_t)img->width*img->height;
138 
139  for (i = 0; i < img->channels; i++)
140  mean[i]/=size;
141 
142  return mean;
143 }
144 
145 
146 double *
148 {
149  double *data = NULL;
150  size_t size;
151  double *min; /**< An array of size channels. */
152  int i;
153 
154  ICV_IMAGE_VAL_PTR(img);
155 
156  min = (double *)bu_malloc(sizeof(double)*img->channels, "min values");
157 
158  for (i = 0; i < img->channels; i++)
159  min[i] = 1.0;
160 
161  data = img->data;
162 
163  for (size = (size_t)img->width*img->height; size>0; size--) {
164  for (i = 0; i < img->channels; i++)
165  if (min[i] < *data++)
166  min[i] = *(data-1);
167  }
168 
169  return min;
170 }
171 
172 
173 double *
174 icv_var(icv_image_t* img, size_t** bins, int n_bins)
175 {
176  int i,c;
177  double *var;
178  double *mean;
179  size_t size;
180  double d;
181 
182  ICV_IMAGE_VAL_PTR(img);
183 
184  var = (double *) bu_malloc(sizeof(double)*img->channels, "variance values");
185 
186  size = (size_t) img->height*img->width;
187 
188  mean = icv_mean(img);
189  for (i = 0; i < n_bins; i++) {
190  for (c = 0; c < img->channels; c++) {
191  d = (double)i - n_bins*mean[c];
192  var[c] += bins[c][i] * d * d;
193  }
194  }
195 
196  for (c = 0; c < img->channels; c++) {
197  var[c] /= size;
198  }
199 
200  return var;
201 }
202 
203 
204 double *
205 icv_skew(icv_image_t* img, size_t** bins, int n_bins)
206 {
207  int i,c;
208  double *skew;
209  double *mean;
210  size_t size;
211  double d;
212 
213  ICV_IMAGE_VAL_PTR(img);
214 
215  skew = (double *)bu_malloc(sizeof(double)*img->channels, "skewness values");
216 
217  size = (size_t) img->height*img->width;
218 
219  mean = icv_mean(img);
220  for (i = 0; i < n_bins; i++) {
221  for (c = 0; c < img->channels; c++) {
222  d = (double)i - n_bins*mean[c];
223  skew[c] += bins[c][i] * d * d *d;
224  }
225  }
226 
227  for (c = 0; c < img->channels; c++) {
228  skew[c] /= size;
229  }
230 
231  return skew;
232 }
233 
234 
235 int *
236 icv_median(icv_image_t* img, size_t** bins, int n_bins)
237 {
238  int i,c;
239  int *median;
240  double *sum;
241  double *partial_sum;
242 
243  ICV_IMAGE_VAL_PTR(img);
244 
245  median = (int *)bu_malloc(sizeof(int)*img->channels, "median values");
246  partial_sum = (double *)bu_malloc(sizeof(int)*img->channels, "partial sum values");
247 
248  sum = icv_sum(img);
249 
250  for (c = 0; c < img->channels; c++) {
251  partial_sum[c] = 0;
252  for (i = 0; i < n_bins; i++) {
253  if (partial_sum[c] < sum[c]/2) {
254  partial_sum[c] += i*bins[c][i];
255  median[c] = i;
256  } else break;
257  }
258  }
259 
260  bu_free(partial_sum, "icv_median : partial sum values\n");
261 
262  return median;
263 }
264 
265 
266 int *
267 icv_mode(icv_image_t* img, size_t** bins, int n_bins)
268 {
269  int i,c;
270  int *mode;
271 
272  ICV_IMAGE_VAL_PTR(img);
273 
274  mode = (int *) bu_malloc(sizeof(int)*img->channels, "mode values");
275 
276  for (c = 0; c < img->channels; c++) {
277  mode[c] = 0;
278  for (i = 0; i < n_bins; i++)
279  if (bins[c][mode[c]] < bins[c][i])
280  mode[c] = i;
281  }
282  return mode;
283 }
284 
285 /*
286  * Local Variables:
287  * tab-width: 8
288  * mode: C
289  * indent-tabs-mode: t
290  * c-file-style: "stroustrup"
291  * End:
292  * ex: shiftwidth=4 tabstop=8
293  */
double * data
Definition: icv.h:89
double * icv_var(icv_image_t *img, size_t **bins, int n_bins)
Definition: stat.c:174
int width
Definition: icv.h:91
size_t ** icv_hist(icv_image_t *img, int n_bins)
Definition: stat.c:49
int * icv_mode(icv_image_t *img, size_t **bins, int n_bins)
Definition: stat.c:267
int channels
Definition: icv.h:91
Definition: icv.h:86
int height
Definition: icv.h:91
#define HIDDEN
Definition: common.h:86
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
double * icv_skew(icv_image_t *img, size_t **bins, int n_bins)
Definition: stat.c:205
COMPLEX data[64]
Definition: fftest.c:34
#define ICV_IMAGE_VAL_PTR(_i)
Definition: icv.h:128
double * icv_min(icv_image_t *img)
Definition: stat.c:147
double * icv_mean(icv_image_t *img)
Definition: stat.c:128
HIDDEN size_t ** icv_init_bins(icv_image_t *img, int n_bins)
Definition: stat.c:31
int * icv_median(icv_image_t *img, size_t **bins, int n_bins)
Definition: stat.c:236
double * icv_sum(icv_image_t *img)
Definition: stat.c:101
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
double * icv_max(icv_image_t *img)
Definition: stat.c:75