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