00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <xsh_utils_wrappers.h>
00022 #include <xsh_error.h>
00023 #include <xsh_msg.h>
00024 #include <math.h>
00025
00026
00027
00028
00029 static cpl_image *
00030 xsh_image_filter_wrapper(const cpl_image *b, const cpl_matrix *k, cpl_filter_mode mode)
00031 {
00032 const double EPSILON = 1E-5;
00033 int nx = cpl_image_get_size_x(b);
00034 int ny = cpl_image_get_size_y(b);
00035 int nrow = cpl_matrix_get_nrow(k);
00036 int ncol = cpl_matrix_get_ncol(k);
00037 int i, j;
00038 cpl_type type = cpl_image_get_type(b);
00039 cpl_image * a = cpl_image_new(nx, ny, type);
00040
00041 cpl_mask* m = cpl_mask_new(ncol, nrow);
00042
00043
00044
00045 for (i = 0; i < ncol ; i++)
00046 {
00047 for (j = 0; j < nrow ; j++)
00048 {
00049 double value = cpl_matrix_get(k, j, i);
00050 if (fabs(value - 1.0) < EPSILON)
00051 {
00052 cpl_mask_set(m, i + 1, j + 1, CPL_BINARY_1);
00053 }
00054 }
00055 }
00056
00057 cpl_image_filter_mask(a, b, m, mode, CPL_BORDER_FILTER);
00058 cpl_mask_delete(m);
00059 return a;
00060 }
00061
00062 cpl_image*
00063 xsh_image_filter_mode(const cpl_image* b,
00064 const cpl_matrix * ker,
00065 cpl_filter_mode filter)
00066 {
00067 int nx = cpl_image_get_size_x(b);
00068 int ny = cpl_image_get_size_y(b);
00069 int type = cpl_image_get_type(b);
00070 cpl_image * a = cpl_image_new(nx, ny, type);
00071
00072 switch(filter) {
00073 case CPL_FILTER_MEDIAN:
00074 check(cpl_image_filter(a, b, ker, CPL_FILTER_MEDIAN, CPL_BORDER_FILTER));
00075 break;
00076 case CPL_FILTER_LINEAR:
00077 check(cpl_image_filter(a, b, ker, CPL_FILTER_LINEAR, CPL_BORDER_FILTER));
00078 break;
00079 case CPL_FILTER_STDEV:
00080 cpl_image_filter(a, b, ker, CPL_FILTER_STDEV, CPL_BORDER_FILTER);
00081 break;
00082 case CPL_FILTER_MORPHO:
00083 cpl_image_filter(a, b, ker, CPL_FILTER_MORPHO, CPL_BORDER_FILTER);
00084 break;
00085 default:
00086 xsh_msg_error("Filter type not supported");
00087 return NULL;
00088 }
00089 cleanup:
00090
00091 return a;
00092
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 cpl_polynomial * xsh_polynomial_fit_2d_create(cpl_bivector * xy_pos,
00122 cpl_vector * values,
00123 cpl_size * degree,
00124 double * mse)
00125 {
00126 typedef double* (*get_data)(cpl_bivector*);
00127 get_data data_extractor[2] = { &cpl_bivector_get_x_data, &cpl_bivector_get_y_data};
00128
00129
00130
00131 double rechisq = 0;
00132 int i, j;
00133 cpl_vector * fitresidual = 0;
00134 cpl_matrix * samppos2d = 0;
00135 cpl_polynomial * fit2d = cpl_polynomial_new(2);
00136 int xy_size = cpl_bivector_get_size(xy_pos);
00137
00138 samppos2d = cpl_matrix_new(2, xy_size);
00139 for (i = 0; i < 2; i++)
00140 {
00141 for (j = 0; j < xy_size; j++)
00142 {
00143 double value = data_extractor[i](xy_pos)[j];
00144 cpl_matrix_set(samppos2d, i, j, value);
00145 }
00146 }
00147
00148 cpl_polynomial_fit(fit2d, samppos2d, NULL, values, NULL, CPL_FALSE,
00149 NULL, degree);
00150
00151 fitresidual = cpl_vector_new(xy_size);
00152 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit2d,
00153 samppos2d, &rechisq);
00154 if (mse)
00155 {
00156 *mse = cpl_vector_product(fitresidual, fitresidual)
00157 / cpl_vector_get_size(fitresidual);
00158 }
00159 cpl_matrix_delete(samppos2d);
00160 cpl_vector_delete(fitresidual);
00161 return fit2d;
00162 }
00163
00164 cpl_polynomial *
00165 xsh_polynomial_fit_1d_create(
00166 const cpl_vector * x_pos,
00167 const cpl_vector * values,
00168 int degree,
00169 double * mse
00170 )
00171 {
00172 cpl_polynomial * fit1d = cpl_polynomial_new(1);
00173
00174
00175 int x_size = cpl_vector_get_size(x_pos);
00176 double rechisq = 0;
00177 cpl_size loc_deg=(cpl_size)degree;
00178 cpl_matrix * samppos = cpl_matrix_wrap(1, x_size,
00179 (double*)cpl_vector_get_data_const(x_pos));
00180 cpl_vector * fitresidual = cpl_vector_new(x_size);
00181
00182 cpl_polynomial_fit(fit1d, samppos, NULL, values, NULL,
00183 CPL_FALSE, NULL, &loc_deg);
00184 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
00185
00186 if ( x_size > (degree + 1) ) {
00187 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit1d,
00188 samppos, &rechisq);
00189 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
00190 }
00191 if (mse)
00192 {
00193 *mse = cpl_vector_product(fitresidual, fitresidual)
00194 / cpl_vector_get_size(fitresidual);
00195 }
00196 cpl_matrix_unwrap(samppos);
00197 cpl_vector_delete(fitresidual);
00198 return fit1d;
00199 }
00200
00201
00202 cpl_image *
00203 xsh_image_filter_median(const cpl_image * img, const cpl_matrix * mx)
00204 {
00205 return xsh_image_filter_wrapper(img, mx, CPL_FILTER_MEDIAN);
00206 }
00207
00208 cpl_image *
00209 xsh_image_filter_linear(const cpl_image *img, const cpl_matrix * mx)
00210 {
00211 return xsh_image_filter_mode(img, mx, CPL_FILTER_LINEAR);
00212
00213 }
00214
00215