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
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 #ifdef HAVE_CONFIG_H
00087 # include <config.h>
00088 #endif
00089 #include "sinfo_vltPort.h"
00090 #define POSIX_SOURCE 1
00091
00092
00093
00094 #include "sinfo_function_1d.h"
00095 #include "sinfo_shift_images.h"
00096 #include "sinfo_new_resampling.h"
00097 #include "sinfo_globals.h"
00098
00099
00100 static int filecounter ;
00108
00109
00110
00111
00123 double sinfo_new_determine_shift_by_correlation ( cpl_image * refImage,
00124 cpl_image * shiftedImage )
00125 {
00126 int col, row ;
00127 int i, j, k, width;
00128 unsigned long convsize ;
00129 float * lineref ;
00130 float * line ;
00131 float * offset2 ;
00132 double * result ;
00133 double mean_offset2 ;
00134
00135 int maxlag ;
00136 float * refres ;
00137 float * myres ;
00138 int halfsearch ;
00139 int delta ;
00140 double xcorr_max ;
00141
00142 float par[MAXPAR] ;
00143 float derv_par[MAXPAR] ;
00144 int iters, xdim, ndat ;
00145 int numpar, its ;
00146 int * mpar ;
00147 float tol, lab ;
00148 float * xdat, * wdat ;
00149 Vector * peak;
00150 char filename[FILE_NAME_SZ] ;
00151 FILE * fp ;
00152
00153 int rlx=0;
00154 int rly=0;
00155 int slx=0;
00156 int sly=0;
00157 float* prdata=NULL;
00158 float* psdata=NULL;
00159
00160 if ( NULL == refImage || NULL == shiftedImage )
00161 {
00162 sinfo_msg_error("image not given!") ;
00163 return ZERO ;
00164 }
00165 rlx=cpl_image_get_size_x(refImage);
00166 rly=cpl_image_get_size_x(refImage);
00167 prdata=cpl_image_get_data_float(refImage);
00168 slx=cpl_image_get_size_x(shiftedImage);
00169 sly=cpl_image_get_size_x(shiftedImage);
00170 psdata=cpl_image_get_data_float(shiftedImage);
00171
00172 if ( rlx != slx || rly != sly )
00173 {
00174 sinfo_msg_error("image size not compatible!") ;
00175 return ZERO ;
00176 }
00177 snprintf(filename, MAX_NAME_SIZE-1,"offset%d.list", filecounter) ;
00178
00179 fp = fopen(filename, "w") ;
00180
00181 convsize = sly;
00182
00183 lineref = (float*) cpl_calloc(convsize, sizeof(float) ) ;
00184 line = (float*) cpl_calloc(convsize, sizeof(float) ) ;
00185
00186 offset2 = (float*) cpl_calloc(slx, sizeof(float) ) ;
00187
00188 for ( col = 0 ; col < slx ; col++ )
00189 {
00190
00191 for ( row = 0 ; row < (int) convsize ; row++ )
00192 {
00193 lineref[row] = 0. ;
00194 line[row] = 0. ;
00195 }
00196
00197
00198 for ( row = 0 ; row < (sly) ; row++ )
00199 {
00200 lineref[row] = prdata[col+row*slx] ;
00201 line[row] = psdata[col+row*slx] ;
00202 }
00203
00204 myres = sinfo_function1d_filter_lowpass(line, convsize,
00205 LOW_PASS_GAUSSIAN, 3) ;
00206 refres = sinfo_function1d_filter_lowpass(lineref, convsize,
00207 LOW_PASS_GAUSSIAN, 4) ;
00208
00209
00210 halfsearch = convsize / 2 ;
00211 result = sinfo_new_xcorrel( myres, convsize, refres, convsize,
00212 halfsearch, &delta, &maxlag, &xcorr_max ) ;
00213
00214 if ( xcorr_max < 0. )
00215 {
00216 sinfo_function1d_del ( refres ) ;
00217 sinfo_function1d_del ( myres ) ;
00218 cpl_free (result) ;
00219 continue ;
00220 }
00221
00222
00223
00224
00225
00226 i = maxlag; j = i+1;
00227 while (result[j] < result[i])
00228 {
00229 i++; j++;
00230 }
00231 i = maxlag; k = i-1;
00232 while (result[k] < result[i])
00233 {
00234 i--; k--;
00235 }
00236 width = j-k+1;
00237
00238 if ( NULL == (peak = sinfo_new_vector (width)) )
00239 {
00240 sinfo_msg_error ("cannot allocate new Vector ") ;
00241 return ZERO ;
00242 }
00243
00244
00245
00246 xdat = (float *) cpl_calloc( peak -> n_elements, sizeof (float) ) ;
00247 wdat = (float *) cpl_calloc( peak -> n_elements, sizeof (float) ) ;
00248 mpar = (int *) cpl_calloc( MAXPAR, sizeof (int) ) ;
00249
00250
00251
00252
00253 for ( i = 0 ; i < width ; i++ )
00254 {
00255 peak -> data[i] = result[k+i] ;
00256 xdat[i] = i;
00257 wdat[i] = 1.0;
00258 }
00259
00260
00261 xdim = XDIM;
00262 ndat = peak -> n_elements ;
00263 numpar = MAXPAR ;
00264 tol = TOL ;
00265 lab = LAB ;
00266 its = ITS ;
00267 par[1] = width/2.0 ;
00268 par[2] = (float) (maxlag - k) ;
00269 par[3] = (peak -> data[0] + peak -> data[peak->n_elements - 1]) / 2.0 ;
00270 par[0] = result[maxlag] - (par[3]) ;
00271
00272 for ( i = 0 ; i < MAXPAR ; i++ )
00273 {
00274 derv_par[i] = 0.0 ;
00275 mpar[i] = 1 ;
00276 }
00277
00278
00279 if ( 0 > ( iters = sinfo_new_lsqfit_c( xdat, &xdim, peak -> data, wdat,
00280 &ndat, par, derv_par, mpar,
00281 &numpar, &tol, &its, &lab )) )
00282 {
00283 sinfo_msg_warning ("sinfo_new_lsqfit_c: least squares fit failed "
00284 "in col: %d, error no.: %d", col, iters) ;
00285 sinfo_new_destroy_vector ( peak ) ;
00286 cpl_free ( xdat ) ;
00287 cpl_free ( wdat ) ;
00288 cpl_free ( mpar ) ;
00289 sinfo_function1d_del ( refres ) ;
00290 sinfo_function1d_del ( myres ) ;
00291 cpl_free (result) ;
00292 continue ;
00293 }
00294
00295 sinfo_new_destroy_vector ( peak ) ;
00296 cpl_free (xdat) ;
00297 cpl_free (wdat) ;
00298 cpl_free (mpar) ;
00299 sinfo_function1d_del ( refres ) ;
00300 sinfo_function1d_del ( myres ) ;
00301 cpl_free (result) ;
00302
00303 offset2[col] = (float)( k+par[2] - sly/2) ;
00304 fprintf(fp, "offset: %f in col: %d\n", offset2[col], col) ;
00305 }
00306
00307 mean_offset2 = (double)sinfo_new_clean_mean (offset2, slx, 15., 15. ) ;
00308 fprintf(fp, "mean offset: %f\n", mean_offset2) ;
00309 fclose(fp) ;
00310
00311 cpl_free ( lineref ) ;
00312 cpl_free ( line ) ;
00313 cpl_free ( offset2 ) ;
00314 filecounter++ ;
00315 if (filecounter > 100 ) filecounter = 0 ;
00316
00317 return mean_offset2 ;
00318 }
00319
00320
00331 cpl_image *
00332 sinfo_new_shift_image_in_spec ( cpl_image * shiftedImage,
00333 double shift,
00334 double * sub_shift )
00335 {
00336 cpl_image * retIm ;
00337 int col, row ;
00338 int intshift ;
00339 int ilx=0;
00340 int ily=0;
00341 int olx=0;
00342 int oly=0;
00343
00344 float* pidata=NULL;
00345 float* podata=NULL;
00346
00347 if ( shiftedImage == NULL )
00348 {
00349 sinfo_msg_error("no image given!") ;
00350 return NULL ;
00351 }
00352
00353 ilx=cpl_image_get_size_x(shiftedImage);
00354 ily=cpl_image_get_size_y(shiftedImage);
00355 pidata=cpl_image_get_data_float(shiftedImage);
00356
00357 intshift = sinfo_new_nint (shift) ;
00358 *sub_shift = shift - (double) intshift ;
00359 if ( intshift == 0 )
00360 {
00361 retIm =cpl_image_duplicate ( shiftedImage ) ;
00362 return retIm ;
00363 }
00364 else
00365 {
00366 if ( NULL == (retIm = cpl_image_new(ilx,ily,CPL_TYPE_FLOAT)) )
00367 {
00368 sinfo_msg_error ("could not allocate memory!") ;
00369 return NULL ;
00370 }
00371 }
00372
00373 olx=cpl_image_get_size_x(retIm);
00374 oly=cpl_image_get_size_y(retIm);
00375 podata=cpl_image_get_data_float(retIm);
00376
00377 for ( col = 0 ; col < ilx ; col++ )
00378 {
00379 for ( row = 0 ; row < ily ; row++ )
00380 {
00381 if ( (row-intshift >= 0 ) && (row - intshift < oly) )
00382 {
00383 podata[col+(row-intshift)*olx] = pidata[col+row*olx] ;
00384 }
00385 }
00386 }
00387 return retIm ;
00388 }
00389
00399 cpl_image *
00400 sinfo_new_fine_shift_image_in_spec_poly ( cpl_image * shiftedImage,
00401 double sub_shift,
00402 int order )
00403 {
00404 cpl_image * retIm ;
00405
00406 float* spec=NULL ;
00407 float* corrected_spec=NULL ;
00408 float* xnum=NULL ;
00409
00410 float sum, new_sum ;
00411 float eval ;
00412 float * imageptr ;
00413 int row, col ;
00414 int firstpos ;
00415 int n_points ;
00416 int i ;
00417 int flag;
00418 int ilx=0;
00419 int ily=0;
00420 int olx=0;
00421 int oly=0;
00422
00423 float* pidata=NULL;
00424 float* podata=NULL;
00425
00426 if ( shiftedImage == NULL )
00427 {
00428 sinfo_msg_error("no image given!") ;
00429 return NULL ;
00430 }
00431 ilx=cpl_image_get_size_x(shiftedImage);
00432 ily=cpl_image_get_size_y(shiftedImage);
00433 pidata=cpl_image_get_data_float(shiftedImage);
00434
00435 if ( order <= 0 )
00436 {
00437 sinfo_msg_error("wrong order of interpolation polynom given!") ;
00438 return NULL ;
00439 }
00440
00441
00442 if ( NULL == (retIm = cpl_image_new(ilx, ily,CPL_TYPE_FLOAT)) )
00443 {
00444 sinfo_msg_error ("could not allocate memory!") ;
00445 return NULL ;
00446 }
00447
00448 olx=cpl_image_get_size_x(retIm);
00449 oly=cpl_image_get_size_y(retIm);
00450 podata=cpl_image_get_data_float(retIm);
00451
00452 n_points = order + 1 ;
00453 if ( n_points % 2 == 0 )
00454 {
00455 firstpos = (int)(n_points/2) - 1 ;
00456 }
00457 else
00458 {
00459 firstpos = (int)(n_points/2) ;
00460 }
00461
00462 spec=cpl_calloc(ily,sizeof(float)) ;
00463 corrected_spec=cpl_calloc(ily,sizeof(float)) ;
00464 xnum=cpl_calloc(order+1,sizeof(float)) ;
00465
00466
00467
00468 for ( i = 0 ; i < n_points ; i++ )
00469 {
00470 xnum[i] = i ;
00471 }
00472
00473 for ( col = 0 ; col < ilx ; col++ )
00474 {
00475 for ( row = 0 ; row < ily ; row++ )
00476 {
00477 corrected_spec[row] = 0. ;
00478 }
00479 sum = 0. ;
00480 for ( row = 0 ; row < ily ; row++ )
00481 {
00482 spec[row] = pidata[col + row*ilx] ;
00483 if (isnan(spec[row]) )
00484 {
00485 spec[row] = 0. ;
00486
00487 for ( i = row - firstpos ; i < row-firstpos+n_points ; i++ )
00488 {
00489 if ( i < 0 ) continue ;
00490 if ( i >= ily) continue ;
00491 corrected_spec[i] = ZERO ;
00492 }
00493 }
00494 if ( row != 0 && row != ily - 1 )
00495 {
00496 sum += spec[row] ;
00497 }
00498 }
00499
00500 new_sum = 0. ;
00501 for ( row = 0 ; row < ily ; row++ )
00502 {
00503
00504
00505
00506
00507
00508
00509 if (isnan(corrected_spec[row])) continue ;
00510 if ( row - firstpos < 0 )
00511 {
00512 imageptr = &spec[0] ;
00513 eval = sub_shift + row ;
00514 }
00515 else if ( row - firstpos + n_points >= ily )
00516 {
00517 imageptr = &spec[ily - n_points] ;
00518 eval = sub_shift + row + n_points - ily ;
00519 }
00520 else
00521 {
00522 imageptr = &spec[row-firstpos] ;
00523 eval = sub_shift + firstpos ;
00524 }
00525
00526 flag=0;
00527 corrected_spec[row]=sinfo_new_nev_ille( xnum, imageptr,
00528 order, eval, &flag) ;
00529 if ( row != 0 && row != ily - 1 )
00530 {
00531 new_sum += corrected_spec[row] ;
00532 }
00533 }
00534
00535 for ( row = 0 ; row < ily ; row++ )
00536 {
00537 if ( new_sum == 0. )
00538 {
00539 new_sum = 1. ;
00540 }
00541 if ( row == 0 )
00542 {
00543 podata[col+row*olx] = ZERO ;
00544 }
00545 else if ( row == ily - 1 )
00546 {
00547 podata[col+row*olx] = ZERO ;
00548 }
00549 else if ( isnan(corrected_spec[row]) )
00550 {
00551 podata[col+row*olx] = ZERO ;
00552 }
00553 else
00554 {
00555 corrected_spec[row] *= sum / new_sum ;
00556 podata[col+row*olx] = corrected_spec[row] ;
00557 }
00558 }
00559 }
00560 cpl_free(spec) ;
00561 cpl_free(corrected_spec) ;
00562 cpl_free(xnum) ;
00563 return retIm ;
00564 }
00573 cpl_image *
00574 sinfo_new_fine_shift_image_in_spec_cubic_spline ( cpl_image * shiftedImage,
00575 double sub_shift )
00576 {
00577 cpl_image * retIm ;
00578
00579 float* spec=NULL ;
00580 float* corrected_spec=NULL ;
00581 float* xnum=NULL ;
00582 float* eval=NULL ;
00583 float sum, new_sum ;
00584 int row, col ;
00585 int i ;
00586 int ilx=0;
00587 int ily=0;
00588 int olx=0;
00589 int oly=0;
00590
00591 float* pidata=NULL;
00592 float* podata=NULL;
00593
00594 if ( shiftedImage == NULL )
00595 {
00596 sinfo_msg_error("no image given!") ;
00597 return NULL ;
00598 }
00599 ilx=cpl_image_get_size_x(shiftedImage);
00600 ily=cpl_image_get_size_y(shiftedImage);
00601 pidata=cpl_image_get_data_float(shiftedImage);
00602
00603
00604 if ( NULL == (retIm = cpl_image_new(ilx,ily,CPL_TYPE_FLOAT)) )
00605 {
00606 sinfo_msg_error ("could not allocate memory!") ;
00607 return NULL ;
00608 }
00609 olx=cpl_image_get_size_x(retIm);
00610 oly=cpl_image_get_size_y(retIm);
00611 podata=cpl_image_get_data_float(retIm);
00612
00613 xnum=cpl_calloc(ily,sizeof(float)) ;
00614
00615 for ( i = 0 ; i < ily ; i++ )
00616 {
00617 xnum[i] = i ;
00618 }
00619
00620 spec=cpl_calloc(ily,sizeof(float)) ;
00621 corrected_spec=cpl_calloc(ily,sizeof(float)) ;
00622 eval=cpl_calloc(ily,sizeof(float)) ;
00623
00624 for ( col = 0 ; col < ilx ; col++ )
00625 {
00626 sum = 0. ;
00627 for ( row = 0 ; row < ily ; row++ )
00628 {
00629 spec[row] = pidata[col + row*ilx] ;
00630 if (isnan(spec[row]) )
00631 {
00632 for ( i = row-1 ; i <= row+1 ; i++ )
00633 {
00634 if ( i < 0 ) continue ;
00635 if ( i >= ily) continue ;
00636 corrected_spec[i] = ZERO ;
00637 }
00638 spec[row] = 0. ;
00639 }
00640 sum += spec[row] ;
00641 eval[row] = (float)sub_shift+(float)row ;
00642 }
00643
00644 if ( -1 == sinfo_function1d_natural_spline( xnum, spec, ily, eval,
00645 corrected_spec, ily ) )
00646 {
00647 sinfo_msg_error("error in spline interpolation!") ;
00648 return NULL ;
00649 }
00650
00651 new_sum = 0. ;
00652 for ( row = 0 ; row < ily ; row++ )
00653 {
00654 if ( isnan(corrected_spec[row]) )
00655 {
00656 continue ;
00657 }
00658 new_sum += corrected_spec[row] ;
00659 }
00660
00661 for ( row = 0 ; row < ily ; row++ )
00662 {
00663 if ( new_sum == 0. ) new_sum =1. ;
00664 {
00665 if ( isnan(corrected_spec[row]) )
00666 {
00667 podata[col+row*olx] = ZERO ;
00668 }
00669 else
00670 {
00671 corrected_spec[row] *= sum / new_sum ;
00672 podata[col+row*olx] = corrected_spec[row] ;
00673 }
00674 }
00675 }
00676 }
00677 cpl_free(xnum);
00678 cpl_free(spec) ;
00679 cpl_free(corrected_spec) ;
00680 cpl_free(eval) ;
00681
00682 return retIm ;
00683 }
00684
00696 cpl_imagelist * sinfo_align_cube_to_reference (cpl_imagelist * cube,
00697 cpl_image * refIm,
00698 int order,
00699 int shift_indicator )
00700 {
00701 cpl_imagelist * retCube=NULL ;
00702 cpl_image * shiftedIm=NULL ;
00703 cpl_image * fineShiftedIm=NULL ;
00704 double shift=0 ;
00705 double sub_shift=0 ;
00706 int z=0;
00707 double * ker=NULL ;
00708 cpl_image* img=NULL;
00709
00710 if (cube == NULL)
00711 {
00712 sinfo_msg_error("no input cube given!") ;
00713 return NULL ;
00714 }
00715 if (refIm == NULL)
00716 {
00717 sinfo_msg_error("no input ref. image given!") ;
00718 return NULL ;
00719 }
00720
00721
00722 retCube = cpl_imagelist_new() ;
00723
00724 if ( shift_indicator != 0 && shift_indicator != 1 )
00725 {
00726 ker = sinfo_new_generate_interpolation_kernel("tanh") ;
00727 if (ker == NULL)
00728 {
00729 sinfo_msg_error("kernel generation failure: aborting resampling") ;
00730 return NULL ;
00731 }
00732 }
00733
00734 for ( z = 0 ; z < cpl_imagelist_get_size(cube) ; z++ )
00735 {
00736
00737 img=cpl_imagelist_get(cube,z);
00738 if (isnan( shift=sinfo_new_determine_shift_by_correlation(refIm,img)))
00739 {
00740 sinfo_msg_error("error in sinfo_determineShiftByCorrelation()!") ;
00741 return NULL ;
00742 }
00743
00744 if ( NULL == (shiftedIm = sinfo_new_shift_image_in_spec(img,shift,
00745 &sub_shift)) )
00746 {
00747 sinfo_msg_error("error in sinfo_shiftImageInSpec()!") ;
00748 return NULL ;
00749 }
00750 if ( shift_indicator == 0 )
00751 {
00752 if ( NULL == (fineShiftedIm =
00753 sinfo_new_fine_shift_image_in_spec_poly (shiftedIm,
00754 sub_shift, order)))
00755 {
00756 sinfo_msg_error("error in sinfo_fineShiftImageInSpecPoly()!") ;
00757 return NULL ;
00758 }
00759 }
00760 else if ( shift_indicator == 1)
00761 {
00762 if ( NULL == (fineShiftedIm =
00763 sinfo_new_fine_shift_image_in_spec_cubic_spline (shiftedIm,
00764 sub_shift)))
00765 {
00766 sinfo_msg_error("error in fineShiftImageInSpecCubicspline()!") ;
00767 return NULL ;
00768 }
00769 }
00770
00771 else
00772 {
00773 if ( NULL == (fineShiftedIm =
00774 sinfo_new_shift_image(shiftedIm,0.,sub_shift, ker ) ) )
00775 {
00776 sinfo_msg_error("error in fineShiftImageInSpecCubicspline()!") ;
00777 return NULL ;
00778 }
00779 }
00780 cpl_imagelist_set(retCube,fineShiftedIm,z);
00781 cpl_image_delete (shiftedIm) ;
00782 cpl_image_delete (fineShiftedIm) ;
00783 }
00784 if ( shift_indicator != 0 && shift_indicator != 1 ) cpl_free(ker) ;
00785 return retCube ;
00786 }
00787
00788