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 fclose(fp);
00242 return ZERO ;
00243 }
00244
00245
00246
00247 xdat = (float *) cpl_calloc( peak -> n_elements, sizeof (float) ) ;
00248 wdat = (float *) cpl_calloc( peak -> n_elements, sizeof (float) ) ;
00249 mpar = (int *) cpl_calloc( MAXPAR, sizeof (int) ) ;
00250
00251
00252
00253
00254 for ( i = 0 ; i < width ; i++ )
00255 {
00256 peak -> data[i] = result[k+i] ;
00257 xdat[i] = i;
00258 wdat[i] = 1.0;
00259 }
00260
00261
00262 xdim = XDIM;
00263 ndat = peak -> n_elements ;
00264 numpar = MAXPAR ;
00265 tol = TOL ;
00266 lab = LAB ;
00267 its = ITS ;
00268 par[1] = width/2.0 ;
00269 par[2] = (float) (maxlag - k) ;
00270 par[3] = (peak -> data[0] + peak -> data[peak->n_elements - 1]) / 2.0 ;
00271 par[0] = result[maxlag] - (par[3]) ;
00272
00273 for ( i = 0 ; i < MAXPAR ; i++ )
00274 {
00275 derv_par[i] = 0.0 ;
00276 mpar[i] = 1 ;
00277 }
00278
00279
00280 if ( 0 > ( iters = sinfo_new_lsqfit_c( xdat, &xdim, peak -> data, wdat,
00281 &ndat, par, derv_par, mpar,
00282 &numpar, &tol, &its, &lab )) )
00283 {
00284 sinfo_msg_warning ("sinfo_new_lsqfit_c: least squares fit failed "
00285 "in col: %d, error no.: %d", col, iters) ;
00286 sinfo_new_destroy_vector ( peak ) ;
00287 cpl_free ( xdat ) ;
00288 cpl_free ( wdat ) ;
00289 cpl_free ( mpar ) ;
00290 sinfo_function1d_del ( refres ) ;
00291 sinfo_function1d_del ( myres ) ;
00292 cpl_free (result) ;
00293 continue ;
00294 }
00295
00296 sinfo_new_destroy_vector ( peak ) ;
00297 cpl_free (xdat) ;
00298 cpl_free (wdat) ;
00299 cpl_free (mpar) ;
00300 sinfo_function1d_del ( refres ) ;
00301 sinfo_function1d_del ( myres ) ;
00302 cpl_free (result) ;
00303
00304 offset2[col] = (float)( k+par[2] - sly/2) ;
00305 fprintf(fp, "offset: %f in col: %d\n", offset2[col], col) ;
00306 }
00307
00308 mean_offset2 = (double)sinfo_new_clean_mean (offset2, slx, 15., 15. ) ;
00309 fprintf(fp, "mean offset: %f\n", mean_offset2) ;
00310 fclose(fp) ;
00311
00312 cpl_free ( lineref ) ;
00313 cpl_free ( line ) ;
00314 cpl_free ( offset2 ) ;
00315 filecounter++ ;
00316 if (filecounter > 100 ) filecounter = 0 ;
00317
00318 return mean_offset2 ;
00319 }
00320
00321
00332 cpl_image *
00333 sinfo_new_shift_image_in_spec ( cpl_image * shiftedImage,
00334 double shift,
00335 double * sub_shift )
00336 {
00337 cpl_image * retIm ;
00338 int col, row ;
00339 int intshift ;
00340 int ilx=0;
00341 int ily=0;
00342 int olx=0;
00343 int oly=0;
00344
00345 float* pidata=NULL;
00346 float* podata=NULL;
00347
00348 if ( shiftedImage == NULL )
00349 {
00350 sinfo_msg_error("no image given!") ;
00351 return NULL ;
00352 }
00353
00354 ilx=cpl_image_get_size_x(shiftedImage);
00355 ily=cpl_image_get_size_y(shiftedImage);
00356 pidata=cpl_image_get_data_float(shiftedImage);
00357
00358 intshift = sinfo_new_nint (shift) ;
00359 *sub_shift = shift - (double) intshift ;
00360 if ( intshift == 0 )
00361 {
00362 retIm =cpl_image_duplicate ( shiftedImage ) ;
00363 return retIm ;
00364 }
00365 else
00366 {
00367 if ( NULL == (retIm = cpl_image_new(ilx,ily,CPL_TYPE_FLOAT)) )
00368 {
00369 sinfo_msg_error ("could not allocate memory!") ;
00370 return NULL ;
00371 }
00372 }
00373
00374 olx=cpl_image_get_size_x(retIm);
00375 oly=cpl_image_get_size_y(retIm);
00376 podata=cpl_image_get_data_float(retIm);
00377
00378 for ( col = 0 ; col < ilx ; col++ )
00379 {
00380 for ( row = 0 ; row < ily ; row++ )
00381 {
00382 if ( (row-intshift >= 0 ) && (row - intshift < oly) )
00383 {
00384 podata[col+(row-intshift)*olx] = pidata[col+row*olx] ;
00385 }
00386 }
00387 }
00388 return retIm ;
00389 }
00390
00400 cpl_image *
00401 sinfo_new_fine_shift_image_in_spec_poly ( cpl_image * shiftedImage,
00402 double sub_shift,
00403 int order )
00404 {
00405 cpl_image * retIm ;
00406
00407 float* spec=NULL ;
00408 float* corrected_spec=NULL ;
00409 float* xnum=NULL ;
00410
00411 float sum, new_sum ;
00412 float eval ;
00413 float * imageptr ;
00414 int row, col ;
00415 int firstpos ;
00416 int n_points ;
00417 int i ;
00418 int flag;
00419 int ilx=0;
00420 int ily=0;
00421 int olx=0;
00422 int oly=0;
00423
00424 float* pidata=NULL;
00425 float* podata=NULL;
00426
00427 if ( shiftedImage == NULL )
00428 {
00429 sinfo_msg_error("no image given!") ;
00430 return NULL ;
00431 }
00432 ilx=cpl_image_get_size_x(shiftedImage);
00433 ily=cpl_image_get_size_y(shiftedImage);
00434 pidata=cpl_image_get_data_float(shiftedImage);
00435
00436 if ( order <= 0 )
00437 {
00438 sinfo_msg_error("wrong order of interpolation polynom given!") ;
00439 return NULL ;
00440 }
00441
00442
00443 if ( NULL == (retIm = cpl_image_new(ilx, ily,CPL_TYPE_FLOAT)) )
00444 {
00445 sinfo_msg_error ("could not allocate memory!") ;
00446 return NULL ;
00447 }
00448
00449 olx=cpl_image_get_size_x(retIm);
00450 oly=cpl_image_get_size_y(retIm);
00451 podata=cpl_image_get_data_float(retIm);
00452
00453 n_points = order + 1 ;
00454 if ( n_points % 2 == 0 )
00455 {
00456 firstpos = (int)(n_points/2) - 1 ;
00457 }
00458 else
00459 {
00460 firstpos = (int)(n_points/2) ;
00461 }
00462
00463 spec=cpl_calloc(ily,sizeof(float)) ;
00464 corrected_spec=cpl_calloc(ily,sizeof(float)) ;
00465 xnum=cpl_calloc(order+1,sizeof(float)) ;
00466
00467
00468
00469 for ( i = 0 ; i < n_points ; i++ )
00470 {
00471 xnum[i] = i ;
00472 }
00473
00474 for ( col = 0 ; col < ilx ; col++ )
00475 {
00476 for ( row = 0 ; row < ily ; row++ )
00477 {
00478 corrected_spec[row] = 0. ;
00479 }
00480 sum = 0. ;
00481 for ( row = 0 ; row < ily ; row++ )
00482 {
00483 spec[row] = pidata[col + row*ilx] ;
00484 if (isnan(spec[row]) )
00485 {
00486 spec[row] = 0. ;
00487
00488 for ( i = row - firstpos ; i < row-firstpos+n_points ; i++ )
00489 {
00490 if ( i < 0 ) continue ;
00491 if ( i >= ily) continue ;
00492 corrected_spec[i] = ZERO ;
00493 }
00494 }
00495 if ( row != 0 && row != ily - 1 )
00496 {
00497 sum += spec[row] ;
00498 }
00499 }
00500
00501 new_sum = 0. ;
00502 for ( row = 0 ; row < ily ; row++ )
00503 {
00504
00505
00506
00507
00508
00509
00510 if (isnan(corrected_spec[row])) continue ;
00511 if ( row - firstpos < 0 )
00512 {
00513 imageptr = &spec[0] ;
00514 eval = sub_shift + row ;
00515 }
00516 else if ( row - firstpos + n_points >= ily )
00517 {
00518 imageptr = &spec[ily - n_points] ;
00519 eval = sub_shift + row + n_points - ily ;
00520 }
00521 else
00522 {
00523 imageptr = &spec[row-firstpos] ;
00524 eval = sub_shift + firstpos ;
00525 }
00526
00527 flag=0;
00528 corrected_spec[row]=sinfo_new_nev_ille( xnum, imageptr,
00529 order, eval, &flag) ;
00530 if ( row != 0 && row != ily - 1 )
00531 {
00532 new_sum += corrected_spec[row] ;
00533 }
00534 }
00535
00536 for ( row = 0 ; row < ily ; row++ )
00537 {
00538 if ( new_sum == 0. )
00539 {
00540 new_sum = 1. ;
00541 }
00542 if ( row == 0 )
00543 {
00544 podata[col+row*olx] = ZERO ;
00545 }
00546 else if ( row == ily - 1 )
00547 {
00548 podata[col+row*olx] = ZERO ;
00549 }
00550 else if ( isnan(corrected_spec[row]) )
00551 {
00552 podata[col+row*olx] = ZERO ;
00553 }
00554 else
00555 {
00556 corrected_spec[row] *= sum / new_sum ;
00557 podata[col+row*olx] = corrected_spec[row] ;
00558 }
00559 }
00560 }
00561 cpl_free(spec) ;
00562 cpl_free(corrected_spec) ;
00563 cpl_free(xnum) ;
00564 return retIm ;
00565 }
00574 cpl_image *
00575 sinfo_new_fine_shift_image_in_spec_cubic_spline ( cpl_image * shiftedImage,
00576 double sub_shift )
00577 {
00578 cpl_image * retIm ;
00579
00580 float* spec=NULL ;
00581 float* corrected_spec=NULL ;
00582 float* xnum=NULL ;
00583 float* eval=NULL ;
00584 float sum, new_sum ;
00585 int row, col ;
00586 int i ;
00587 int ilx=0;
00588 int ily=0;
00589 int olx=0;
00590 int oly=0;
00591
00592 float* pidata=NULL;
00593 float* podata=NULL;
00594
00595 if ( shiftedImage == NULL )
00596 {
00597 sinfo_msg_error("no image given!") ;
00598 return NULL ;
00599 }
00600 ilx=cpl_image_get_size_x(shiftedImage);
00601 ily=cpl_image_get_size_y(shiftedImage);
00602 pidata=cpl_image_get_data_float(shiftedImage);
00603
00604
00605 if ( NULL == (retIm = cpl_image_new(ilx,ily,CPL_TYPE_FLOAT)) )
00606 {
00607 sinfo_msg_error ("could not allocate memory!") ;
00608 return NULL ;
00609 }
00610 olx=cpl_image_get_size_x(retIm);
00611 oly=cpl_image_get_size_y(retIm);
00612 podata=cpl_image_get_data_float(retIm);
00613
00614 xnum=cpl_calloc(ily,sizeof(float)) ;
00615
00616 for ( i = 0 ; i < ily ; i++ )
00617 {
00618 xnum[i] = i ;
00619 }
00620
00621 spec=cpl_calloc(ily,sizeof(float)) ;
00622 corrected_spec=cpl_calloc(ily,sizeof(float)) ;
00623 eval=cpl_calloc(ily,sizeof(float)) ;
00624
00625 for ( col = 0 ; col < ilx ; col++ )
00626 {
00627 sum = 0. ;
00628 for ( row = 0 ; row < ily ; row++ )
00629 {
00630 spec[row] = pidata[col + row*ilx] ;
00631 if (isnan(spec[row]) )
00632 {
00633 for ( i = row-1 ; i <= row+1 ; i++ )
00634 {
00635 if ( i < 0 ) continue ;
00636 if ( i >= ily) continue ;
00637 corrected_spec[i] = ZERO ;
00638 }
00639 spec[row] = 0. ;
00640 }
00641 sum += spec[row] ;
00642 eval[row] = (float)sub_shift+(float)row ;
00643 }
00644
00645 if ( -1 == sinfo_function1d_natural_spline( xnum, spec, ily, eval,
00646 corrected_spec, ily ) )
00647 {
00648 sinfo_msg_error("error in spline interpolation!") ;
00649 return NULL ;
00650 }
00651
00652 new_sum = 0. ;
00653 for ( row = 0 ; row < ily ; row++ )
00654 {
00655 if ( isnan(corrected_spec[row]) )
00656 {
00657 continue ;
00658 }
00659 new_sum += corrected_spec[row] ;
00660 }
00661
00662 for ( row = 0 ; row < ily ; row++ )
00663 {
00664 if ( new_sum == 0. ) new_sum =1. ;
00665 {
00666 if ( isnan(corrected_spec[row]) )
00667 {
00668 podata[col+row*olx] = ZERO ;
00669 }
00670 else
00671 {
00672 corrected_spec[row] *= sum / new_sum ;
00673 podata[col+row*olx] = corrected_spec[row] ;
00674 }
00675 }
00676 }
00677 }
00678 cpl_free(xnum);
00679 cpl_free(spec) ;
00680 cpl_free(corrected_spec) ;
00681 cpl_free(eval) ;
00682
00683 return retIm ;
00684 }
00685
00697 cpl_imagelist * sinfo_align_cube_to_reference (cpl_imagelist * cube,
00698 cpl_image * refIm,
00699 int order,
00700 int shift_indicator )
00701 {
00702 cpl_imagelist * retCube=NULL ;
00703 cpl_image * shiftedIm=NULL ;
00704 cpl_image * fineShiftedIm=NULL ;
00705 double shift=0 ;
00706 double sub_shift=0 ;
00707 int z=0;
00708 double * ker=NULL ;
00709 cpl_image* img=NULL;
00710
00711 if (cube == NULL)
00712 {
00713 sinfo_msg_error("no input cube given!") ;
00714 return NULL ;
00715 }
00716 if (refIm == NULL)
00717 {
00718 sinfo_msg_error("no input ref. image given!") ;
00719 return NULL ;
00720 }
00721
00722
00723 retCube = cpl_imagelist_new() ;
00724
00725 if ( shift_indicator != 0 && shift_indicator != 1 )
00726 {
00727 ker = sinfo_new_generate_interpolation_kernel("tanh") ;
00728 if (ker == NULL)
00729 {
00730 sinfo_msg_error("kernel generation failure: aborting resampling") ;
00731 return NULL ;
00732 }
00733 }
00734
00735 for ( z = 0 ; z < cpl_imagelist_get_size(cube) ; z++ )
00736 {
00737
00738 img=cpl_imagelist_get(cube,z);
00739 if (isnan( shift=sinfo_new_determine_shift_by_correlation(refIm,img)))
00740 {
00741 sinfo_msg_error("error in sinfo_determineShiftByCorrelation()!") ;
00742 return NULL ;
00743 }
00744
00745 if ( NULL == (shiftedIm = sinfo_new_shift_image_in_spec(img,shift,
00746 &sub_shift)) )
00747 {
00748 sinfo_msg_error("error in sinfo_shiftImageInSpec()!") ;
00749 return NULL ;
00750 }
00751 if ( shift_indicator == 0 )
00752 {
00753 if ( NULL == (fineShiftedIm =
00754 sinfo_new_fine_shift_image_in_spec_poly (shiftedIm,
00755 sub_shift, order)))
00756 {
00757 sinfo_msg_error("error in sinfo_fineShiftImageInSpecPoly()!") ;
00758 return NULL ;
00759 }
00760 }
00761 else if ( shift_indicator == 1)
00762 {
00763 if ( NULL == (fineShiftedIm =
00764 sinfo_new_fine_shift_image_in_spec_cubic_spline (shiftedIm,
00765 sub_shift)))
00766 {
00767 sinfo_msg_error("error in fineShiftImageInSpecCubicspline()!") ;
00768 return NULL ;
00769 }
00770 }
00771
00772 else
00773 {
00774 if ( NULL == (fineShiftedIm =
00775 sinfo_new_shift_image(shiftedIm,0.,sub_shift, ker ) ) )
00776 {
00777 sinfo_msg_error("error in fineShiftImageInSpecCubicspline()!") ;
00778 return NULL ;
00779 }
00780 }
00781 cpl_imagelist_set(retCube,fineShiftedIm,z);
00782 cpl_image_delete (shiftedIm) ;
00783 cpl_image_delete (fineShiftedIm) ;
00784 }
00785 if ( shift_indicator != 0 && shift_indicator != 1 ) cpl_free(ker) ;
00786 return retCube ;
00787 }
00788
00789