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 ;
140 float derv_par[MAXPAR] ;
141 int iters, xdim, ndat ;
145 float * xdat, * wdat ;
147 char filename[FILE_NAME_SZ] ;
157 if ( NULL == refImage || NULL == shiftedImage )
162 rlx=cpl_image_get_size_x(refImage);
163 rly=cpl_image_get_size_x(refImage);
164 prdata=cpl_image_get_data_float(refImage);
165 slx=cpl_image_get_size_x(shiftedImage);
166 sly=cpl_image_get_size_x(shiftedImage);
167 psdata=cpl_image_get_data_float(shiftedImage);
169 if ( rlx != slx || rly != sly )
174 snprintf(filename, MAX_NAME_SIZE-1,
"offset%d.list", filecounter) ;
176 fp = fopen(filename,
"w") ;
180 lineref = (
float*) cpl_calloc(convsize,
sizeof(
float) ) ;
181 line = (
float*) cpl_calloc(convsize,
sizeof(
float) ) ;
183 offset2 = (
float*) cpl_calloc(slx,
sizeof(
float) ) ;
185 for ( col = 0 ; col < slx ; col++ )
188 for ( row = 0 ; row < (int) convsize ; row++ )
195 for ( row = 0 ; row < (sly) ; row++ )
197 lineref[row] = prdata[col+row*slx] ;
198 line[row] = psdata[col+row*slx] ;
201 float* myres = sinfo_function1d_filter_lowpass(line, convsize,
202 LOW_PASS_GAUSSIAN, 3) ;
203 float* refres = sinfo_function1d_filter_lowpass(lineref, convsize,
204 LOW_PASS_GAUSSIAN, 4) ;
207 int halfsearch = convsize / 2 ;
208 result = sinfo_new_xcorrel( myres, convsize, refres, convsize,
209 halfsearch, &delta, &maxlag, &xcorr_max ) ;
211 if ( xcorr_max < 0. )
213 sinfo_function1d_del ( refres ) ;
214 sinfo_function1d_del ( myres ) ;
224 while (result[j] < result[i])
229 while (result[k] < result[i])
235 if ( NULL == (peak = sinfo_new_vector (width)) )
239 cpl_free ( offset2 ) ;
245 xdat = (
float *) cpl_calloc( peak -> n_elements,
sizeof (
float) ) ;
246 wdat = (
float *) cpl_calloc( peak -> n_elements,
sizeof (
float) ) ;
247 mpar = (
int *) cpl_calloc( MAXPAR,
sizeof (
int) ) ;
252 for ( i = 0 ; i < width ; i++ )
254 peak -> data[i] = result[k+i] ;
261 ndat = peak -> n_elements ;
267 par[2] = (float) (maxlag - k) ;
268 par[3] = (peak -> data[0] + peak -> data[peak->n_elements - 1]) / 2.0 ;
269 par[0] = result[maxlag] - (par[3]) ;
271 for ( i = 0 ; i < MAXPAR ; i++ )
278 if ( 0 > ( iters = sinfo_new_lsqfit_c( xdat, &xdim, peak -> data, wdat,
279 &ndat, par, derv_par, mpar,
280 &numpar, &tol, &its, &lab )) )
283 "in col: %d, error no.: %d", col, iters) ;
284 sinfo_new_destroy_vector ( peak ) ;
288 sinfo_function1d_del ( refres ) ;
289 sinfo_function1d_del ( myres ) ;
294 sinfo_new_destroy_vector ( peak ) ;
298 sinfo_function1d_del ( refres ) ;
299 sinfo_function1d_del ( myres ) ;
302 offset2[col] = (float)( k+par[2] - sly/2) ;
303 fprintf(fp,
"offset: %f in col: %d\n", offset2[col], col) ;
306 mean_offset2 = (double)sinfo_new_clean_mean (offset2, slx, 15., 15. ) ;
307 fprintf(fp,
"mean offset: %f\n", mean_offset2) ;
310 cpl_free ( lineref ) ;
312 cpl_free ( offset2 ) ;
314 if (filecounter > 100 ) filecounter = 0 ;
316 return mean_offset2 ;
331 sinfo_new_shift_image_in_spec ( cpl_image * shiftedImage,
346 if ( shiftedImage == NULL )
352 ilx=cpl_image_get_size_x(shiftedImage);
353 ily=cpl_image_get_size_y(shiftedImage);
354 pidata=cpl_image_get_data_float(shiftedImage);
356 intshift = sinfo_new_nint (shift) ;
357 *sub_shift = shift - (double) intshift ;
360 retIm =cpl_image_duplicate ( shiftedImage ) ;
365 if ( NULL == (retIm = cpl_image_new(ilx,ily,CPL_TYPE_FLOAT)) )
372 olx=cpl_image_get_size_x(retIm);
373 oly=cpl_image_get_size_y(retIm);
374 podata=cpl_image_get_data_float(retIm);
376 for ( col = 0 ; col < ilx ; col++ )
378 for ( row = 0 ; row < ily ; row++ )
380 if ( (row-intshift >= 0 ) && (row - intshift < oly) )
382 podata[col+(row-intshift)*olx] = pidata[col+row*olx] ;
399 sinfo_new_fine_shift_image_in_spec_poly ( cpl_image * shiftedImage,
406 float* corrected_spec=NULL ;
425 if ( shiftedImage == NULL )
430 ilx=cpl_image_get_size_x(shiftedImage);
431 ily=cpl_image_get_size_y(shiftedImage);
432 pidata=cpl_image_get_data_float(shiftedImage);
441 if ( NULL == (retIm = cpl_image_new(ilx, ily,CPL_TYPE_FLOAT)) )
447 olx=cpl_image_get_size_x(retIm);
449 podata=cpl_image_get_data_float(retIm);
451 n_points = order + 1 ;
452 if ( n_points % 2 == 0 )
454 firstpos = (int)(n_points/2) - 1 ;
458 firstpos = (int)(n_points/2) ;
461 spec=cpl_calloc(ily,
sizeof(
float)) ;
462 corrected_spec=cpl_calloc(ily,
sizeof(
float)) ;
463 xnum=cpl_calloc(order+1,
sizeof(
float)) ;
467 for ( i = 0 ; i < n_points ; i++ )
472 for ( col = 0 ; col < ilx ; col++ )
474 for ( row = 0 ; row < ily ; row++ )
476 corrected_spec[row] = 0. ;
479 for ( row = 0 ; row < ily ; row++ )
481 spec[row] = pidata[col + row*ilx] ;
482 if (isnan(spec[row]) )
486 for ( i = row - firstpos ; i < row-firstpos+n_points ; i++ )
488 if ( i < 0 ) continue ;
489 if ( i >= ily) continue ;
490 corrected_spec[i] = ZERO ;
493 if ( row != 0 && row != ily - 1 )
500 for ( row = 0 ; row < ily ; row++ )
508 if (isnan(corrected_spec[row])) continue ;
509 if ( row - firstpos < 0 )
511 imageptr = &spec[0] ;
512 eval = sub_shift + row ;
514 else if ( row - firstpos + n_points >= ily )
516 imageptr = &spec[ily - n_points] ;
517 eval = sub_shift + row + n_points - ily ;
521 imageptr = &spec[row-firstpos] ;
522 eval = sub_shift + firstpos ;
526 corrected_spec[row]=sinfo_new_nev_ille( xnum, imageptr,
527 order, eval, &flag) ;
528 if ( row != 0 && row != ily - 1 )
530 new_sum += corrected_spec[row] ;
534 for ( row = 0 ; row < ily ; row++ )
542 podata[col+row*olx] = ZERO ;
544 else if ( row == ily - 1 )
546 podata[col+row*olx] = ZERO ;
548 else if ( isnan(corrected_spec[row]) )
550 podata[col+row*olx] = ZERO ;
554 corrected_spec[row] *= sum / new_sum ;
555 podata[col+row*olx] = corrected_spec[row] ;
560 cpl_free(corrected_spec) ;
573 sinfo_new_fine_shift_image_in_spec_cubic_spline ( cpl_image * shiftedImage,
579 float* corrected_spec=NULL ;
593 if ( shiftedImage == NULL )
598 ilx=cpl_image_get_size_x(shiftedImage);
599 ily=cpl_image_get_size_y(shiftedImage);
600 pidata=cpl_image_get_data_float(shiftedImage);
603 if ( NULL == (retIm = cpl_image_new(ilx,ily,CPL_TYPE_FLOAT)) )
608 olx=cpl_image_get_size_x(retIm);
610 podata=cpl_image_get_data_float(retIm);
612 xnum=cpl_calloc(ily,
sizeof(
float)) ;
614 for ( i = 0 ; i < ily ; i++ )
619 spec=cpl_calloc(ily,
sizeof(
float)) ;
620 corrected_spec=cpl_calloc(ily,
sizeof(
float)) ;
621 eval=cpl_calloc(ily,
sizeof(
float)) ;
623 for ( col = 0 ; col < ilx ; col++ )
626 for ( row = 0 ; row < ily ; row++ )
628 spec[row] = pidata[col + row*ilx] ;
629 if (isnan(spec[row]) )
631 for ( i = row-1 ; i <= row+1 ; i++ )
633 if ( i < 0 ) continue ;
634 if ( i >= ily) continue ;
635 corrected_spec[i] = ZERO ;
640 eval[row] = (float)sub_shift+(
float)row ;
643 if ( -1 == sinfo_function1d_natural_spline( xnum, spec, ily, eval,
644 corrected_spec, ily ) )
651 for ( row = 0 ; row < ily ; row++ )
653 if ( isnan(corrected_spec[row]) )
657 new_sum += corrected_spec[row] ;
660 for ( row = 0 ; row < ily ; row++ )
662 if ( new_sum == 0. ) new_sum =1. ;
664 if ( isnan(corrected_spec[row]) )
666 podata[col+row*olx] = ZERO ;
670 corrected_spec[row] *= sum / new_sum ;
671 podata[col+row*olx] = corrected_spec[row] ;
678 cpl_free(corrected_spec) ;
695 cpl_imagelist * sinfo_align_cube_to_reference (cpl_imagelist * cube,
698 int shift_indicator )
700 cpl_imagelist * retCube=NULL ;
701 cpl_image * fineShiftedIm=NULL ;
717 retCube = cpl_imagelist_new() ;
719 if ( shift_indicator != 0 && shift_indicator != 1 )
721 ker = sinfo_new_generate_interpolation_kernel(
"tanh") ;
725 cpl_imagelist_delete(retCube);
730 for ( z = 0 ; z < cpl_imagelist_get_size(cube) ; z++ )
733 cpl_image* img=cpl_imagelist_get(cube,z);
735 if (isnan( shift=sinfo_new_determine_shift_by_correlation(refIm,img)))
741 cpl_image * shiftedIm=NULL ;
743 if ( NULL == (shiftedIm = sinfo_new_shift_image_in_spec(img,shift,
749 if ( shift_indicator == 0 )
751 if ( NULL == (fineShiftedIm =
752 sinfo_new_fine_shift_image_in_spec_poly (shiftedIm,
759 else if ( shift_indicator == 1)
761 if ( NULL == (fineShiftedIm =
762 sinfo_new_fine_shift_image_in_spec_cubic_spline (shiftedIm,
772 if ( NULL == (fineShiftedIm =
773 sinfo_new_shift_image(shiftedIm,0.,sub_shift, ker ) ) )
779 cpl_imagelist_set(retCube,fineShiftedIm,z);
780 cpl_image_delete (shiftedIm) ;
781 cpl_image_delete (fineShiftedIm) ;
783 if ( shift_indicator != 0 && shift_indicator != 1 ) cpl_free(ker) ;
#define sinfo_msg_error(...)
Print an error message.
#define sinfo_msg_warning(...)
Print an warning message.