OMEGA Pipeline Reference Manual  1.0.5
omega_satellites.c
1 /* $Id: omega_satellites.c,v 1.6 2013-06-18 07:07:17 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: 2013-06-18 07:07:17 $
24  * $Revision: 1.6 $
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 #include <string.h>
36 #include <math.h>
37 
38 #include "omega_dfs.h"
39 #include "omega_background.h"
40 #include "omega_utils.h"
41 #include "omega_satellites.h"
42 
43 
60 /*----------------------------------------------------------------------------*/
64 /*----------------------------------------------------------------------------*/
80 /*----------------------------------------------------------------------------*/
81 /*cpl_mask * detsat(cpl_image *science, cpl_image *flat, cpl_image *weight0, cpl_parameterlist *hpars)*/
82 cpl_mask * detsat(cpl_image *nscience, cpl_parameterlist *hpars)
83 {
84 
85  int count = 0;
86  int i = 0;
87  int j = 0;
88  int npars = 0;
89  int nx = 0;
90  int ny = 0;
91 
92  double dthre = 5.0;
93  double hthre = 1000.0;
94  double stdev = 1.0;
95  double median = 0.;
96 
97  float threshold = 0.0f;
98 
99  const char *_id = "detsat";
100  const char *alias = NULL;
101 /* const char *bckname = "OMEGA_back0062.fits";*/
102 
103  cpl_parameter *par;
104  cpl_image *hough;
105 /* cpl_image *nscience;*/
106 /* cpl_image *temp;*/
107 /* cpl_image *back;*/
108  cpl_mask *hough_map;
109 /* cpl_mask *mask;*/
110  cpl_stats *stats;
111 
112 
113  if(!hpars) {
114  cpl_msg_error ("", "%s Parameters list not found", _id);
115  return NULL;
116  }
117 
118 
119 /* Get parameters*/
120  npars = cpl_parameterlist_get_size(hpars);
121  par = cpl_parameterlist_get_first(hpars);
122 
123  for(i=0; i<npars; i++) {
124  alias = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
125  if(strcmp("det-sate", alias) == 0) {
126  dthre = cpl_parameter_get_double(par) ;
127  j++;
128  }
129  else if(strcmp("hough-thre", alias) == 0) {
130  hthre = cpl_parameter_get_double(par) ;
131  j++;
132  }
133  par = cpl_parameterlist_get_next(hpars);
134  if(j == 2) break;
135  }
136 
137 
138  stats = cpl_stats_new_from_image (nscience, CPL_STATS_ALL);
139  if(stats == NULL) {
140  cpl_msg_warning("","%s Cannot calculate image statistics",_id);
141  stdev = 1.0;
142  median=0.;
143  cpl_error_reset();
144  }
145  else{
146  stdev = cpl_stats_get_median_dev(stats);
147  median = cpl_stats_get_median(stats);
148  if(stdev == 0.0) stdev = 1.0;
149  cpl_stats_delete(stats);
150  }
151  threshold = median + (float)(dthre * stdev);
152  //threshold = (float)(dthre * stdev);
153 
154 
155  hough = hough_transform(nscience, threshold);
156  if(hough == NULL) {
157  cpl_msg_warning("","%s Error in Hough Transform", _id);
158  cpl_image_delete(hough);
159  return NULL;
160  }
161  threshold = (float)hthre;
162 
163  nx = cpl_image_get_size_x(nscience);
164  ny = cpl_image_get_size_y(nscience);
165 
166  hough_map = hough_inverse_transform(hough, threshold, nx, ny);
167  if(hough_map == NULL) {
168  cpl_msg_warning("","%s Error in Inverse Hough Transform", _id);
169  cpl_image_delete(hough);
170  cpl_mask_delete(hough_map);
171  return NULL;
172  }
173 
174 
175  count = cpl_mask_count(hough_map);
176  if(count == -1) {
177  cpl_msg_info("","No satellite tracks were detected in image");
178  }
179  else {
180  cpl_msg_info("","Detected %d satellite tracks in image", count);
181  }
182 
183 /*FIXME: Don't know why but the satellites map need to have a mask_not*/
184  //cpl_mask_not(hough_map);
185 
186  cpl_image_delete(hough);
187 
188  return hough_map;
189 
190 }
191 
192 /* ************************************************************************
193  Routines to compute Hough Transform
194  Converted from Eclipse (in OMEGA pae2)
195 **************************************************************************/
204 /*--------------------------------------------------------------------------*/
205 cpl_image *hough_transform (cpl_image *img, float thre)
206 {
207 
208  int i = 0;
209  int j = 0;
210  int l = 0;
211  int k = 0;
212  int nx = 0;
213  int ny = 0;
214  int nrho = 0;
215  int ntheta = 0;
216 
217  double drho = 0.0;
218  double theta = 0.0;
219  double dtheta = 0.0;
220  double x = 0.0;
221  double y = 0.0;
222 
223  float *sins;
224  float *coss;
225  float *hough_data;
226  float *img_data;
227 
228  const char *_id = "hough_transform:";
229 
230  cpl_image *hough_img;
231 
232 
233 
234 /* Get input image */
235  if (img == NULL) {
236  cpl_msg_error("","<%s> No input image",_id);
237  return NULL;
238  }
239 
240 /* Extract the image data */
241  img_data = (float *)cpl_image_get_data(img);
242 
243  drho = 1.0/sqrt(2.0);
244  nx = cpl_image_get_size_x (img);
245  ny = cpl_image_get_size_y (img);
246 
247  nrho = (int)(sqrt(nx*nx + ny*ny)/drho) + 1;
248  ntheta = 3600;
249  dtheta = 2*PI/(double)ntheta;
250 
251  hough_img = cpl_image_new(nrho,ntheta,CPL_TYPE_FLOAT);
252  if(hough_img == NULL) {
253  cpl_msg_error("","<%s>Cannot create Hough image",_id);
254  return NULL;
255  }
256 
257  hough_data = (float *)cpl_image_get_data(hough_img);
258 
259 
260  for(i=0; i<nrho*ntheta; i++) {
261  hough_data[i] = (float)0.0;
262  }
263 
264  sins = calloc(1, ntheta*sizeof(float));
265  if(sins == NULL) {
266  cpl_msg_error("","%s Error in allocating memory", _id);
267  cpl_image_delete(hough_img);
268  return NULL;
269  }
270 
271  coss = calloc(1, ntheta*sizeof(float));
272  if(coss == NULL) {
273  cpl_msg_error("","%s Error in allocating memory", _id);
274  cpl_image_delete(hough_img);
275  free(sins);
276  return NULL;
277  }
278 
279  for(i=0; i<ntheta; i++){
280  theta = ((double)i + 0.5) * dtheta;
281  sins[i] = sin(theta);
282  coss[i] = cos(theta);
283  }
284 
285  for(j=0; j<ny; j++){
286  y = (double)j;
287  for(i=0; i<nx; i++){
288  if(img_data[j*nx+i] > thre){
289  x = (double)i;
290  for(k=0; k<ntheta; k++){
291  l = (int)floor((x*coss[k] + y*sins[k]) / drho);
292  hough_data[k*nrho+l] += (float)1.0;
293  }
294  }
295  }
296  }
297 
298  free(sins);
299  free(coss);
300 
301  return hough_img;
302 }
303 
304 
305 cpl_mask *hough_inverse_transform(cpl_image *himg, float thre, int lx, int ly)
306 {
307 
308  int i = 0;
309  int j = 0;
310  int k = 0;
311  int l = 0;
312  int hnx = 0;
313  int hny = 0;
314  int nrho = 0;
315  int ntheta = 0;
316 
317  double rho = 0.0;
318  double drho = 0.0;
319  double theta = 0.0;
320  double dtheta = 0.0;
321  double edge = 0.0;
322  double a = 0.0;
323  double b = 0.0;
324 
325  float *hdata;
326 
327  const char *_id = "hough_inverse_transform:";
328 
329  cpl_mask *pmap;
330  cpl_binary *pdata;
331 
332 
333  if(himg == NULL) {
334  cpl_msg_error("","<%s> No input image",_id);
335  return NULL;
336  }
337 
338  drho = 1.0/sqrt(2.0);
339  nrho = (int)(sqrt(lx*lx + ly*ly)/drho) + 1;
340  ntheta = 3600;
341  dtheta = 2*PI / (double)ntheta;
342 
343  hnx = cpl_image_get_size_x(himg);
344  hny = cpl_image_get_size_y(himg);
345 
346 
347  if( !((nrho == hnx) && (ntheta == hny)) ) {
348  cpl_msg_error("","%s Dimensions of Hough image are incompatible",_id);
349  return NULL;
350  }
351 
352  pmap = cpl_mask_new(lx, ly);
353  if(pmap == NULL) {
354  cpl_msg_error("","<%s> Error creating pixelmap",_id);
355  return NULL;
356  }
357 
358  pdata = cpl_mask_get_data(pmap);
359 
360  edge = sqrt(2.0)/2.0;
361 
362  hdata = (float *)cpl_image_get_data(himg);
363 
364  for(j=0; j<ntheta; j++) {
365  theta = ((double)j + 0.5) * dtheta;
366  for(i=0; i<nrho; i++) {
367  rho = (double)i * drho;
368  if( hdata[j*nrho + i] > thre ) {
369 
370  if( fabs(sin(theta) > edge) ) {
371  /*compute y = a*x+b */
372  a = -cos(theta) / sin(theta);
373  b = rho/sin(theta);
374  for(l=0; l<lx; l++){
375  k = floor( (double)l * a + b);
376  if( (k>0) && (k<ly) ){
377  pdata[k*lx+l] = CPL_BINARY_1;
378  }
379  }
380  }
381  else {
382  /*compute x =a * y + b */
383  a = -sin(theta) / cos(theta);
384  b = rho/cos(theta);
385  for(k=0; k<ly; k++){
386  l = floor( (double)k * a + b);
387  if( (l>=0) && (l<lx) ){
388  pdata[k*lx+l] = CPL_BINARY_1;
389  }
390  }
391  }
392  }
393  }
394  }
395 
396  return pmap;
397 }
398 
399