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
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <math.h>
00039 #include <errno.h>
00040 #include <complex.h>
00041
00042 #include "omega_bpm.h"
00043
00044 #include "omega_utils.h"
00058
00062
00063
00064
00065 cpl_image *omega_mirror_edges(cpl_image *image, int dx, int dy);
00066
00067
00083
00084 cpl_image * omega_gen_lowpass(int xs, int ys, double sigma_x, double sigma_y)
00085 {
00086
00087 int i= 0.0;
00088 int j= 0.0;
00089 int hlx= 0.0;
00090 int hly = 0.0;
00091 double x= 0.0;
00092 double y= 0.0;
00093 double gaussval= 0.0;
00094 float *data;
00095
00096 cpl_image *lowpass_image;
00097
00098
00099 lowpass_image = cpl_image_new (xs, ys, CPL_TYPE_FLOAT);
00100 if (lowpass_image == NULL) {
00101 cpl_msg_error (cpl_func, "Cannot generate lowpass filter <%s>",cpl_error_get_message());
00102 return NULL;
00103 }
00104
00105 hlx = xs/2;
00106 hly = ys/2;
00107
00108 data = cpl_image_get_data_float(lowpass_image);
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 data[0] = (float)1.0;
00121
00122
00123 for (i=1 ; i<=hlx ; i++) {
00124 x = (double)i / sigma_x;
00125 gaussval = (double)exp(-0.5*x*x);
00126 data[i] = gaussval;
00127 data[xs-i] = gaussval;
00128 }
00129
00130 for (j=1; j<=hly ; j++) {
00131 y = (double)j / sigma_y;
00132
00133 data[j*xs] = (double)exp(-0.5*y*y);
00134 data[(ys-j)*xs] = (double)exp(-0.5*y*y);
00135
00136 for (i=1 ; i<=hlx ; i++) {
00137
00138 x = (double) i / sigma_x;
00139 gaussval = (double)exp (-0.5*(x*x+y*y));
00140 data[j*xs+i] = gaussval;
00141 data[(j+1)*xs-i] = gaussval;
00142 data[(ys-j)*xs+i] = gaussval;
00143 data[(ys+1-j)*xs-i] = gaussval;
00144
00145 }
00146 }
00147
00148
00149
00150
00151
00152 if(errno != 0)
00153 errno = 0;
00154
00155 return lowpass_image;
00156 }
00157
00171 cpl_image * omega_get_spatial_freq(cpl_image *flat, cpl_mask *bpm,
00172 double gausfilt, int mirrorx, int mirrory){
00173
00174 int xsize=0, ysize=0;
00175
00176 double sigma_x = 0.;
00177 double sigma_y = 0.;
00178
00179 cpl_image *clean_flat=NULL;
00180 cpl_image *eflat=NULL;
00181 cpl_image *eflat_complex=NULL;
00182 cpl_image *eflat_real=NULL;
00183
00184 cpl_image *filter_image=NULL;
00185 cpl_image *filter_image_complex=NULL;
00186 cpl_image *flat_real;
00187
00188
00189
00190 clean_flat = cpl_image_duplicate(flat);
00191 if (bpm != NULL) {
00192 cpl_image_reject_from_mask(clean_flat,bpm);
00193 cpl_detector_interpolate_rejected(clean_flat);
00194 }
00195
00196
00197 eflat = omega_mirror_edges(clean_flat, mirrorx, mirrory);
00198 if(eflat == NULL){
00199 freeimage(clean_flat);
00200 cpl_msg_error(cpl_func,"Filter image is NULL");
00201 return NULL;
00202 }
00203
00204 freeimage(clean_flat);
00205
00206 xsize = cpl_image_get_size_x(eflat);
00207 ysize = cpl_image_get_size_y(eflat);
00208
00209 sigma_x = gausfilt;
00210 sigma_y = (double)(sigma_x * ysize) / xsize;
00211
00212
00213
00214 filter_image = omega_gen_lowpass(xsize, ysize, sigma_x, sigma_y);
00215 if(filter_image == NULL){
00216 cpl_msg_error(cpl_func,"Filter image is NULL");
00217 return NULL;
00218 }
00219
00220 eflat_complex = cpl_image_new(xsize,ysize, CPL_TYPE_FLOAT_COMPLEX);
00221 eflat_real = cpl_image_new(xsize,ysize, CPL_TYPE_FLOAT);
00222 filter_image_complex =cpl_image_cast(filter_image,CPL_TYPE_FLOAT_COMPLEX);
00223
00224
00225 cpl_image_delete(filter_image);
00226
00227
00228
00229 cpl_fft_image(eflat_complex, eflat, CPL_FFT_FORWARD);
00230
00231 cpl_image_delete(eflat);
00232
00233
00234 cpl_image_multiply(eflat_complex,filter_image_complex);
00235
00236
00237
00238 cpl_fft_image(eflat_real, eflat_complex,CPL_FFT_BACKWARD);
00239
00240 cpl_image_delete(eflat_complex);
00241 cpl_image_delete(filter_image_complex);
00242
00243
00244 flat_real = cpl_image_extract(eflat_real, mirrorx+1, mirrory+1,
00245 xsize-mirrorx, ysize-mirrory);
00246
00247 if (flat_real == NULL) {
00248 cpl_msg_error (cpl_func,"Real extracted image is NULL. <%s>", cpl_error_get_message());
00249 return NULL;
00250 }
00251 cpl_image_delete(eflat_real);
00252
00253
00254 return flat_real;
00255 }
00256
00257
00265
00266 cpl_image *omega_mirror_edges(cpl_image *image, int dx, int dy)
00267 {
00268
00269 int xx = 0;
00270 int yy = 0;
00271 int xs = 0;
00272 int ys = 0;
00273 int inrow = 1;
00274 int outrow = 1;
00275 int i =0;
00276 int j = 0;
00277
00278 float *data;
00279 float *out_data;
00280
00281
00282 cpl_image *big_image;
00283
00284
00285
00286 xs = cpl_image_get_size_x(image);
00287 ys = cpl_image_get_size_y(image);
00288
00289 xx = xs+(2*dx);
00290 yy = ys+(2*dy);
00291
00292 data = cpl_image_get_data_float(image);
00293
00294 big_image = cpl_image_new(xx, yy, CPL_TYPE_FLOAT);
00295 out_data = cpl_image_get_data_float(big_image);
00296
00297 for (j=0; j<ys ; j++){
00298 inrow = j*xs;
00299 outrow = (j+dy)*xx;
00300
00301 for (i=0; i<xs ; i++){
00302 out_data[outrow+dx+i] = data[inrow+i];
00303 }
00304
00305 for (i=0; i<dx; i++){
00306 out_data[outrow+i] = data[inrow+dx-i-1];
00307 out_data[outrow+xs+dx+i] = data[inrow+xs-i-1];
00308
00309 }
00310 }
00311
00312 for (j=0; j<dy ; j++) {
00313
00314 for (i=0; i<xx; i++) {
00315 out_data[j*xx+i] = out_data[(2*dy-j-1)*xx+i];
00316 out_data[(yy-j-1)*xx+i] = out_data[(yy-2*dy+j)*xx+i];
00317 }
00318 }
00319
00320 return big_image;
00321 }
00322
00323