00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035 #include <string.h>
00036 #include <math.h>
00037
00038 #include "omega_dfs.h"
00039 #include "omega_background.h"
00040 #include "omega_utils.h"
00041 #include "omega_satellites.h"
00042
00043
00060
00064
00080
00081
00082 cpl_mask * detsat(cpl_image *nscience, cpl_parameterlist *hpars)
00083 {
00084
00085 int count = 0;
00086 int i = 0;
00087 int j = 0;
00088 int npars = 0;
00089 int nx = 0;
00090 int ny = 0;
00091
00092 double dthre = 5.0;
00093 double hthre = 1000.0;
00094 double stdev = 1.0;
00095
00096 float threshold = 0.0f;
00097
00098 const char *_id = "detsat";
00099 const char *alias = NULL;
00100
00101
00102 cpl_parameter *par;
00103 cpl_image *hough;
00104
00105
00106
00107 cpl_mask *hough_map;
00108
00109 cpl_stats *stats;
00110
00111
00112 if(!hpars) {
00113 cpl_msg_error ("", "%s Parameters list not found", _id);
00114 return NULL;
00115 }
00116
00117
00118
00119 npars = cpl_parameterlist_get_size(hpars);
00120 par = cpl_parameterlist_get_first(hpars);
00121
00122 for(i=0; i<npars; i++) {
00123 alias = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
00124 if(strcmp("det-sate", alias) == 0) {
00125 dthre = cpl_parameter_get_double(par) ;
00126 j++;
00127 }
00128 else if(strcmp("hough-thre", alias) == 0) {
00129 hthre = cpl_parameter_get_double(par) ;
00130 j++;
00131 }
00132 par = cpl_parameterlist_get_next(hpars);
00133 if(j == 2) break;
00134 }
00135
00136
00137 stats = cpl_stats_new_from_image (nscience, CPL_STATS_ALL);
00138 if(stats == NULL) {
00139 cpl_msg_warning("","%s Cannot calculate image statistics",_id);
00140 stdev = 1.0;
00141 cpl_error_reset();
00142 }
00143 else{
00144 stdev = cpl_stats_get_median_dev(stats);
00145 if(stdev == 0.0) stdev = 1.0;
00146 cpl_stats_delete(stats);
00147 }
00148
00149 threshold = (float)(dthre * stdev);
00150
00151
00152 hough = hough_transform(nscience, threshold);
00153 if(hough == NULL) {
00154 cpl_msg_warning("","%s Error in Hough Transform", _id);
00155 cpl_image_delete(hough);
00156 return NULL;
00157 }
00158
00159 threshold = (float)hthre;
00160
00161 nx = cpl_image_get_size_x(nscience);
00162 ny = cpl_image_get_size_y(nscience);
00163
00164 hough_map = hough_inverse_transform(hough, threshold, nx, ny);
00165 if(hough_map == NULL) {
00166 cpl_msg_warning("","%s Error in Inverse Hough Transform", _id);
00167 cpl_image_delete(hough);
00168 cpl_mask_delete(hough_map);
00169 return NULL;
00170 }
00171
00172
00173 count = cpl_mask_count(hough_map);
00174 if(count == -1) {
00175 cpl_msg_info("","No satellite tracks were detected in image");
00176 }
00177 else {
00178 cpl_msg_info("","Detected %d satellite tracks in image", count);
00179 }
00180
00181
00182
00183
00184 cpl_image_delete(hough);
00185
00186 return hough_map;
00187
00188 }
00189
00190
00191
00192
00193
00202
00203 cpl_image *hough_transform (cpl_image *img, float thre)
00204 {
00205
00206 int i = 0;
00207 int j = 0;
00208 int l = 0;
00209 int k = 0;
00210 int nx = 0;
00211 int ny = 0;
00212 int nrho = 0;
00213 int ntheta = 0;
00214
00215 double drho = 0.0;
00216 double theta = 0.0;
00217 double dtheta = 0.0;
00218 double x = 0.0;
00219 double y = 0.0;
00220
00221 float *sins;
00222 float *coss;
00223 float *hough_data;
00224 float *img_data;
00225
00226 const char *_id = "hough_transform:";
00227
00228 cpl_image *hough_img;
00229
00230
00231
00232
00233 if (img == NULL) {
00234 cpl_msg_error("","<%s> No input image",_id);
00235 return NULL;
00236 }
00237
00238
00239 img_data = (float *)cpl_image_get_data(img);
00240
00241 drho = 1.0/sqrt(2.0);
00242 nx = cpl_image_get_size_x (img);
00243 ny = cpl_image_get_size_y (img);
00244
00245 nrho = (int)(sqrt(nx*nx + ny*ny)/drho) + 1;
00246 ntheta = 1800;
00247 dtheta = PI/(double)ntheta;
00248
00249 hough_img = cpl_image_new(nrho,ntheta,CPL_TYPE_FLOAT);
00250 if(hough_img == NULL) {
00251 cpl_msg_error("","<%s>Cannot create Hough image",_id);
00252 return NULL;
00253 }
00254
00255 hough_data = (float *)cpl_image_get_data(hough_img);
00256
00257
00258 for(i=0; i<nrho*ntheta; i++) {
00259 hough_data[i] = (float)0.0;
00260 }
00261
00262 sins = calloc(1, ntheta*sizeof(float));
00263 if(sins == NULL) {
00264 cpl_msg_error("","%s Error in allocating memory", _id);
00265 cpl_image_delete(hough_img);
00266 return NULL;
00267 }
00268
00269 coss = calloc(1, ntheta*sizeof(float));
00270 if(coss == NULL) {
00271 cpl_msg_error("","%s Error in allocating memory", _id);
00272 cpl_image_delete(hough_img);
00273 free(sins);
00274 return NULL;
00275 }
00276
00277 for(i=0; i<ntheta; i++){
00278 theta = ((double)i + 0.5) * dtheta;
00279 sins[i] = sin(theta);
00280 coss[i] = cos(theta);
00281 }
00282
00283 for(j=0; j<ny; j++){
00284 y = (double)j;
00285 for(i=0; i<nx; i++){
00286 if(img_data[j*nx+i] > thre){
00287 x = (double)i;
00288 for(k=0; k<ntheta; k++){
00289 l = (int)floor((x*coss[k] + y*sins[k]) / drho);
00290 hough_data[k*nrho+l] += (float)1.0;
00291 }
00292 }
00293 }
00294 }
00295
00296 free(sins);
00297 free(coss);
00298
00299 return hough_img;
00300 }
00301
00302
00303 cpl_mask *hough_inverse_transform(cpl_image *himg, float thre, int lx, int ly)
00304 {
00305
00306 int i = 0;
00307 int j = 0;
00308 int k = 0;
00309 int l = 0;
00310 int hnx = 0;
00311 int hny = 0;
00312 int nrho = 0;
00313 int ntheta = 0;
00314
00315 double rho = 0.0;
00316 double drho = 0.0;
00317 double theta = 0.0;
00318 double dtheta = 0.0;
00319 double edge = 0.0;
00320 double a = 0.0;
00321 double b = 0.0;
00322
00323 float *hdata;
00324
00325 const char *_id = "hough_inverse_transform:";
00326
00327 cpl_mask *pmap;
00328 cpl_binary *pdata;
00329
00330
00331 if(himg == NULL) {
00332 cpl_msg_error("","<%s> No input image",_id);
00333 return NULL;
00334 }
00335
00336 drho = 1.0/sqrt(2.0);
00337 nrho = (int)(sqrt(lx*lx + ly*ly)/drho) + 1;
00338 ntheta = 1800;
00339 dtheta = PI / (double)ntheta;
00340
00341 hnx = cpl_image_get_size_x(himg);
00342 hny = cpl_image_get_size_y(himg);
00343
00344
00345 if( !((nrho == hnx) && (ntheta == hny)) ) {
00346 cpl_msg_error("","%s Dimensions of Hough image are incompatible",_id);
00347 return NULL;
00348 }
00349
00350 pmap = cpl_mask_new(lx, ly);
00351 if(pmap == NULL) {
00352 cpl_msg_error("","<%s> Error creating pixelmap",_id);
00353 return NULL;
00354 }
00355
00356 pdata = cpl_mask_get_data(pmap);
00357
00358 edge = sqrt(2.0)/2.0;
00359
00360 hdata = (float *)cpl_image_get_data(himg);
00361
00362 for(j=0; j<ntheta; j++) {
00363 theta = ((double)j + 0.5) * dtheta;
00364 for(i=0; i<nrho; i++) {
00365 rho = (double)i * drho;
00366 if( hdata[j*nrho + i] > thre ) {
00367
00368 if( fabs(sin(theta) > edge) ) {
00369
00370 a = -cos(theta) / sin(theta);
00371 b = rho/sin(theta);
00372 for(l=0; l<lx; l++){
00373 k = floor( (double)l * a + b);
00374 if( (k>0) && (k<ly) ){
00375 pdata[k*lx+l] = CPL_BINARY_1;
00376 }
00377 }
00378 }
00379 else {
00380
00381 a = -sin(theta) / cos(theta);
00382 b = rho/cos(theta);
00383 for(k=0; k<ly; k++){
00384 l = floor( (double)k * a + b);
00385 if( (l>=0) && (l<lx) ){
00386 pdata[k*lx+l] = CPL_BINARY_1;
00387 }
00388 }
00389 }
00390 }
00391 }
00392 }
00393
00394 return pmap;
00395 }
00396
00397