36 #include "irplib_distortion.h"
38 #include "irplib_flat.h"
39 #include "irplib_utils.h"
40 #include "irplib_polynomial.h"
49 #define IRPLIB_MAX(A,B) ((A) > (B) ? (A) : (B))
50 #define IRPLIB_MIN(A,B) ((A) < (B) ? (A) : (B))
52 #define ARC_MINGOODPIX 100
53 #define ARC_MINARCLENFACT 2.0
54 #define ARC_MINNBARCS 4
55 #define ARC_RANGE_FACT 3.0
56 #define ARC_WINDOWSIZE 32
58 #define TRESH_MEDIAN_MIN 0.0
59 #define TRESH_SIGMA_MAX 200.0
71 static cpl_apertures * irplib_distortion_detect_arcs(cpl_image *,
72 cpl_image **,
int,
int,
double,
int,
int,
int,
int);
73 static cpl_error_code irplib_distortion_fill_border(cpl_image *,
int,
int,
75 static int irplib_distortion_threshold1d(cpl_image *,
double, cpl_image *,
77 static cpl_error_code irplib_distortion_purge_arcs(cpl_apertures **, cpl_image *,
78 const cpl_image *,
int,
int,
80 static cpl_error_code irplib_distortion_fill_arc_positions(cpl_bivector *,
84 const cpl_apertures *);
86 static double irplib_distortion_get_row_centroid(
const cpl_image *,
87 const cpl_image *,
int,
int);
89 static int irplib_distortion_sub_hor_lowpass(cpl_image *,
int);
90 static cpl_image * irplib_distortion_remove_ramp(
const cpl_image *);
92 static cpl_error_code irplib_image_filter_background_line(cpl_image *,
93 const cpl_image *,
int, cpl_boolean) ;
95 static cpl_error_code irplib_polynomial_fit_2d(cpl_polynomial *,
97 const cpl_vector *,
int,
100 static cpl_matrix * irplib_matrix_product_normal_create(
const cpl_matrix *);
138 cpl_polynomial * irplib_distortion_estimate(
139 const cpl_image * org,
149 cpl_apertures ** arcs)
151 cpl_image * local_im;
152 cpl_image * filtered;
153 cpl_image * label_image;
154 double rightmost, leftmost;
156 cpl_vector * values_to_fit;
158 cpl_polynomial * poly2d;
160 const int nx = cpl_image_get_size_x(org);
161 const int ny = cpl_image_get_size_y(org);
162 const int min_arc_range = (int)(nx / ARC_RANGE_FACT);
166 cpl_ensure(org != NULL, CPL_ERROR_NULL_INPUT, NULL);
167 cpl_ensure(kappa >= 0.0, CPL_ERROR_ILLEGAL_INPUT, NULL);
168 cpl_ensure(max_arc_width > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
173 filtered = cpl_image_new(nx, ny, cpl_image_get_type(org));
175 irplib_image_filter_background_line(filtered, org, max_arc_width, CPL_TRUE);
178 local_im = irplib_distortion_remove_ramp(filtered);
179 cpl_image_delete(filtered);
184 cpl_error_ensure(local_im != NULL, cpl_error_get_code(),
185 return(NULL),
"Cannot clean the image");
188 *arcs = irplib_distortion_detect_arcs(local_im, &label_image, arc_sat,
189 max_arc_width, kappa, xmin, ymin,
192 cpl_image_delete(local_im);
193 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
194 "Cannot detect the arcs");
197 n_arcs = cpl_apertures_get_size(*arcs);
198 cpl_msg_info(cpl_func,
"%d detected arcs", n_arcs);
201 rightmost = leftmost = cpl_apertures_get_pos_x(*arcs, 1);
202 for (i=1; i<n_arcs; i++) {
203 if (cpl_apertures_get_pos_x(*arcs, i+1) < leftmost)
204 leftmost = cpl_apertures_get_pos_x(*arcs, i+1);
205 if (cpl_apertures_get_pos_x(*arcs, i+1) > rightmost)
206 rightmost = cpl_apertures_get_pos_x(*arcs, i+1);
208 if ((
int)(rightmost-leftmost) < min_arc_range) {
209 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
210 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
211 "too narrow range (%g-%g)<%d",
212 rightmost, leftmost, min_arc_range);
214 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
217 cpl_apertures_delete(*arcs);
218 cpl_image_delete(local_im);
219 cpl_image_delete(label_image);
225 cpl_msg_info(cpl_func,
"Create deformation grid");
226 grid = cpl_bivector_new(n_arcs * ny);
227 values_to_fit = cpl_vector_new(n_arcs * ny);
229 if (irplib_distortion_fill_arc_positions(grid, values_to_fit, local_im,
230 label_image, *arcs)){
231 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
232 "cannot get arcs positions");
233 cpl_apertures_delete(*arcs);
234 cpl_image_delete(local_im);
235 cpl_image_delete(label_image);
239 cpl_image_delete(label_image);
240 cpl_image_delete(local_im);
243 poly2d = cpl_polynomial_new(2);
244 if (irplib_polynomial_fit_2d(poly2d, grid, values_to_fit, degree,
246 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
247 "cannot apply the 2d fit");
248 cpl_bivector_delete(grid);
249 cpl_vector_delete(values_to_fit);
250 cpl_apertures_delete(*arcs);
255 cpl_msg_info(cpl_func,
256 "Fitted a %d. degree 2D-polynomial to %"CPL_SIZE_FORMAT
" points "
257 "with mean-square error: %g", degree,
258 cpl_vector_get_size(values_to_fit), mse);
261 cpl_bivector_delete(grid);
262 cpl_vector_delete(values_to_fit);
285 static cpl_apertures * irplib_distortion_detect_arcs(
287 cpl_image ** label_im,
296 const int ny = cpl_image_get_size_y(im);
298 const int min_arclen = (int)(ny / ARC_MINARCLENFACT);
301 cpl_image * collapsed;
303 double threshold, fillval, median_val, sigma;
312 median_val = cpl_image_get_median_dev(im, &sigma);
313 fillval = median_val-sigma/2.0;
314 if (irplib_distortion_fill_border(im, xmin, ymin, xmax, ymax, fillval)) {
315 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
316 "cannot fill bad zones");
321 filt_im = cpl_image_duplicate(im);
322 if (irplib_distortion_sub_hor_lowpass(filt_im, ARC_WINDOWSIZE) == -1) {
323 cpl_image_delete(filt_im);
328 median_val = cpl_image_get_median_dev(filt_im, &sigma);
331 if (median_val < TRESH_MEDIAN_MIN) median_val = TRESH_MEDIAN_MIN;
332 if (sigma > TRESH_SIGMA_MAX) sigma = TRESH_SIGMA_MAX;
335 threshold = median_val + sigma * kappa;
338 collapsed = cpl_image_collapse_median_create(filt_im, 0, 0, 0);
341 if (irplib_distortion_threshold1d(filt_im, median_val, collapsed, 0.0)==-1) {
342 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
343 "cannot threshold the filtered image");
344 cpl_image_delete(filt_im);
345 cpl_image_delete(collapsed);
348 cpl_image_delete(collapsed);
351 bin_im = cpl_mask_threshold_image_create(filt_im, threshold,
353 cpl_image_delete(filt_im);
354 if (bin_im == NULL) {
355 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
356 "cannot binarise the image");
361 ngoodpix = cpl_mask_count(bin_im);
362 if (ngoodpix < ARC_MINGOODPIX) {
363 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
364 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
365 "Too few (%d) white pixels", ngoodpix);
367 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
368 "Too few white pixels");
370 cpl_mask_delete(bin_im);
375 filter = cpl_mask_new(3, 3);
376 cpl_mask_not(filter);
377 cpl_mask_filter(bin_im, bin_im, filter, CPL_FILTER_OPENING,
379 cpl_mask_delete(filter);
382 *label_im = cpl_image_labelise_mask_create(bin_im, &nobj);
383 cpl_mask_delete(bin_im);
386 if ((det = cpl_apertures_new_from_image(im, *label_im)) == NULL) {
387 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
388 "Cannot compute arcs stats");
389 cpl_image_delete(*label_im);
395 if (irplib_distortion_purge_arcs(&det, *label_im, im, min_arclen,
396 max_arc_width, arc_sat)) {
397 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
398 "Cannot purge the arcs");
399 cpl_image_delete(*label_im);
401 cpl_apertures_delete(det);
404 if (cpl_apertures_get_size(det) < ARC_MINNBARCS) {
405 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
406 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
407 "Not enough valid arcs (%"CPL_SIZE_FORMAT
" < %d)",
408 cpl_apertures_get_size(det), ARC_MINNBARCS);
410 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
411 "Not enough valid arcs, min="
412 IRPLIB_STRINGIFY(ARC_MINNBARCS));
414 cpl_image_delete(*label_im);
416 cpl_apertures_delete(det);
435 static cpl_error_code irplib_distortion_fill_border(cpl_image *
self,
442 const int nx = cpl_image_get_size_x(
self);
443 const int ny = cpl_image_get_size_y(
self);
444 float * pfi = cpl_image_get_data_float(
self);
445 const float fvalue = (float)fillval;
449 cpl_ensure_code(pfi != NULL, cpl_error_get_code());
452 xmin = IRPLIB_MIN(xmin, nx+1);
453 ymax = IRPLIB_MIN(ymax, ny);
456 xmax = IRPLIB_MAX(xmax, xmin - 1);
457 ymin = IRPLIB_MIN(ymin, ymax + 1);
461 for (j = 0; j < ymin-1; j++) {
462 for (i = 0; i < nx; i++) {
463 pfi[i+j*nx] = fvalue;
468 for (; j < ymax; j++) {
469 for (i = 0; i < xmin-1; i++) {
470 pfi[i+j*nx] = fvalue;
472 for (i = xmax; i < nx; i++) {
473 pfi[i+j*nx] = fvalue;
478 for (; j < ny; j++) {
479 for (i = 0; i < nx; i++) {
480 pfi[i+j*nx] = fvalue;
484 return CPL_ERROR_NONE;
487 static int irplib_distortion_threshold1d(
499 if (im == NULL)
return -1;
500 if (im1d == NULL)
return -1;
501 if (cpl_image_get_type(im) != CPL_TYPE_FLOAT)
return -1;
502 if (cpl_image_get_type(im1d) != CPL_TYPE_FLOAT)
return -1;
505 pim = cpl_image_get_data_float(im);
506 pim1d = cpl_image_get_data_float(im1d);
507 nx = cpl_image_get_size_x(im);
508 ny = cpl_image_get_size_y(im);
512 if (pim1d[i] < threshold) {
513 for (j=0; j<ny; j++) pim[i+j*nx] = (
float)newval;
520 static int irplib_distortion_sub_hor_lowpass(
526 cpl_vector * avglinehi;
527 cpl_vector * avglinelo;
530 int lopos, hipos, nx, ny;
534 if (im == NULL)
return -1;
535 if (filt_size <= 0)
return -1;
538 nx = cpl_image_get_size_x(im);
539 ny = cpl_image_get_size_y(im);
541 hipos = (int)(3*ny/4);
544 if ((linehi = cpl_vector_new_from_image_row(im, hipos)) == NULL) {
547 if ((linelo = cpl_vector_new_from_image_row(im, lopos)) == NULL) {
548 cpl_vector_delete(linehi);
553 if ((avglinehi = cpl_vector_filter_median_create(linehi,
554 filt_size)) == NULL) {
555 cpl_vector_delete(linehi);
556 cpl_vector_delete(linelo);
559 cpl_vector_delete(linehi);
561 if ((avglinelo = cpl_vector_filter_median_create(linelo,
562 filt_size)) == NULL) {
563 cpl_vector_delete(linelo);
564 cpl_vector_delete(avglinehi);
567 cpl_vector_delete(linelo);
570 cpl_vector_add(avglinehi, avglinelo);
571 cpl_vector_delete(avglinelo);
572 cpl_vector_divide_scalar(avglinehi, 2.0);
575 pavglinehi = cpl_vector_get_data(avglinehi);
576 pim = cpl_image_get_data_float(im);
577 for (i=0; i<nx; i++) {
578 for (j=0; j<ny; j++) {
579 pim[i+j*nx] -= pavglinehi[i];
582 cpl_vector_delete(avglinehi);
600 cpl_error_code irplib_distortion_purge_arcs(cpl_apertures **
self,
602 const cpl_image * arc_im,
607 const double ycenter = 0.5 * (1 + cpl_image_get_size_y(arc_im));
615 cpl_ensure_code(
self != NULL, CPL_ERROR_NULL_INPUT);
618 narcs = cpl_apertures_get_size(*
self);
620 cpl_ensure_code(narcs > 0, CPL_ERROR_DATA_NOT_FOUND);
621 cpl_ensure_code(cpl_image_get_type(lab_im) == CPL_TYPE_INT,
622 CPL_ERROR_ILLEGAL_INPUT);
625 relabel = cpl_calloc(narcs,
sizeof(
int));
628 for (i = 0; i < narcs; i++) {
631 + cpl_apertures_get_top(*
self, i+1)
632 - cpl_apertures_get_bottom(*
self, i+1);
634 if (cpl_apertures_get_top(*
self, i+1) < ycenter)
continue;
635 if (cpl_apertures_get_bottom(*
self, i+1) > ycenter)
continue;
637 if (arclen > min_arclen) {
638 const int arcwidth = 1
639 + cpl_apertures_get_right(*
self, i+1)
640 - cpl_apertures_get_left(*
self, i+1);
641 if (arcwidth < max_arcwidth) {
642 const int edge = cpl_apertures_get_left_y(*
self, i+1);
644 const double mean = cpl_apertures_get_mean(*
self, i+1);
645 if (mean < arc_sat) {
646 relabel[i] = ++nkeep;
648 if (nkeep == i+1) ifirst = nkeep;
657 int * plabim = cpl_image_get_data_int(lab_im);
658 const int npix = cpl_image_get_size_x(lab_im)
659 * cpl_image_get_size_y(lab_im);
663 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
664 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
665 "All %d arc(s) are invalid", narcs);
667 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
668 "All arcs are invalid");
672 for (i = 0; i < npix; i++) {
673 const int label = plabim[i];
675 if (label < 0 || label > narcs)
break;
676 if (label >= ifirst) plabim[i] = relabel[label-1];
682 return cpl_error_set(cpl_func, plabim[i] < 0
683 ? CPL_ERROR_ILLEGAL_INPUT
684 : CPL_ERROR_INCOMPATIBLE_INPUT);
688 cpl_apertures_delete(*
self);
689 *
self = cpl_apertures_new_from_image(arc_im, lab_im);
695 cpl_msg_info(cpl_func,
"Purged %d of %d arcs (1st purged=%d)", narcs - nkeep,
699 cpl_ensure_code(*
self != NULL, cpl_error_get_code());
701 return CPL_ERROR_NONE;
720 static cpl_error_code
721 irplib_distortion_fill_arc_positions(cpl_bivector * grid,
722 cpl_vector * fitvalues,
723 const cpl_image * in,
724 const cpl_image * label_im,
725 const cpl_apertures * det)
727 const int narcs = cpl_apertures_get_size(det);
728 int nfitvals = cpl_vector_get_size(fitvalues);
729 const int nx = cpl_image_get_size_x(label_im);
730 const int ny = cpl_image_get_size_y(label_im);
731 cpl_image * filt_img;
733 cpl_vector * gridx = cpl_bivector_get_x(grid);
734 cpl_vector * gridy = cpl_bivector_get_y(grid);
735 cpl_polynomial* dist1d;
736 cpl_matrix * dist1dx = NULL;
737 cpl_vector * dist1dy = NULL;
744 cpl_ensure_code(nfitvals > 0, CPL_ERROR_DATA_NOT_FOUND);
745 cpl_ensure_code(narcs > 0, CPL_ERROR_DATA_NOT_FOUND);
746 cpl_ensure_code(cpl_image_get_type(label_im) == CPL_TYPE_INT,
747 CPL_ERROR_TYPE_MISMATCH);
750 if (nfitvals < narcs * ny) {
751 nfitvals = narcs * ny;
752 cpl_vector_set_size(fitvalues, nfitvals);
754 if (cpl_vector_get_size(gridx) < nfitvals ||
755 cpl_vector_get_size(gridy) < nfitvals) {
756 cpl_vector_set_size(gridx, nfitvals);
757 cpl_vector_set_size(gridy, nfitvals);
761 dgridx = cpl_vector_get_data(gridx);
762 dgridy = cpl_vector_get_data(gridy);
763 dfitv = cpl_vector_get_data(fitvalues);
766 kernel = cpl_mask_new(3, 3);
767 cpl_mask_not(kernel);
768 filt_img = cpl_image_new(nx, ny, cpl_image_get_type(in));
769 cpl_image_filter_mask(filt_img, in, kernel, CPL_FILTER_MEDIAN,
771 cpl_mask_delete(kernel);
773 dist1d = cpl_polynomial_new(1);
775 for (obj = 0; obj < narcs; obj++) {
777 const int * plabel_im = cpl_image_get_data_int_const(label_im);
778 const int ndist1d = cpl_apertures_get_top(det, obj+1)
779 - cpl_apertures_get_bottom(det, obj+1) + 1;
780 cpl_boolean sampsym = CPL_TRUE;
784 (void)cpl_matrix_unwrap(dist1dx);
785 (void)cpl_vector_unwrap(dist1dy);
786 dist1dx = cpl_matrix_wrap(1, ndist1d, dgridy + ndone);
787 dist1dy = cpl_vector_wrap(ndist1d, dfitv + ndone);
791 for (j = cpl_apertures_get_bottom(det, obj+1)-1;
792 j < cpl_apertures_get_top(det, obj+1); j++) {
794 for (i = 0; i < nx; i++) {
795 if (plabel_im[i + j * nx] == obj + 1)
break;
799 cpl_errorstate prestate = cpl_errorstate_get();
801 const double x_finepos
802 = irplib_distortion_get_row_centroid(filt_img, label_im,
804 if (!cpl_errorstate_is_equal(prestate)) {
805 irplib_error_recover(prestate,
"Could not find X-position "
806 "for line %d at y=%d (x=%d)",
808 }
else if (x_finepos >= 0.0) {
809 cpl_matrix_set(dist1dx, 0, k, 1.0 + j);
810 cpl_vector_set(dist1dy, k, 1.0 + x_finepos);
811 if (k > 0 && j != 1 + prevj) sampsym = CPL_FALSE;
818 double ref_xpos, grad;
819 cpl_error_code error;
820 const cpl_boolean did_drop = k != ndist1d;
821 const cpl_size mindeg = 0;
822 const cpl_size maxdeg = 2;
826 dist1dx = cpl_matrix_wrap(1, k, cpl_matrix_unwrap(dist1dx));
827 dist1dy = cpl_vector_wrap(k, cpl_vector_unwrap(dist1dy));
830 error = cpl_polynomial_fit(dist1d, dist1dx, &sampsym, dist1dy, NULL,
831 CPL_FALSE, &mindeg, &maxdeg);
833 cpl_msg_error(cpl_func,
"1D-fit failed");
837 ref_xpos = cpl_polynomial_eval_1d(dist1d, 0.5 * (ny + 1), &grad);
839 for (j = cpl_apertures_get_bottom(det, obj+1)-1;
840 j < cpl_apertures_get_top(det, obj+1); j++) {
841 const double xpos = cpl_polynomial_eval_1d(dist1d, j+1.0, NULL);
843 dfitv [ndone] = xpos;
844 dgridx[ndone] = ref_xpos;
848 dgridy[ndone] = 1.0 + j;
851 cpl_msg_info(cpl_func,
"Line %d has center gradient %g", obj+1,
856 cpl_image_delete(filt_img);
857 cpl_polynomial_delete(dist1d);
858 (void)cpl_matrix_unwrap(dist1dx);
859 (void)cpl_vector_unwrap(dist1dy);
861 cpl_msg_info(cpl_func,
"Found %d fitting points ("
862 "expected up to %d points)", ndone, nfitvals);
864 cpl_ensure_code(obj == narcs, cpl_error_get_code());
866 cpl_ensure_code(ndone > 0, CPL_ERROR_DATA_NOT_FOUND);
868 cpl_vector_set_size(fitvalues, ndone);
869 cpl_vector_set_size(gridx, ndone);
870 cpl_vector_set_size(gridy, ndone);
872 return CPL_ERROR_NONE;
886 static double irplib_distortion_get_row_centroid(
const cpl_image * im,
887 const cpl_image * label_im,
891 const int nx = cpl_image_get_size_x(im);
892 const int ny = cpl_image_get_size_y(im);
893 const int ynx = y * nx;
894 const float * pim = cpl_image_get_data_float_const(im);
895 const int * plabel_im = cpl_image_get_data_int_const(label_im);
904 cpl_ensure(pim != NULL, cpl_error_get_code(), -1.0);
905 cpl_ensure(plabel_im != NULL, cpl_error_get_code(), -2.0);
906 cpl_ensure(x >= 0, CPL_ERROR_ILLEGAL_INPUT, -3.0);
907 cpl_ensure(y >= 0, CPL_ERROR_ILLEGAL_INPUT, -4.0);
908 cpl_ensure(x < nx, CPL_ERROR_ILLEGAL_INPUT, -5.0);
909 cpl_ensure(y < ny, CPL_ERROR_ILLEGAL_INPUT, -6.0);
911 max = (double)pim[x + ynx];
912 objnum = plabel_im[x + ynx];
916 const double val = (double)pim[x + ynx];
922 if (firstpos < 0) firstpos = x;
935 }
while (x < nx && objnum == plabel_im[x + ynx]);
937 cpl_ensure(sum > 0.0, CPL_ERROR_DATA_NOT_FOUND, -7.0);
945 return (wsum < sum * firstpos || wsum > sum * lastpos)
946 ? maxpos : wsum / sum;
956 #define IS_NB_TESTPOINTS 8
957 #define IS_MIN_SLOPE 0.01
958 #define IS_MAX_SLOPE_DIF 0.075
959 #define IS_MAX_FIT_EDGE_DIF 0.05
960 #define IS_MIN_RAMP 10.0
961 #define IS_MAX_MNERR 13.0
962 #define IS_MAX_MNERR_DIF 8.0
963 #define IS_MAX_INTER_DIF 20.0
964 #define IS_SKIPZONE 2.5
965 #define SQR(x) ((x)*(x))
966 static cpl_image * irplib_distortion_remove_ramp(
const cpl_image * in)
969 const int nx = cpl_image_get_size_x(in);
970 const int ny = cpl_image_get_size_y(in);
971 const int yhi = (int)(ny/2);
972 const int ylo = yhi - 1;
973 cpl_bivector * testpointlo;
974 double * testpointlo_x;
975 double * testpointlo_y;
976 cpl_bivector * testpointhi;
977 double * testpointhi_x;
978 double * testpointhi_y;
979 const int spacing = ny / (IS_SKIPZONE*IS_NB_TESTPOINTS);
980 double rampdif, fitslope;
984 double * median_data;
985 double medianerrlo, medianerrhi;
991 cpl_ensure(cpl_image_get_type(in) == CPL_TYPE_FLOAT,
992 CPL_ERROR_UNSUPPORTED_MODE, NULL);
994 if (ny < IS_SKIPZONE * IS_NB_TESTPOINTS){
995 #if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
996 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
997 "image has %d lines, min="
998 IRPLIB_STRINGIFY(IS_SKIPZONE)
"*"
999 IRPLIB_STRINGIFY(IS_NB_TESTPOINTS), ny);
1001 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1002 "image has too few lines, min="
1003 IRPLIB_STRINGIFY(IS_SKIPZONE)
"*"
1004 IRPLIB_STRINGIFY(IS_NB_TESTPOINTS));
1010 testpointhi = cpl_bivector_new(IS_NB_TESTPOINTS);
1011 testpointhi_x = cpl_bivector_get_x_data(testpointhi);
1012 testpointhi_y = cpl_bivector_get_y_data(testpointhi);
1013 testpointlo = cpl_bivector_new(IS_NB_TESTPOINTS);
1014 testpointlo_x = cpl_bivector_get_x_data(testpointlo);
1015 testpointlo_y = cpl_bivector_get_y_data(testpointlo);
1016 for (i=0; i<IS_NB_TESTPOINTS; i++) {
1018 cpl_vector * tmp_vector;
1019 y = yhi + i * spacing;
1020 tmp_vector = cpl_vector_new_from_image_row(in, y+1);
1021 testpointhi_x[i] = y - ny / 2;
1022 testpointhi_y[i] = cpl_vector_get_median_const(tmp_vector);
1023 cpl_vector_delete(tmp_vector);
1024 y = ylo - i * spacing;
1025 tmp_vector = cpl_vector_new_from_image_row(in, y+1);
1026 testpointlo_x[IS_NB_TESTPOINTS-i-1] = y;
1027 testpointlo_y[IS_NB_TESTPOINTS-i-1]=cpl_vector_get_median_const(tmp_vector);
1028 cpl_vector_delete(tmp_vector);
1033 testpointhi_y, IS_NB_TESTPOINTS);
1035 testpointlo_y, IS_NB_TESTPOINTS);
1038 median = cpl_vector_new(IS_NB_TESTPOINTS);
1039 median_data = cpl_vector_get_data(median);
1040 for (i=0; i<IS_NB_TESTPOINTS; i++) {
1041 median_data[i]=SQR(testpointhi_y[i]
1042 - pol_coefhi[0] - pol_coefhi[1] * testpointhi_x[i]);
1044 medianerrhi = cpl_vector_get_median(median);
1045 for (i=0; i<IS_NB_TESTPOINTS; i++) {
1046 median_data[i]=SQR(testpointlo_y[i]
1047 - pol_coeflo[0] - pol_coeflo[1] * testpointlo_x[i]);
1049 medianerrlo = cpl_vector_get_median(median);
1050 cpl_vector_delete(median);
1051 rampdif = testpointlo_y[IS_NB_TESTPOINTS-1] - testpointhi_y[0];
1052 slope = rampdif / (ny/2.0);
1053 fitslope = (pol_coefhi[1] + pol_coeflo[1]) / 2.0;
1055 cpl_bivector_delete(testpointlo);
1056 cpl_bivector_delete(testpointhi);
1059 if (fabs(rampdif)<IS_MIN_RAMP ||
1060 fabs(pol_coefhi[1]) < IS_MIN_SLOPE ||
1061 fabs(pol_coeflo[1]) < IS_MIN_SLOPE ||
1062 pol_coefhi[1]/pol_coeflo[1]<0.5 ||
1063 pol_coefhi[1]/pol_coeflo[1]>2.0 ||
1064 fabs(pol_coefhi[1]-pol_coeflo[1])>IS_MAX_SLOPE_DIF ||
1065 fabs(pol_coefhi[0]-pol_coeflo[0]) > IS_MAX_INTER_DIF ||
1066 medianerrlo> IS_MAX_MNERR ||
1067 medianerrhi> IS_MAX_MNERR ||
1068 fabs(medianerrlo-medianerrhi) >IS_MAX_MNERR_DIF ||
1069 fabs(slope-fitslope) > IS_MAX_FIT_EDGE_DIF ||
1070 slope/fitslope<0.5 ||
1071 slope/fitslope>2.0) ramp_present = 0;
1072 else ramp_present = 1;
1074 cpl_free(pol_coeflo);
1075 cpl_free(pol_coefhi);
1078 out = cpl_image_duplicate(in);
1079 pout = cpl_image_get_data_float(out);
1080 if (ramp_present == 1) {
1083 for (j=0; j<ny/2; j++) {
1084 val = slope * (j-ny/2);
1085 for (i=0; i<nx; i++)
1086 pout[i+j*nx] -= val;
1088 for (j=ny/2; j<ny; j++) {
1089 val = slope * (j-ny);
1090 for (i=0; i<nx; i++)
1091 pout[i+j*nx] -= val;
1114 static cpl_error_code irplib_image_filter_background_line(cpl_image *
self,
1115 const cpl_image * other,
1117 cpl_boolean vertical)
1119 const int nx = cpl_image_get_size_x(
self);
1120 const int ny = cpl_image_get_size_y(
self);
1121 const int msize = 1 + 2 * hsize;
1123 cpl_image * background;
1124 cpl_error_code error = CPL_ERROR_NONE;
1126 cpl_ensure_code(
self != NULL, CPL_ERROR_NULL_INPUT);
1127 cpl_ensure_code(hsize >= 0, CPL_ERROR_ILLEGAL_INPUT);
1129 if (other == NULL) other =
self;
1131 mask = vertical ? cpl_mask_new(msize, 1) : cpl_mask_new(1, msize);
1133 error |= cpl_mask_not(mask);
1135 background = cpl_image_new(nx, ny, cpl_image_get_type(other));
1137 error |= cpl_image_filter_mask(background, other, mask, CPL_FILTER_MEDIAN,
1139 cpl_mask_delete(mask);
1141 if (
self != other) {
1142 error |= cpl_image_copy(
self, other, 1, 1);
1145 error |= cpl_image_subtract(
self, background);
1146 cpl_image_delete(background);
1148 return error ? cpl_error_set_where(cpl_func) : CPL_ERROR_NONE;
1178 static cpl_matrix * irplib_matrix_product_normal_create(
const cpl_matrix *
self)
1182 cpl_matrix * product;
1183 const double * ai = cpl_matrix_get_data_const(
self);
1185 const int m = cpl_matrix_get_nrow(
self);
1186 const int n = cpl_matrix_get_ncol(
self);
1190 cpl_ensure(
self != NULL, CPL_ERROR_NULL_INPUT, NULL);
1196 product = cpl_matrix_new(m, m);
1197 bwrite = cpl_matrix_get_data(product);
1199 bwrite = (
double *) cpl_malloc(m * m *
sizeof(
double));
1200 product = cpl_matrix_wrap(m, m, bwrite);
1204 for (i = 0; i < m; i++, bwrite += m, ai += n) {
1207 for (j = i; j < m; j++, aj += n) {
1209 for (k = 0; k < n; k++) {
1210 sum += ai[k] * aj[k];
1235 static cpl_error_code irplib_polynomial_fit_2d(cpl_polynomial *
self,
1236 const cpl_bivector * xy_pos,
1237 const cpl_vector * values,
1238 int degree,
double fixy,
1242 const int np = cpl_bivector_get_size(xy_pos);
1244 const int nc1 = 1+degree;
1247 const int nc = nc1 * (1 + nc1) / 2 - nc1;
1252 #ifdef IRPLIB_DISTORTION_ASSERT
1262 cpl_error_code error;
1265 cpl_ensure_code(
self != NULL, CPL_ERROR_NULL_INPUT);
1266 cpl_ensure_code(cpl_polynomial_get_dimension(
self) == 2,
1267 CPL_ERROR_INVALID_TYPE);
1268 cpl_ensure_code(np > 0, cpl_error_get_code());
1269 cpl_ensure_code(values != NULL, CPL_ERROR_NULL_INPUT);
1271 cpl_ensure_code(cpl_vector_get_size(values) == np,
1272 CPL_ERROR_INCOMPATIBLE_INPUT);
1274 cpl_ensure_code(degree > 0, CPL_ERROR_ILLEGAL_INPUT);
1275 cpl_ensure_code(np >= nc, CPL_ERROR_DATA_NOT_FOUND);
1278 yhat = cpl_vector_duplicate(cpl_bivector_get_y_const(xy_pos));
1279 cpl_vector_subtract_scalar(yhat, fixy);
1282 xhat = cpl_vector_duplicate(cpl_bivector_get_x_const(xy_pos));
1283 zhat = cpl_vector_duplicate(values);
1284 cpl_vector_subtract(zhat, xhat);
1289 dmv = (
double*)cpl_malloc(nc*np*
sizeof(
double));
1290 mv = cpl_matrix_wrap(nc, np, dmv);
1293 for (i=0; i < np; i++) {
1294 const double x = cpl_vector_get(xhat, i);
1295 const double y = cpl_vector_get(yhat, i);
1298 for (degy = 1; degy <= degree; degy++) {
1300 for (degx = 0; degx <= degree-degy; degx++, j++) {
1301 dmv[np * j + i] = xvalue * yvalue;
1308 cpl_vector_delete(xhat);
1309 cpl_vector_delete(yhat);
1312 mb = cpl_matrix_wrap(np, 1, cpl_vector_get_data(zhat));
1315 mx = cpl_matrix_product_create(mv, mb);
1317 cpl_matrix_unwrap(mb);
1318 cpl_vector_delete(zhat);
1321 mh = irplib_matrix_product_normal_create(mv);
1322 cpl_matrix_delete(mv);
1325 error = cpl_matrix_decomp_chol(mh) || cpl_matrix_solve_chol(mh, mx);
1327 cpl_matrix_delete(mh);
1330 cpl_matrix_delete(mx);
1331 cpl_ensure_code(0, error);
1336 #ifdef IRPLIB_DISTORTION_ASSERT
1341 for (degy = 1; degy <= degree; degy++) {
1343 for (degx = 0; degx <= degree-degy; degx++, j++) {
1346 cpl_polynomial_set_coeff(
self, powers, cpl_matrix_get(mx, j, 0));
1351 cpl_matrix_delete(mx);
1356 cpl_polynomial_set_coeff(
self, powers, 1.0);
1359 cpl_polynomial_shift_1d(
self, 1, -fixy);
1363 const cpl_vector * x_pos = cpl_bivector_get_x_const(xy_pos);
1364 const cpl_vector * y_pos = cpl_bivector_get_y_const(xy_pos);
1365 cpl_vector * x_val = cpl_vector_new(2);
1368 for (i=0; i<np; i++) {
1370 cpl_vector_set(x_val, 0, cpl_vector_get(x_pos, i));
1371 cpl_vector_set(x_val, 1, cpl_vector_get(y_pos, i));
1373 residue = cpl_vector_get(values, i)
1374 - cpl_polynomial_eval(
self, x_val);
1375 *mse += residue * residue;
1377 cpl_vector_delete(x_val);
1382 return CPL_ERROR_NONE;
double * irplib_flat_fit_slope_robust(double *x, double *y, int np)
Fit a slope to a list of points (robust fit).