89 #include "sinfo_vltPort.h"
90 #define POSIX_SOURCE 1
94 #include "sinfo_function_1d.h"
95 #include "sinfo_shift_images.h"
96 #include "sinfo_new_resampling.h"
97 #include "sinfo_globals.h"
100 static int filecounter ;
123 double sinfo_new_determine_shift_by_correlation ( cpl_image * refImage,
124 cpl_image * shiftedImage )
128 unsigned long convsize ;
133 double mean_offset2 ;
143 float derv_par[MAXPAR] ;
144 int iters, xdim, ndat ;
148 float * xdat, * wdat ;
150 char filename[FILE_NAME_SZ] ;
160 if ( NULL == refImage || NULL == shiftedImage )
165 rlx=cpl_image_get_size_x(refImage);
166 rly=cpl_image_get_size_x(refImage);
167 prdata=cpl_image_get_data_float(refImage);
168 slx=cpl_image_get_size_x(shiftedImage);
169 sly=cpl_image_get_size_x(shiftedImage);
170 psdata=cpl_image_get_data_float(shiftedImage);
172 if ( rlx != slx || rly != sly )
177 snprintf(filename, MAX_NAME_SIZE-1,
"offset%d.list", filecounter) ;
179 fp = fopen(filename,
"w") ;
183 lineref = (
float*) cpl_calloc(convsize,
sizeof(
float) ) ;
184 line = (
float*) cpl_calloc(convsize,
sizeof(
float) ) ;
186 offset2 = (
float*) cpl_calloc(slx,
sizeof(
float) ) ;
188 for ( col = 0 ; col < slx ; col++ )
191 for ( row = 0 ; row < (int) convsize ; row++ )
198 for ( row = 0 ; row < (sly) ; row++ )
200 lineref[row] = prdata[col+row*slx] ;
201 line[row] = psdata[col+row*slx] ;
204 myres = sinfo_function1d_filter_lowpass(line, convsize,
205 LOW_PASS_GAUSSIAN, 3) ;
206 refres = sinfo_function1d_filter_lowpass(lineref, convsize,
207 LOW_PASS_GAUSSIAN, 4) ;
210 halfsearch = convsize / 2 ;
211 result = sinfo_new_xcorrel( myres, convsize, refres, convsize,
212 halfsearch, &delta, &maxlag, &xcorr_max ) ;
214 if ( xcorr_max < 0. )
216 sinfo_function1d_del ( refres ) ;
217 sinfo_function1d_del ( myres ) ;
227 while (result[j] < result[i])
232 while (result[k] < result[i])
238 if ( NULL == (peak = sinfo_new_vector (width)) )
247 xdat = (
float *) cpl_calloc( peak -> n_elements,
sizeof (
float) ) ;
248 wdat = (
float *) cpl_calloc( peak -> n_elements,
sizeof (
float) ) ;
249 mpar = (
int *) cpl_calloc( MAXPAR,
sizeof (
int) ) ;
254 for ( i = 0 ; i < width ; i++ )
256 peak -> data[i] = result[k+i] ;
263 ndat = peak -> n_elements ;
269 par[2] = (float) (maxlag - k) ;
270 par[3] = (peak -> data[0] + peak -> data[peak->n_elements - 1]) / 2.0 ;
271 par[0] = result[maxlag] - (par[3]) ;
273 for ( i = 0 ; i < MAXPAR ; i++ )
280 if ( 0 > ( iters = sinfo_new_lsqfit_c( xdat, &xdim, peak -> data, wdat,
281 &ndat, par, derv_par, mpar,
282 &numpar, &tol, &its, &lab )) )
285 "in col: %d, error no.: %d", col, iters) ;
286 sinfo_new_destroy_vector ( peak ) ;
290 sinfo_function1d_del ( refres ) ;
291 sinfo_function1d_del ( myres ) ;
296 sinfo_new_destroy_vector ( peak ) ;
300 sinfo_function1d_del ( refres ) ;
301 sinfo_function1d_del ( myres ) ;
304 offset2[col] = (float)( k+par[2] - sly/2) ;
305 fprintf(fp,
"offset: %f in col: %d\n", offset2[col], col) ;
308 mean_offset2 = (double)sinfo_new_clean_mean (offset2, slx, 15., 15. ) ;
309 fprintf(fp,
"mean offset: %f\n", mean_offset2) ;
312 cpl_free ( lineref ) ;
314 cpl_free ( offset2 ) ;
316 if (filecounter > 100 ) filecounter = 0 ;
318 return mean_offset2 ;
333 sinfo_new_shift_image_in_spec ( cpl_image * shiftedImage,
348 if ( shiftedImage == NULL )
354 ilx=cpl_image_get_size_x(shiftedImage);
355 ily=cpl_image_get_size_y(shiftedImage);
356 pidata=cpl_image_get_data_float(shiftedImage);
358 intshift = sinfo_new_nint (shift) ;
359 *sub_shift = shift - (double) intshift ;
362 retIm =cpl_image_duplicate ( shiftedImage ) ;
367 if ( NULL == (retIm = cpl_image_new(ilx,ily,CPL_TYPE_FLOAT)) )
374 olx=cpl_image_get_size_x(retIm);
375 oly=cpl_image_get_size_y(retIm);
376 podata=cpl_image_get_data_float(retIm);
378 for ( col = 0 ; col < ilx ; col++ )
380 for ( row = 0 ; row < ily ; row++ )
382 if ( (row-intshift >= 0 ) && (row - intshift < oly) )
384 podata[col+(row-intshift)*olx] = pidata[col+row*olx] ;
401 sinfo_new_fine_shift_image_in_spec_poly ( cpl_image * shiftedImage,
408 float* corrected_spec=NULL ;
427 if ( shiftedImage == NULL )
432 ilx=cpl_image_get_size_x(shiftedImage);
433 ily=cpl_image_get_size_y(shiftedImage);
434 pidata=cpl_image_get_data_float(shiftedImage);
443 if ( NULL == (retIm = cpl_image_new(ilx, ily,CPL_TYPE_FLOAT)) )
449 olx=cpl_image_get_size_x(retIm);
451 podata=cpl_image_get_data_float(retIm);
453 n_points = order + 1 ;
454 if ( n_points % 2 == 0 )
456 firstpos = (int)(n_points/2) - 1 ;
460 firstpos = (int)(n_points/2) ;
463 spec=cpl_calloc(ily,
sizeof(
float)) ;
464 corrected_spec=cpl_calloc(ily,
sizeof(
float)) ;
465 xnum=cpl_calloc(order+1,
sizeof(
float)) ;
469 for ( i = 0 ; i < n_points ; i++ )
474 for ( col = 0 ; col < ilx ; col++ )
476 for ( row = 0 ; row < ily ; row++ )
478 corrected_spec[row] = 0. ;
481 for ( row = 0 ; row < ily ; row++ )
483 spec[row] = pidata[col + row*ilx] ;
484 if (isnan(spec[row]) )
488 for ( i = row - firstpos ; i < row-firstpos+n_points ; i++ )
490 if ( i < 0 ) continue ;
491 if ( i >= ily) continue ;
492 corrected_spec[i] = ZERO ;
495 if ( row != 0 && row != ily - 1 )
502 for ( row = 0 ; row < ily ; row++ )
510 if (isnan(corrected_spec[row])) continue ;
511 if ( row - firstpos < 0 )
513 imageptr = &spec[0] ;
514 eval = sub_shift + row ;
516 else if ( row - firstpos + n_points >= ily )
518 imageptr = &spec[ily - n_points] ;
519 eval = sub_shift + row + n_points - ily ;
523 imageptr = &spec[row-firstpos] ;
524 eval = sub_shift + firstpos ;
528 corrected_spec[row]=sinfo_new_nev_ille( xnum, imageptr,
529 order, eval, &flag) ;
530 if ( row != 0 && row != ily - 1 )
532 new_sum += corrected_spec[row] ;
536 for ( row = 0 ; row < ily ; row++ )
544 podata[col+row*olx] = ZERO ;
546 else if ( row == ily - 1 )
548 podata[col+row*olx] = ZERO ;
550 else if ( isnan(corrected_spec[row]) )
552 podata[col+row*olx] = ZERO ;
556 corrected_spec[row] *= sum / new_sum ;
557 podata[col+row*olx] = corrected_spec[row] ;
562 cpl_free(corrected_spec) ;
575 sinfo_new_fine_shift_image_in_spec_cubic_spline ( cpl_image * shiftedImage,
581 float* corrected_spec=NULL ;
595 if ( shiftedImage == NULL )
600 ilx=cpl_image_get_size_x(shiftedImage);
601 ily=cpl_image_get_size_y(shiftedImage);
602 pidata=cpl_image_get_data_float(shiftedImage);
605 if ( NULL == (retIm = cpl_image_new(ilx,ily,CPL_TYPE_FLOAT)) )
610 olx=cpl_image_get_size_x(retIm);
612 podata=cpl_image_get_data_float(retIm);
614 xnum=cpl_calloc(ily,
sizeof(
float)) ;
616 for ( i = 0 ; i < ily ; i++ )
621 spec=cpl_calloc(ily,
sizeof(
float)) ;
622 corrected_spec=cpl_calloc(ily,
sizeof(
float)) ;
623 eval=cpl_calloc(ily,
sizeof(
float)) ;
625 for ( col = 0 ; col < ilx ; col++ )
628 for ( row = 0 ; row < ily ; row++ )
630 spec[row] = pidata[col + row*ilx] ;
631 if (isnan(spec[row]) )
633 for ( i = row-1 ; i <= row+1 ; i++ )
635 if ( i < 0 ) continue ;
636 if ( i >= ily) continue ;
637 corrected_spec[i] = ZERO ;
642 eval[row] = (float)sub_shift+(
float)row ;
645 if ( -1 == sinfo_function1d_natural_spline( xnum, spec, ily, eval,
646 corrected_spec, ily ) )
653 for ( row = 0 ; row < ily ; row++ )
655 if ( isnan(corrected_spec[row]) )
659 new_sum += corrected_spec[row] ;
662 for ( row = 0 ; row < ily ; row++ )
664 if ( new_sum == 0. ) new_sum =1. ;
666 if ( isnan(corrected_spec[row]) )
668 podata[col+row*olx] = ZERO ;
672 corrected_spec[row] *= sum / new_sum ;
673 podata[col+row*olx] = corrected_spec[row] ;
680 cpl_free(corrected_spec) ;
697 cpl_imagelist * sinfo_align_cube_to_reference (cpl_imagelist * cube,
700 int shift_indicator )
702 cpl_imagelist * retCube=NULL ;
703 cpl_image * shiftedIm=NULL ;
704 cpl_image * fineShiftedIm=NULL ;
723 retCube = cpl_imagelist_new() ;
725 if ( shift_indicator != 0 && shift_indicator != 1 )
727 ker = sinfo_new_generate_interpolation_kernel(
"tanh") ;
735 for ( z = 0 ; z < cpl_imagelist_get_size(cube) ; z++ )
738 img=cpl_imagelist_get(cube,z);
739 if (isnan( shift=sinfo_new_determine_shift_by_correlation(refIm,img)))
745 if ( NULL == (shiftedIm = sinfo_new_shift_image_in_spec(img,shift,
751 if ( shift_indicator == 0 )
753 if ( NULL == (fineShiftedIm =
754 sinfo_new_fine_shift_image_in_spec_poly (shiftedIm,
761 else if ( shift_indicator == 1)
763 if ( NULL == (fineShiftedIm =
764 sinfo_new_fine_shift_image_in_spec_cubic_spline (shiftedIm,
774 if ( NULL == (fineShiftedIm =
775 sinfo_new_shift_image(shiftedIm,0.,sub_shift, ker ) ) )
781 cpl_imagelist_set(retCube,fineShiftedIm,z);
782 cpl_image_delete (shiftedIm) ;
783 cpl_image_delete (fineShiftedIm) ;
785 if ( shift_indicator != 0 && shift_indicator != 1 ) cpl_free(ker) ;