OMEGA Pipeline Reference Manual  1.0.5
omega_stats.c
1 /* $Id: omega_stats.c,v 1.2 2012-01-31 13:26:37 agabasch Exp $
2  *
3  * This file is part of the OMEGA Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: agabasch $
23  * $Date: 2012-01-31 13:26:37 $
24  * $Revision: 1.2 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <math.h>
39 
40 #include "omega_dfs.h"
41 #include "omega_stats.h"
42 #include "omega_utils.h"
43 
44 
59 /*----------------------------------------------------------------------------*/
62 /*-------------------------------------------------------------------------
63 
64  VARIOUS ROUTINES TO CALCULATE STATISTICS
65 
66 
67 -------------------------------------------------------------------------*/
68 
69 /*-------------------------------------------------------------------------*/
82 /*--------------------------------------------------------------------------*/
83 cpl_stats * omega_iter_stat(cpl_image *img, double threshold, int iter)
84 {
85 
86  int i = 0;
87  double mean = 0.0;
88  double median = 0.0;
89  double stdev = 0.0;
90 
91  cpl_mask *mask;
92  cpl_image *image;
93  cpl_stats *stats = NULL;
94 
95  if(img == NULL)
96  return NULL;
97 
98  image = cpl_image_duplicate(img);
99 
100  stats = cpl_stats_new_from_image(image, CPL_STATS_ALL);
101 
102  for(i=0; i<iter; i++){
103  mean = cpl_stats_get_mean(stats);
104  median = cpl_stats_get_median(stats);
105  stdev = cpl_stats_get_stdev(stats);
106 
107  mask = cpl_mask_threshold_image_create(image,median-threshold*stdev,median+threshold*stdev);
108  cpl_mask_not(mask);
109  cpl_image_reject_from_mask(image, mask);
110  freemask(mask);
111 
112  freestats(stats);
113 
114  stats = cpl_stats_new_from_image(image, CPL_STATS_ALL);
115  if((abs(stdev/cpl_stats_get_stdev(stats)-1) <0.01)){
116  break;
117  }
118 
119  }
120 
121  freeimage(image);
122 
123  return stats;
124 }
125 
126 /*-------------------------------------------------------------------------*/
139 /*--------------------------------------------------------------------------*/
140 cpl_stats * omega_iter_stat_opts(cpl_image *img, cpl_vector *zone, double threshold, int iter)
141 {
142 
143  int x0 = 0;
144  int zy0 = 0;
145  int x1 = 0;
146  int zy1 = 0;
147  int i = 0;
148  double mean = 0.0;
149  double median = 0.0;
150  double stdev = 0.0;
151 
152  cpl_mask *mask;
153  cpl_stats *stats = NULL;
154  cpl_image *image;
155 
156  if(img == NULL)
157  return NULL;
158 
159  image = cpl_image_duplicate(img);
160 
161 
162  if(zone != NULL){
163  x0 = (int)cpl_vector_get(zone,0);
164  zy0 = (int)cpl_vector_get(zone,1);
165  x1 = (int)cpl_vector_get(zone,2);
166  zy1 = (int)cpl_vector_get(zone,3);
167  stats = cpl_stats_new_from_image_window(image, CPL_STATS_ALL,x0,zy0,x1,zy1);
168  }
169  else{
170  stats = cpl_stats_new_from_image(image, CPL_STATS_ALL);
171  }
172 
173  for(i=0; i<iter; i++){
174 
175  mean = cpl_stats_get_mean(stats);
176  median = cpl_stats_get_median(stats);
177  stdev = cpl_stats_get_stdev(stats);
178 
179  mask = cpl_mask_threshold_image_create(image,median-threshold*stdev,median+threshold*stdev);
180  cpl_mask_not(mask);
181  cpl_image_reject_from_mask(image, mask);
182  freemask(mask);
183  freestats(stats);
184 
185  if(zone != NULL){
186  stats = cpl_stats_new_from_image_window(image, CPL_STATS_ALL,x0,zy0,x1,zy1);
187  }
188  else{
189  stats = cpl_stats_new_from_image(image, CPL_STATS_ALL);
190  }
191  if( (abs(mean/cpl_stats_get_mean(stats)-1) < 0.01) &&
192  (abs(stdev/cpl_stats_get_stdev(stats)-1) < 0.01) ){
193  break;
194  }
195 
196  }
197 
198  freeimage(image);
199 
200  return stats;
201 }
202 
203 /*-----------------------------------------------------------
204  Hacked from cpl_tools
205 ------------------------------------------------------------*/
206 float get_median_float(float *a, int n)
207 {
208  return get_kth_float(a,n,(((n)&1)?((n)/2):(((n)/2)-1)));
209 }
210 /*----------------------------------------------------------------------------*/
219 /*----------------------------------------------------------------------------*/
220 float get_kth_float(float *a, int n, int k)
221 {
222  const char *_id = "get_kth_float:";
223  register float x ;
224  register int i, j, l, m ;
225 
226 /* cpl_assure(a, CPL_ERROR_NULL_INPUT, "cpl_tools_get_kth_float", 0.00) ;*/
227 
228  if(a == NULL){
229  cpl_msg_error("","%s Error in input for median of array",_id);
230  return 0;
231  }
232 
233  l=0 ; m=n-1 ;
234  while (l<m) {
235  x=a[k] ;
236  i=l ;
237  j=m ;
238  do {
239  while (a[i]<x) i++ ;
240  while (x<a[j]) j-- ;
241  if (i<=j) {
242  FLOAT_SWAP(a[i],a[j]) ;
243  i++ ; j-- ;
244  }
245  } while (i<=j) ;
246  if (j<k) l=i ;
247  if (k<i) m=j ;
248  }
249 
250  return a[k] ;
251 }