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
71static cpl_apertures *irplib_distortion_detect_arcs(cpl_image *,
81irplib_distortion_fill_border(cpl_image *,
int,
int,
int,
int,
double);
83irplib_distortion_threshold1d(cpl_image *,
double, cpl_image *,
double);
84static cpl_error_code irplib_distortion_purge_arcs(cpl_apertures **,
91irplib_distortion_fill_arc_positions(cpl_bivector *,
95 const cpl_apertures *);
97static double irplib_distortion_get_row_centroid(
const cpl_image *,
102static int irplib_distortion_sub_hor_lowpass(cpl_image *,
int);
103static cpl_image *irplib_distortion_remove_ramp(
const cpl_image *);
105static cpl_error_code irplib_image_filter_background_line(cpl_image *,
110static cpl_error_code irplib_polynomial_fit_2d(cpl_polynomial *,
111 const cpl_bivector *,
117static cpl_matrix *irplib_matrix_product_normal_create(
const cpl_matrix *);
156irplib_distortion_estimate(
const cpl_image *org,
166 cpl_apertures **arcs)
170 cpl_image *label_image;
171 double rightmost, leftmost;
173 cpl_vector *values_to_fit;
175 cpl_polynomial *poly2d;
177 const int nx = cpl_image_get_size_x(org);
178 const int ny = cpl_image_get_size_y(org);
179 const int min_arc_range = (int)(nx / ARC_RANGE_FACT);
183 cpl_ensure(org != NULL, CPL_ERROR_NULL_INPUT, NULL);
184 cpl_ensure(kappa >= 0.0, CPL_ERROR_ILLEGAL_INPUT, NULL);
185 cpl_ensure(max_arc_width > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
190 filtered = cpl_image_new(nx, ny, cpl_image_get_type(org));
192 irplib_image_filter_background_line(filtered, org, max_arc_width, CPL_TRUE);
195 local_im = irplib_distortion_remove_ramp(filtered);
196 cpl_image_delete(filtered);
202 cpl_error_ensure(local_im != NULL, cpl_error_get_code(),
return (NULL),
203 "Cannot clean the image");
206 *arcs = irplib_distortion_detect_arcs(local_im, &label_image, arc_sat,
207 max_arc_width, kappa, xmin, ymin,
210 cpl_image_delete(local_im);
211 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
212 "Cannot detect the arcs");
215 n_arcs = cpl_apertures_get_size(*arcs);
216 cpl_msg_info(cpl_func,
"%d detected arcs", n_arcs);
219 rightmost = leftmost = cpl_apertures_get_pos_x(*arcs, 1);
220 for (i = 1; i < n_arcs; i++) {
221 if (cpl_apertures_get_pos_x(*arcs, i + 1) < leftmost)
222 leftmost = cpl_apertures_get_pos_x(*arcs, i + 1);
223 if (cpl_apertures_get_pos_x(*arcs, i + 1) > rightmost)
224 rightmost = cpl_apertures_get_pos_x(*arcs, i + 1);
226 if ((
int)(rightmost - leftmost) < min_arc_range) {
227#if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
228 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
229 "too narrow range (%g-%g)<%d", rightmost,
230 leftmost, min_arc_range);
232 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
235 cpl_apertures_delete(*arcs);
236 cpl_image_delete(local_im);
237 cpl_image_delete(label_image);
243 cpl_msg_info(cpl_func,
"Create deformation grid");
244 grid = cpl_bivector_new(n_arcs * ny);
245 values_to_fit = cpl_vector_new(n_arcs * ny);
247 if (irplib_distortion_fill_arc_positions(grid, values_to_fit, local_im,
248 label_image, *arcs)) {
249 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
250 "cannot get arcs positions");
251 cpl_apertures_delete(*arcs);
252 cpl_image_delete(local_im);
253 cpl_image_delete(label_image);
257 cpl_image_delete(label_image);
258 cpl_image_delete(local_im);
261 poly2d = cpl_polynomial_new(2);
262 if (irplib_polynomial_fit_2d(poly2d, grid, values_to_fit, degree,
263 0.5 * (ny + 1), &mse)) {
264 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
265 "cannot apply the 2d fit");
266 cpl_bivector_delete(grid);
267 cpl_vector_delete(values_to_fit);
268 cpl_apertures_delete(*arcs);
273 cpl_msg_info(cpl_func,
274 "Fitted a %d. degree 2D-polynomial to %" CPL_SIZE_FORMAT
276 "with mean-square error: %g",
277 degree, cpl_vector_get_size(values_to_fit), mse);
280 cpl_bivector_delete(grid);
281 cpl_vector_delete(values_to_fit);
304static cpl_apertures *
305irplib_distortion_detect_arcs(cpl_image *im,
306 cpl_image **label_im,
315 const int ny = cpl_image_get_size_y(im);
317 const int min_arclen = (int)(ny / ARC_MINARCLENFACT);
320 cpl_image *collapsed;
322 double threshold, fillval, median_val, sigma;
331 median_val = cpl_image_get_median_dev(im, &sigma);
332 fillval = median_val - sigma / 2.0;
333 if (irplib_distortion_fill_border(im, xmin, ymin, xmax, ymax, fillval)) {
334 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
335 "cannot fill bad zones");
340 filt_im = cpl_image_duplicate(im);
341 if (irplib_distortion_sub_hor_lowpass(filt_im, ARC_WINDOWSIZE) == -1) {
342 cpl_image_delete(filt_im);
347 median_val = cpl_image_get_median_dev(filt_im, &sigma);
350 if (median_val < TRESH_MEDIAN_MIN)
351 median_val = TRESH_MEDIAN_MIN;
352 if (sigma > TRESH_SIGMA_MAX)
353 sigma = TRESH_SIGMA_MAX;
356 threshold = median_val + sigma * kappa;
359 collapsed = cpl_image_collapse_median_create(filt_im, 0, 0, 0);
362 if (irplib_distortion_threshold1d(filt_im, median_val, collapsed, 0.0) ==
364 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
365 "cannot threshold the filtered image");
366 cpl_image_delete(filt_im);
367 cpl_image_delete(collapsed);
370 cpl_image_delete(collapsed);
373 bin_im = cpl_mask_threshold_image_create(filt_im, threshold, DBL_MAX);
374 cpl_image_delete(filt_im);
375 if (bin_im == NULL) {
376 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
377 "cannot binarise the image");
382 ngoodpix = cpl_mask_count(bin_im);
383 if (ngoodpix < ARC_MINGOODPIX) {
384#if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
385 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
386 "Too few (%d) white pixels", ngoodpix);
388 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
389 "Too few white pixels");
391 cpl_mask_delete(bin_im);
396 filter = cpl_mask_new(3, 3);
397 cpl_mask_not(filter);
398 cpl_mask_filter(bin_im, bin_im, filter, CPL_FILTER_OPENING,
400 cpl_mask_delete(filter);
403 *label_im = cpl_image_labelise_mask_create(bin_im, &nobj);
404 cpl_mask_delete(bin_im);
407 if ((det = cpl_apertures_new_from_image(im, *label_im)) == NULL) {
408 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
409 "Cannot compute arcs stats");
410 cpl_image_delete(*label_im);
416 if (irplib_distortion_purge_arcs(&det, *label_im, im, min_arclen,
417 max_arc_width, arc_sat)) {
418 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
419 "Cannot purge the arcs");
420 cpl_image_delete(*label_im);
422 cpl_apertures_delete(det);
425 if (cpl_apertures_get_size(det) < ARC_MINNBARCS) {
426#if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
427 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
428 "Not enough valid arcs (%" CPL_SIZE_FORMAT
430 cpl_apertures_get_size(det), ARC_MINNBARCS);
432 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
433 "Not enough valid arcs, min=" CPL_STRINGIFY(ARC_MINNBARCS));
435 cpl_image_delete(*label_im);
437 cpl_apertures_delete(det);
457irplib_distortion_fill_border(cpl_image *self,
464 const int nx = cpl_image_get_size_x(self);
465 const int ny = cpl_image_get_size_y(self);
466 float *pfi = cpl_image_get_data_float(self);
467 const float fvalue = (float)fillval;
471 cpl_ensure_code(pfi != NULL, cpl_error_get_code());
474 xmin = IRPLIB_MIN(xmin, nx + 1);
475 ymax = IRPLIB_MIN(ymax, ny);
478 xmax = IRPLIB_MAX(xmax, xmin - 1);
479 ymin = IRPLIB_MIN(ymin, ymax + 1);
483 for (j = 0; j < ymin - 1; j++) {
484 for (i = 0; i < nx; i++) {
485 pfi[i + j * nx] = fvalue;
490 for (; j < ymax; j++) {
491 for (i = 0; i < xmin - 1; i++) {
492 pfi[i + j * nx] = fvalue;
494 for (i = xmax; i < nx; i++) {
495 pfi[i + j * nx] = fvalue;
500 for (; j < ny; j++) {
501 for (i = 0; i < nx; i++) {
502 pfi[i + j * nx] = fvalue;
506 return CPL_ERROR_NONE;
510irplib_distortion_threshold1d(cpl_image *im,
525 if (cpl_image_get_type(im) != CPL_TYPE_FLOAT)
527 if (cpl_image_get_type(im1d) != CPL_TYPE_FLOAT)
531 pim = cpl_image_get_data_float(im);
532 pim1d = cpl_image_get_data_float(im1d);
533 nx = cpl_image_get_size_x(im);
534 ny = cpl_image_get_size_y(im);
537 for (i = 0; i < nx; i++)
538 if (pim1d[i] < threshold) {
539 for (j = 0; j < ny; j++)
540 pim[i + j * nx] = (
float)newval;
548irplib_distortion_sub_hor_lowpass(cpl_image *im,
int filt_size)
552 cpl_vector *avglinehi;
553 cpl_vector *avglinelo;
556 int lopos, hipos, nx, ny;
566 nx = cpl_image_get_size_x(im);
567 ny = cpl_image_get_size_y(im);
568 lopos = (int)(ny / 4);
569 hipos = (int)(3 * ny / 4);
572 if ((linehi = cpl_vector_new_from_image_row(im, hipos)) == NULL) {
575 if ((linelo = cpl_vector_new_from_image_row(im, lopos)) == NULL) {
576 cpl_vector_delete(linehi);
581 if ((avglinehi = cpl_vector_filter_median_create(linehi, filt_size)) ==
583 cpl_vector_delete(linehi);
584 cpl_vector_delete(linelo);
587 cpl_vector_delete(linehi);
589 if ((avglinelo = cpl_vector_filter_median_create(linelo, filt_size)) ==
591 cpl_vector_delete(linelo);
592 cpl_vector_delete(avglinehi);
595 cpl_vector_delete(linelo);
598 cpl_vector_add(avglinehi, avglinelo);
599 cpl_vector_delete(avglinelo);
600 cpl_vector_divide_scalar(avglinehi, 2.0);
603 pavglinehi = cpl_vector_get_data(avglinehi);
604 pim = cpl_image_get_data_float(im);
605 for (i = 0; i < nx; i++) {
606 for (j = 0; j < ny; j++) {
607 pim[i + j * nx] -= pavglinehi[i];
610 cpl_vector_delete(avglinehi);
628irplib_distortion_purge_arcs(cpl_apertures **self,
630 const cpl_image *arc_im,
635 const double ycenter = 0.5 * (1 + cpl_image_get_size_y(arc_im));
643 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
646 narcs = cpl_apertures_get_size(*self);
648 cpl_ensure_code(narcs > 0, CPL_ERROR_DATA_NOT_FOUND);
649 cpl_ensure_code(cpl_image_get_type(lab_im) == CPL_TYPE_INT,
650 CPL_ERROR_ILLEGAL_INPUT);
653 relabel = cpl_calloc(narcs,
sizeof(
int));
656 for (i = 0; i < narcs; i++) {
658 const int arclen = 1 + cpl_apertures_get_top(*self, i + 1) -
659 cpl_apertures_get_bottom(*self, i + 1);
661 if (cpl_apertures_get_top(*self, i + 1) < ycenter)
663 if (cpl_apertures_get_bottom(*self, i + 1) > ycenter)
666 if (arclen > min_arclen) {
667 const int arcwidth = 1 + cpl_apertures_get_right(*self, i + 1) -
668 cpl_apertures_get_left(*self, i + 1);
669 if (arcwidth < max_arcwidth) {
670 const int edge = cpl_apertures_get_left_y(*self, i + 1);
672 const double mean = cpl_apertures_get_mean(*self, i + 1);
673 if (mean < arc_sat) {
674 relabel[i] = ++nkeep;
686 int *plabim = cpl_image_get_data_int(lab_im);
688 cpl_image_get_size_x(lab_im) * cpl_image_get_size_y(lab_im);
692#if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
693 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
694 "All %d arc(s) are invalid", narcs);
696 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
697 "All arcs are invalid");
701 for (i = 0; i < npix; i++) {
702 const int label = plabim[i];
704 if (label < 0 || label > narcs)
707 plabim[i] = relabel[label - 1];
713 return cpl_error_set(cpl_func, plabim[i] < 0
714 ? CPL_ERROR_ILLEGAL_INPUT
715 : CPL_ERROR_INCOMPATIBLE_INPUT);
719 cpl_apertures_delete(*self);
720 *self = cpl_apertures_new_from_image(arc_im, lab_im);
725 cpl_msg_info(cpl_func,
"Purged %d of %d arcs (1st purged=%d)",
726 narcs - nkeep, narcs, ifirst);
729 cpl_ensure_code(*self != NULL, cpl_error_get_code());
731 return CPL_ERROR_NONE;
751irplib_distortion_fill_arc_positions(cpl_bivector *grid,
752 cpl_vector *fitvalues,
754 const cpl_image *label_im,
755 const cpl_apertures *det)
757 const int narcs = cpl_apertures_get_size(det);
758 int nfitvals = cpl_vector_get_size(fitvalues);
759 const int nx = cpl_image_get_size_x(label_im);
760 const int ny = cpl_image_get_size_y(label_im);
763 cpl_vector *gridx = cpl_bivector_get_x(grid);
764 cpl_vector *gridy = cpl_bivector_get_y(grid);
765 cpl_polynomial *dist1d;
766 cpl_matrix *dist1dx = NULL;
767 cpl_vector *dist1dy = NULL;
774 cpl_ensure_code(nfitvals > 0, CPL_ERROR_DATA_NOT_FOUND);
775 cpl_ensure_code(narcs > 0, CPL_ERROR_DATA_NOT_FOUND);
776 cpl_ensure_code(cpl_image_get_type(label_im) == CPL_TYPE_INT,
777 CPL_ERROR_TYPE_MISMATCH);
780 if (nfitvals < narcs * ny) {
781 nfitvals = narcs * ny;
782 cpl_vector_set_size(fitvalues, nfitvals);
784 if (cpl_vector_get_size(gridx) < nfitvals ||
785 cpl_vector_get_size(gridy) < nfitvals) {
786 cpl_vector_set_size(gridx, nfitvals);
787 cpl_vector_set_size(gridy, nfitvals);
791 dgridx = cpl_vector_get_data(gridx);
792 dgridy = cpl_vector_get_data(gridy);
793 dfitv = cpl_vector_get_data(fitvalues);
796 kernel = cpl_mask_new(3, 3);
797 cpl_mask_not(kernel);
798 filt_img = cpl_image_new(nx, ny, cpl_image_get_type(in));
799 cpl_image_filter_mask(filt_img, in, kernel, CPL_FILTER_MEDIAN,
801 cpl_mask_delete(kernel);
803 dist1d = cpl_polynomial_new(1);
805 for (obj = 0; obj < narcs; obj++) {
807 const int *plabel_im = cpl_image_get_data_int_const(label_im);
808 const int ndist1d = cpl_apertures_get_top(det, obj + 1) -
809 cpl_apertures_get_bottom(det, obj + 1) + 1;
810 cpl_boolean sampsym = CPL_TRUE;
814 (void)cpl_matrix_unwrap(dist1dx);
815 (void)cpl_vector_unwrap(dist1dy);
816 dist1dx = cpl_matrix_wrap(1, ndist1d, dgridy + ndone);
817 dist1dy = cpl_vector_wrap(ndist1d, dfitv + ndone);
821 for (j = cpl_apertures_get_bottom(det, obj + 1) - 1;
822 j < cpl_apertures_get_top(det, obj + 1); j++) {
823 for (i = 0; i < nx; i++) {
824 if (plabel_im[i + j * nx] == obj + 1)
829 cpl_errorstate prestate = cpl_errorstate_get();
831 const double x_finepos =
832 irplib_distortion_get_row_centroid(filt_img, label_im, i,
834 if (!cpl_errorstate_is_equal(prestate)) {
835 irplib_error_recover(prestate,
836 "Could not find X-position "
837 "for line %d at y=%d (x=%d)",
838 obj + 1, j + 1, i + 1);
840 else if (x_finepos >= 0.0) {
841 cpl_matrix_set(dist1dx, 0, k, 1.0 + j);
842 cpl_vector_set(dist1dy, k, 1.0 + x_finepos);
843 if (k > 0 && j != 1 + prevj)
851 double ref_xpos, grad;
852 cpl_error_code error;
853 const cpl_boolean did_drop = k != ndist1d;
854 const cpl_size mindeg = 0;
855 const cpl_size maxdeg = 2;
859 dist1dx = cpl_matrix_wrap(1, k, cpl_matrix_unwrap(dist1dx));
860 dist1dy = cpl_vector_wrap(k, cpl_vector_unwrap(dist1dy));
863 error = cpl_polynomial_fit(dist1d, dist1dx, &sampsym, dist1dy, NULL,
864 CPL_FALSE, &mindeg, &maxdeg);
866 cpl_msg_error(cpl_func,
"1D-fit failed");
870 ref_xpos = cpl_polynomial_eval_1d(dist1d, 0.5 * (ny + 1), &grad);
872 for (j = cpl_apertures_get_bottom(det, obj + 1) - 1;
873 j < cpl_apertures_get_top(det, obj + 1); j++) {
875 cpl_polynomial_eval_1d(dist1d, j + 1.0, NULL);
878 dgridx[ndone] = ref_xpos;
882 dgridy[ndone] = 1.0 + j;
885 cpl_msg_info(cpl_func,
"Line %d has center gradient %g", obj + 1,
890 cpl_image_delete(filt_img);
891 cpl_polynomial_delete(dist1d);
892 (void)cpl_matrix_unwrap(dist1dx);
893 (void)cpl_vector_unwrap(dist1dy);
895 cpl_msg_info(cpl_func,
896 "Found %d fitting points ("
897 "expected up to %d points)",
900 cpl_ensure_code(obj == narcs, cpl_error_get_code());
902 cpl_ensure_code(ndone > 0, CPL_ERROR_DATA_NOT_FOUND);
904 cpl_vector_set_size(fitvalues, ndone);
905 cpl_vector_set_size(gridx, ndone);
906 cpl_vector_set_size(gridy, ndone);
908 return CPL_ERROR_NONE;
923irplib_distortion_get_row_centroid(
const cpl_image *im,
924 const cpl_image *label_im,
928 const int nx = cpl_image_get_size_x(im);
929 const int ny = cpl_image_get_size_y(im);
930 const int ynx = y * nx;
931 const float *pim = cpl_image_get_data_float_const(im);
932 const int *plabel_im = cpl_image_get_data_int_const(label_im);
941 cpl_ensure(pim != NULL, cpl_error_get_code(), -1.0);
942 cpl_ensure(plabel_im != NULL, cpl_error_get_code(), -2.0);
943 cpl_ensure(x >= 0, CPL_ERROR_ILLEGAL_INPUT, -3.0);
944 cpl_ensure(y >= 0, CPL_ERROR_ILLEGAL_INPUT, -4.0);
945 cpl_ensure(x < nx, CPL_ERROR_ILLEGAL_INPUT, -5.0);
946 cpl_ensure(y < ny, CPL_ERROR_ILLEGAL_INPUT, -6.0);
948 max = (double)pim[x + ynx];
949 objnum = plabel_im[x + ynx];
953 const double val = (double)pim[x + ynx];
973 }
while (x < nx && objnum == plabel_im[x + ynx]);
975 cpl_ensure(sum > 0.0, CPL_ERROR_DATA_NOT_FOUND, -7.0);
983 return (wsum < sum * firstpos || wsum > sum * lastpos) ? maxpos
994#define IS_NB_TESTPOINTS 8
995#define IS_MIN_SLOPE 0.01
996#define IS_MAX_SLOPE_DIF 0.075
997#define IS_MAX_FIT_EDGE_DIF 0.05
998#define IS_MIN_RAMP 10.0
999#define IS_MAX_MNERR 13.0
1000#define IS_MAX_MNERR_DIF 8.0
1001#define IS_MAX_INTER_DIF 20.0
1002#define IS_SKIPZONE 2.5
1003#define SQR(x) ((x) * (x))
1005irplib_distortion_remove_ramp(
const cpl_image *in)
1008 const int nx = cpl_image_get_size_x(in);
1009 const int ny = cpl_image_get_size_y(in);
1010 const int yhi = (int)(ny / 2);
1011 const int ylo = yhi - 1;
1012 cpl_bivector *testpointlo;
1013 double *testpointlo_x;
1014 double *testpointlo_y;
1015 cpl_bivector *testpointhi;
1016 double *testpointhi_x;
1017 double *testpointhi_y;
1018 const int spacing = ny / (IS_SKIPZONE * IS_NB_TESTPOINTS);
1019 double rampdif, fitslope;
1020 double *pol_coefhi, *pol_coeflo;
1022 double *median_data;
1023 double medianerrlo, medianerrhi;
1029 cpl_ensure(cpl_image_get_type(in) == CPL_TYPE_FLOAT,
1030 CPL_ERROR_UNSUPPORTED_MODE, NULL);
1032 if (ny < IS_SKIPZONE * IS_NB_TESTPOINTS) {
1033#if defined CPL_HAVE_VA_ARGS && CPL_HAVE_VA_ARGS != 0
1034 cpl_error_set_message(
1035 cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1036 "image has %d lines, min=" CPL_STRINGIFY(IS_SKIPZONE)
"*" CPL_STRINGIFY(IS_NB_TESTPOINTS), ny);
1038 cpl_error_set_message(
1039 cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1040 "image has too few lines, min=" CPL_STRINGIFY(IS_SKIPZONE)
"*" CPL_STRINGIFY(IS_NB_TESTPOINTS));
1046 testpointhi = cpl_bivector_new(IS_NB_TESTPOINTS);
1047 testpointhi_x = cpl_bivector_get_x_data(testpointhi);
1048 testpointhi_y = cpl_bivector_get_y_data(testpointhi);
1049 testpointlo = cpl_bivector_new(IS_NB_TESTPOINTS);
1050 testpointlo_x = cpl_bivector_get_x_data(testpointlo);
1051 testpointlo_y = cpl_bivector_get_y_data(testpointlo);
1052 for (i = 0; i < IS_NB_TESTPOINTS; i++) {
1054 cpl_vector *tmp_vector;
1055 y = yhi + i * spacing;
1056 tmp_vector = cpl_vector_new_from_image_row(in, y + 1);
1057 testpointhi_x[i] = y - ny / 2;
1058 testpointhi_y[i] = cpl_vector_get_median_const(tmp_vector);
1059 cpl_vector_delete(tmp_vector);
1060 y = ylo - i * spacing;
1061 tmp_vector = cpl_vector_new_from_image_row(in, y + 1);
1062 testpointlo_x[IS_NB_TESTPOINTS - i - 1] = y;
1063 testpointlo_y[IS_NB_TESTPOINTS - i - 1] =
1064 cpl_vector_get_median_const(tmp_vector);
1065 cpl_vector_delete(tmp_vector);
1075 median = cpl_vector_new(IS_NB_TESTPOINTS);
1076 median_data = cpl_vector_get_data(median);
1077 for (i = 0; i < IS_NB_TESTPOINTS; i++) {
1078 median_data[i] = SQR(testpointhi_y[i] - pol_coefhi[0] -
1079 pol_coefhi[1] * testpointhi_x[i]);
1081 medianerrhi = cpl_vector_get_median(median);
1082 for (i = 0; i < IS_NB_TESTPOINTS; i++) {
1083 median_data[i] = SQR(testpointlo_y[i] - pol_coeflo[0] -
1084 pol_coeflo[1] * testpointlo_x[i]);
1086 medianerrlo = cpl_vector_get_median(median);
1087 cpl_vector_delete(median);
1088 rampdif = testpointlo_y[IS_NB_TESTPOINTS - 1] - testpointhi_y[0];
1089 slope = rampdif / (ny / 2.0);
1090 fitslope = (pol_coefhi[1] + pol_coeflo[1]) / 2.0;
1092 cpl_bivector_delete(testpointlo);
1093 cpl_bivector_delete(testpointhi);
1096 if (fabs(rampdif) < IS_MIN_RAMP || fabs(pol_coefhi[1]) < IS_MIN_SLOPE ||
1097 fabs(pol_coeflo[1]) < IS_MIN_SLOPE ||
1098 pol_coefhi[1] / pol_coeflo[1] < 0.5 ||
1099 pol_coefhi[1] / pol_coeflo[1] > 2.0 ||
1100 fabs(pol_coefhi[1] - pol_coeflo[1]) > IS_MAX_SLOPE_DIF ||
1101 fabs(pol_coefhi[0] - pol_coeflo[0]) > IS_MAX_INTER_DIF ||
1102 medianerrlo > IS_MAX_MNERR || medianerrhi > IS_MAX_MNERR ||
1103 fabs(medianerrlo - medianerrhi) > IS_MAX_MNERR_DIF ||
1104 fabs(slope - fitslope) > IS_MAX_FIT_EDGE_DIF ||
1105 slope / fitslope < 0.5 || slope / fitslope > 2.0)
1110 cpl_free(pol_coeflo);
1111 cpl_free(pol_coefhi);
1114 out = cpl_image_duplicate(in);
1115 pout = cpl_image_get_data_float(out);
1116 if (ramp_present == 1) {
1119 for (j = 0; j < ny / 2; j++) {
1120 val = slope * (j - ny / 2);
1121 for (i = 0; i < nx; i++)
1122 pout[i + j * nx] -= val;
1124 for (j = ny / 2; j < ny; j++) {
1125 val = slope * (j - ny);
1126 for (i = 0; i < nx; i++)
1127 pout[i + j * nx] -= val;
1149static cpl_error_code
1150irplib_image_filter_background_line(cpl_image *self,
1151 const cpl_image *other,
1153 cpl_boolean vertical)
1155 const int nx = cpl_image_get_size_x(self);
1156 const int ny = cpl_image_get_size_y(self);
1157 const int msize = 1 + 2 * hsize;
1159 cpl_image *background;
1160 cpl_error_code error = CPL_ERROR_NONE;
1162 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
1163 cpl_ensure_code(hsize >= 0, CPL_ERROR_ILLEGAL_INPUT);
1168 mask = vertical ? cpl_mask_new(msize, 1) : cpl_mask_new(1, msize);
1170 error |= cpl_mask_not(mask);
1172 background = cpl_image_new(nx, ny, cpl_image_get_type(other));
1174 error |= cpl_image_filter_mask(background, other, mask, CPL_FILTER_MEDIAN,
1176 cpl_mask_delete(mask);
1178 if (self != other) {
1179 error |= cpl_image_copy(self, other, 1, 1);
1182 error |= cpl_image_subtract(self, background);
1183 cpl_image_delete(background);
1185 return error ? cpl_error_set_where(cpl_func) : CPL_ERROR_NONE;
1215irplib_matrix_product_normal_create(
const cpl_matrix *self)
1218 cpl_matrix *product;
1219 const double *ai = cpl_matrix_get_data_const(self);
1221 const int m = cpl_matrix_get_nrow(self);
1222 const int n = cpl_matrix_get_ncol(self);
1226 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
1232 product = cpl_matrix_new(m, m);
1233 bwrite = cpl_matrix_get_data(product);
1235 bwrite = (
double *)cpl_malloc(m * m *
sizeof(
double));
1236 product = cpl_matrix_wrap(m, m, bwrite);
1240 for (i = 0; i < m; i++, bwrite += m, ai += n) {
1243 for (j = i; j < m; j++, aj += n) {
1245 for (k = 0; k < n; k++) {
1246 sum += ai[k] * aj[k];
1270static cpl_error_code
1271irplib_polynomial_fit_2d(cpl_polynomial *self,
1272 const cpl_bivector *xy_pos,
1273 const cpl_vector *values,
1278 const int np = cpl_bivector_get_size(xy_pos);
1280 const int nc1 = 1 + degree;
1283 const int nc = nc1 * (1 + nc1) / 2 - nc1;
1288#ifdef IRPLIB_DISTORTION_ASSERT
1298 cpl_error_code error;
1301 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
1302 cpl_ensure_code(cpl_polynomial_get_dimension(self) == 2,
1303 CPL_ERROR_INVALID_TYPE);
1304 cpl_ensure_code(np > 0, cpl_error_get_code());
1305 cpl_ensure_code(values != NULL, CPL_ERROR_NULL_INPUT);
1307 cpl_ensure_code(cpl_vector_get_size(values) == np,
1308 CPL_ERROR_INCOMPATIBLE_INPUT);
1310 cpl_ensure_code(degree > 0, CPL_ERROR_ILLEGAL_INPUT);
1311 cpl_ensure_code(np >= nc, CPL_ERROR_DATA_NOT_FOUND);
1314 yhat = cpl_vector_duplicate(cpl_bivector_get_y_const(xy_pos));
1315 cpl_vector_subtract_scalar(yhat, fixy);
1318 xhat = cpl_vector_duplicate(cpl_bivector_get_x_const(xy_pos));
1319 zhat = cpl_vector_duplicate(values);
1320 cpl_vector_subtract(zhat, xhat);
1325 dmv = (
double *)cpl_malloc(nc * np *
sizeof(
double));
1326 mv = cpl_matrix_wrap(nc, np, dmv);
1329 for (i = 0; i < np; i++) {
1330 const double x = cpl_vector_get(xhat, i);
1331 const double y = cpl_vector_get(yhat, i);
1334 for (degy = 1; degy <= degree; degy++) {
1336 for (degx = 0; degx <= degree - degy; degx++, j++) {
1337 dmv[np * j + i] = xvalue * yvalue;
1344 cpl_vector_delete(xhat);
1345 cpl_vector_delete(yhat);
1348 mb = cpl_matrix_wrap(np, 1, cpl_vector_get_data(zhat));
1351 mx = cpl_matrix_product_create(mv, mb);
1353 cpl_matrix_unwrap(mb);
1354 cpl_vector_delete(zhat);
1357 mh = irplib_matrix_product_normal_create(mv);
1358 cpl_matrix_delete(mv);
1361 error = cpl_matrix_decomp_chol(mh) || cpl_matrix_solve_chol(mh, mx);
1363 cpl_matrix_delete(mh);
1366 cpl_matrix_delete(mx);
1367 cpl_ensure_code(0, error);
1372#ifdef IRPLIB_DISTORTION_ASSERT
1377 for (degy = 1; degy <= degree; degy++) {
1379 for (degx = 0; degx <= degree - degy; degx++, j++) {
1382 cpl_polynomial_set_coeff(self, powers, cpl_matrix_get(mx, j, 0));
1387 cpl_matrix_delete(mx);
1392 cpl_polynomial_set_coeff(self, powers, 1.0);
1395 cpl_polynomial_shift_1d(self, 1, -fixy);
1399 const cpl_vector *x_pos = cpl_bivector_get_x_const(xy_pos);
1400 const cpl_vector *y_pos = cpl_bivector_get_y_const(xy_pos);
1401 cpl_vector *x_val = cpl_vector_new(2);
1404 for (i = 0; i < np; i++) {
1406 cpl_vector_set(x_val, 0, cpl_vector_get(x_pos, i));
1407 cpl_vector_set(x_val, 1, cpl_vector_get(y_pos, i));
1410 cpl_vector_get(values, i) - cpl_polynomial_eval(self, x_val);
1411 *mse += residue * residue;
1413 cpl_vector_delete(x_val);
1418 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).