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 threshold = cpl_stats_get_median(stats) + (float)(dthre * stdev);
00149
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 threshold = (float)hthre;
00159
00160 nx = cpl_image_get_size_x(nscience);
00161 ny = cpl_image_get_size_y(nscience);
00162
00163 hough_map = hough_inverse_transform(hough, threshold, nx, ny);
00164 if(hough_map == NULL) {
00165 cpl_msg_warning("","%s Error in Inverse Hough Transform", _id);
00166 cpl_image_delete(hough);
00167 cpl_mask_delete(hough_map);
00168 return NULL;
00169 }
00170
00171
00172 count = cpl_mask_count(hough_map);
00173 if(count == -1) {
00174 cpl_msg_info("","No satellite tracks were detected in image");
00175 }
00176 else {
00177 cpl_msg_info("","Detected %d satellite tracks in image", count);
00178 }
00179
00180
00181
00182
00183 cpl_image_delete(hough);
00184
00185 return hough_map;
00186
00187 }
00188
00189
00190
00191
00192
00201
00202 cpl_image *hough_transform (cpl_image *img, float thre)
00203 {
00204
00205 int i = 0;
00206 int j = 0;
00207 int l = 0;
00208 int k = 0;
00209 int nx = 0;
00210 int ny = 0;
00211 int nrho = 0;
00212 int ntheta = 0;
00213
00214 double drho = 0.0;
00215 double theta = 0.0;
00216 double dtheta = 0.0;
00217 double x = 0.0;
00218 double y = 0.0;
00219
00220 float *sins;
00221 float *coss;
00222 float *hough_data;
00223 float *img_data;
00224
00225 const char *_id = "hough_transform:";
00226
00227 cpl_image *hough_img;
00228
00229
00230
00231
00232 if (img == NULL) {
00233 cpl_msg_error("","<%s> No input image",_id);
00234 return NULL;
00235 }
00236
00237
00238 img_data = (float *)cpl_image_get_data(img);
00239
00240 drho = 1.0/sqrt(2.0);
00241 nx = cpl_image_get_size_x (img);
00242 ny = cpl_image_get_size_y (img);
00243
00244 nrho = (int)(sqrt(nx*nx + ny*ny)/drho) + 1;
00245 ntheta = 3600;
00246 dtheta = 2*PI/(double)ntheta;
00247
00248 hough_img = cpl_image_new(nrho,ntheta,CPL_TYPE_FLOAT);
00249 if(hough_img == NULL) {
00250 cpl_msg_error("","<%s>Cannot create Hough image",_id);
00251 return NULL;
00252 }
00253
00254 hough_data = (float *)cpl_image_get_data(hough_img);
00255
00256
00257 for(i=0; i<nrho*ntheta; i++) {
00258 hough_data[i] = (float)0.0;
00259 }
00260
00261 sins = calloc(1, ntheta*sizeof(float));
00262 if(sins == NULL) {
00263 cpl_msg_error("","%s Error in allocating memory", _id);
00264 cpl_image_delete(hough_img);
00265 return NULL;
00266 }
00267
00268 coss = calloc(1, ntheta*sizeof(float));
00269 if(coss == NULL) {
00270 cpl_msg_error("","%s Error in allocating memory", _id);
00271 cpl_image_delete(hough_img);
00272 free(sins);
00273 return NULL;
00274 }
00275
00276 for(i=0; i<ntheta; i++){
00277 theta = ((double)i + 0.5) * dtheta;
00278 sins[i] = sin(theta);
00279 coss[i] = cos(theta);
00280 }
00281
00282 for(j=0; j<ny; j++){
00283 y = (double)j;
00284 for(i=0; i<nx; i++){
00285 if(img_data[j*nx+i] > thre){
00286 x = (double)i;
00287 for(k=0; k<ntheta; k++){
00288 l = (int)floor((x*coss[k] + y*sins[k]) / drho);
00289 hough_data[k*nrho+l] += (float)1.0;
00290 }
00291 }
00292 }
00293 }
00294
00295 free(sins);
00296 free(coss);
00297
00298 return hough_img;
00299 }
00300
00301
00302 cpl_mask *hough_inverse_transform(cpl_image *himg, float thre, int lx, int ly)
00303 {
00304
00305 int i = 0;
00306 int j = 0;
00307 int k = 0;
00308 int l = 0;
00309 int hnx = 0;
00310 int hny = 0;
00311 int nrho = 0;
00312 int ntheta = 0;
00313
00314 double rho = 0.0;
00315 double drho = 0.0;
00316 double theta = 0.0;
00317 double dtheta = 0.0;
00318 double edge = 0.0;
00319 double a = 0.0;
00320 double b = 0.0;
00321
00322 float *hdata;
00323
00324 const char *_id = "hough_inverse_transform:";
00325
00326 cpl_mask *pmap;
00327 cpl_binary *pdata;
00328
00329
00330 if(himg == NULL) {
00331 cpl_msg_error("","<%s> No input image",_id);
00332 return NULL;
00333 }
00334
00335 drho = 1.0/sqrt(2.0);
00336 nrho = (int)(sqrt(lx*lx + ly*ly)/drho) + 1;
00337 ntheta = 3600;
00338 dtheta = 2*PI / (double)ntheta;
00339
00340 hnx = cpl_image_get_size_x(himg);
00341 hny = cpl_image_get_size_y(himg);
00342
00343
00344 if( !((nrho == hnx) && (ntheta == hny)) ) {
00345 cpl_msg_error("","%s Dimensions of Hough image are incompatible",_id);
00346 return NULL;
00347 }
00348
00349 pmap = cpl_mask_new(lx, ly);
00350 if(pmap == NULL) {
00351 cpl_msg_error("","<%s> Error creating pixelmap",_id);
00352 return NULL;
00353 }
00354
00355 pdata = cpl_mask_get_data(pmap);
00356
00357 edge = sqrt(2.0)/2.0;
00358
00359 hdata = (float *)cpl_image_get_data(himg);
00360
00361 for(j=0; j<ntheta; j++) {
00362 theta = ((double)j + 0.5) * dtheta;
00363 for(i=0; i<nrho; i++) {
00364 rho = (double)i * drho;
00365 if( hdata[j*nrho + i] > thre ) {
00366
00367 if( fabs(sin(theta) > edge) ) {
00368
00369 a = -cos(theta) / sin(theta);
00370 b = rho/sin(theta);
00371 for(l=0; l<lx; l++){
00372 k = floor( (double)l * a + b);
00373 if( (k>0) && (k<ly) ){
00374 pdata[k*lx+l] = CPL_BINARY_1;
00375 }
00376 }
00377 }
00378 else {
00379
00380 a = -sin(theta) / cos(theta);
00381 b = rho/cos(theta);
00382 for(k=0; k<ly; k++){
00383 l = floor( (double)k * a + b);
00384 if( (l>=0) && (l<lx) ){
00385 pdata[k*lx+l] = CPL_BINARY_1;
00386 }
00387 }
00388 }
00389 }
00390 }
00391 }
00392
00393 return pmap;
00394 }
00395
00396