00001 #include "mex.h"
00002 #include <stdio.h>
00003 #include <math.h>
00004 #include "mexhead.h"
00005 #include "Doc/hmi_patch_docstring.h"
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #define NARGIN_MIN 7
00049 #define NARGIN_MAX 7
00050 #define NARGOUT_MIN 2
00051 #define NARGOUT_MAX 4
00052
00053 #define ARG_y 0
00054 #define ARG_mag 1
00055 #define ARG_geom 2
00056 #define ARG_active 3
00057 #define ARG_ker 4
00058 #define ARG_kwt 5
00059 #define ARG_tau 6
00060
00061 #define ARG_bb 0
00062 #define ARG_stats 1
00063 #define ARG_yrgn 2
00064 #define ARG_crit 3
00065
00066 #define PROGNAME hmi_patch
00067 static const char *progname = "hmi_patch";
00068 static const char *in_specs[NARGIN_MAX] = {
00069 "RM",
00070 "RM",
00071 "RV(5)",
00072 "IS",
00073 "RV",
00074 "RV(3)",
00075 "RS(1)"
00076 };
00077 static const char *in_names[NARGIN_MAX] = {
00078 "y",
00079 "mag",
00080 "geom",
00081 "active",
00082 "ker",
00083 "kwt",
00084 "tau"
00085 };
00086 static const char *out_names[NARGOUT_MAX] = {
00087 "bb",
00088 "stats",
00089 "yrgn",
00090 "crit"};
00091
00092
00093 #define SHORTNAME Hpat
00094
00095
00096 #include "smoothsphere.h"
00097 #include "concomponent.h"
00098 #include "region_bb.h"
00099 #include "roi_stats_mag.h"
00100
00101
00102 #define MXT_ssp_NARGIN_USE (MXT_ssp_NARGIN_MAX-1) // bws unused
00103 #define MXT_ssp_NARGOUT_USE 1 // just the smoothed output
00104 #define MXT_ccp_NARGIN_USE MXT_ccp_NARGIN_MAX
00105 #define MXT_ccp_NARGOUT_USE MXT_ccp_NARGOUT_MAX
00106 #define MXT_rbb_NARGIN_USE 1 // coord unused
00107 #define MXT_rbb_NARGOUT_USE MXT_rbb_NARGOUT_MAX
00108 #define MXT_rsm_NARGIN_USE MXT_rsm_NARGIN_MAX
00109 #define MXT_rsm_NARGOUT_USE 1 // names, combo unused
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 static
00121 void
00122 patch_make_mask(double *yp, double *y, int N, double active)
00123 {
00124 int i;
00125
00126 for (i = 0; i < N; i++)
00127 if (y[i] == active)
00128 yp[i] = 1.0;
00129 else
00130 yp[i] = 0.0;
00131 }
00132
00133
00134
00135
00136 static
00137 void
00138 patch_threshold_mask(double *yp, double *x, int N, double tau)
00139 {
00140 int i;
00141
00142 for (i = 0; i < N; i++)
00143 if (x[i] > tau)
00144 yp[i] = 1.0;
00145 else
00146 yp[i] = 0.0;
00147 }
00148
00149
00150
00151
00152
00153 #ifdef StaticP
00154 StaticP
00155 #endif
00156 void
00157 mexFunction(
00158 int nlhs,
00159 mxArray *plhs[],
00160 int nrhs,
00161 const mxArray *prhsC[])
00162 {
00163 mxArray **prhs = (mxArray **) prhsC;
00164
00165 mxArray *prhs_ssp[MXT_ssp_NARGIN_USE];
00166 mxArray *plhs_ssp[MXT_ssp_NARGOUT_USE];
00167
00168 mxArray *prhs_ccp[MXT_ccp_NARGIN_USE];
00169 mxArray *plhs_ccp[MXT_ccp_NARGOUT_USE];
00170
00171 mxArray *prhs_rbb[MXT_rbb_NARGIN_USE];
00172 mxArray *plhs_rbb[MXT_rbb_NARGOUT_USE];
00173
00174 mxArray *prhs_rsm[MXT_rsm_NARGIN_USE];
00175 mxArray *plhs_rsm[MXT_rsm_NARGOUT_USE];
00176
00177 int M, N;
00178 int Nr;
00179 int do_stats;
00180 double *geom, *geomYX;
00181 char errstr[256];
00182 char *msg;
00183
00184
00185 if (nrhs < 0) {
00186 plhs[0] = mxt_PackSignature((mxt_Signature) (-nrhs),
00187 NARGIN_MIN, NARGIN_MAX,
00188 NARGOUT_MIN, NARGOUT_MAX,
00189 in_names, in_specs, out_names, docstring);
00190 return;
00191 }
00192
00193
00194
00195 if ((nrhs < NARGIN_MIN) || (nrhs > NARGIN_MAX))
00196 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00197 "%s: Expect %d <= input args <= %d",
00198 progname, NARGIN_MIN, NARGIN_MAX), errstr));
00199 if ((nlhs < NARGOUT_MIN) || (nlhs > NARGOUT_MAX))
00200 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00201 "%s: Expect %d <= output args <= %d",
00202 progname, NARGOUT_MIN, NARGOUT_MAX), errstr));
00203 mexargparse(nrhs, prhsC, in_names, in_specs, NULL, progname);
00204
00205
00206
00207
00208 M = mxGetM(prhs[ARG_y]);
00209 N = mxGetN(prhs[ARG_y]);
00210 do_stats = (mxGetNumberOfElements(prhs[ARG_mag]) > 0);
00211
00212
00213
00214
00215
00216 prhs_ssp[MXT_ssp_ARG_x] = mxCreateDoubleMatrix(M, N, mxREAL);
00217 patch_make_mask(mxGetPr(prhs_ssp[MXT_ssp_ARG_x]),
00218 mxGetPr(prhs[ARG_y]), M*N,
00219 mxGetScalar(prhs[ARG_active]));
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 prhs_ssp[MXT_ssp_ARG_geom] = mxDuplicateArray(prhs[ARG_geom]);
00230
00231 geom = mxGetPr(prhs[ARG_geom]);
00232 geomYX = mxGetPr(prhs_ssp[MXT_ssp_ARG_geom]);
00233 geomYX[0] = geom[1];
00234 geomYX[1] = geom[0];
00235
00236 prhs_ssp[MXT_ssp_ARG_k] = prhs[ARG_ker];
00237 prhs_ssp[MXT_ssp_ARG_kparam] = mxCreateDoubleMatrix(1,2,mxREAL);
00238
00239
00240 mxGetPr(prhs_ssp[MXT_ssp_ARG_kparam])[0] = 0.015;
00241 mxGetPr(prhs_ssp[MXT_ssp_ARG_kparam])[1] = rint((double) 50*4.0);
00242 prhs_ssp[MXT_ssp_ARG_kwt] = prhs[ARG_kwt];
00243
00244 msg = main_smoothsphere(MXT_ssp_NARGOUT_USE, plhs_ssp,
00245 MXT_ssp_NARGIN_USE, prhs_ssp);
00246 if (msg)
00247 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00248 "%s: Trouble in smoothsphere (%s)",
00249 progname, msg), errstr));
00250
00251
00252 mxDestroyArray(prhs_ssp[MXT_ssp_ARG_geom]);
00253 mxDestroyArray((mxArray *) prhs_ssp[MXT_ssp_ARG_kparam]);
00254
00255
00256
00257
00258
00259 prhs_ccp[MXT_ccp_ARG_x] = mxCreateDoubleMatrix(M, N, mxREAL);
00260 patch_threshold_mask(mxGetPr(prhs_ccp[MXT_ccp_ARG_x]),
00261 mxGetPr(plhs_ssp[MXT_ssp_ARG_y]), M*N,
00262 mxGetScalar(prhs[ARG_tau]));
00263
00264 if (nlhs <= ARG_crit)
00265 mxDestroyArray((mxArray *) plhs_ssp[MXT_ssp_ARG_y]);
00266
00267
00268
00269
00270 prhs_ccp[MXT_ccp_ARG_nbr] = mxCreateDoubleScalar(8.0);
00271 msg = main_concomponent(MXT_ccp_NARGOUT_USE, plhs_ccp,
00272 MXT_ccp_NARGIN_USE, prhs_ccp);
00273 if (msg)
00274 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00275 "%s: Trouble in smoothsphere (%s)",
00276 progname, msg), errstr));
00277
00278 mxDestroyArray((mxArray *) prhs_ccp[MXT_ccp_ARG_x]);
00279 mxDestroyArray((mxArray *) prhs_ccp[MXT_ccp_ARG_nbr]);
00280
00281
00282
00283 prhs_rbb[MXT_rbb_ARG_x] = plhs_ccp[MXT_ccp_ARG_y];
00284
00285
00286 msg = main_region_bb(MXT_rbb_NARGOUT_USE, plhs_rbb,
00287 MXT_rbb_NARGIN_USE, prhs_rbb);
00288 if (msg)
00289 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00290 "%s: Trouble in region_bb (%s)",
00291 progname, msg), errstr));
00292
00293
00294
00295
00296
00297
00298 if (do_stats) {
00299
00300
00301
00302 prhs_rsm[MXT_rsm_ARG_x ] = plhs_ccp[MXT_ccp_ARG_y];
00303 prhs_rsm[MXT_rsm_ARG_y ] = prhs_ssp[MXT_ssp_ARG_x];
00304 prhs_rsm[MXT_rsm_ARG_mag ] = prhs[ARG_mag];
00305 prhs_rsm[MXT_rsm_ARG_geom ] = prhs[ARG_geom];
00306 prhs_rsm[MXT_rsm_ARG_nroi ] = mxCreateDoubleScalar(-1.0);
00307 prhs_rsm[MXT_rsm_ARG_mode ] = mxCreateString("sesw");
00308
00309 msg = main_roi_stats_mag(MXT_rsm_NARGOUT_USE, plhs_rsm,
00310 MXT_rsm_NARGIN_USE, prhs_rsm);
00311 if (msg)
00312 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00313 "%s: Trouble in roi_stats_mag (%s)",
00314 progname, msg), errstr));
00315
00316 mxDestroyArray(prhs_rsm[MXT_rsm_ARG_nroi]);
00317 mxDestroyArray(prhs_rsm[MXT_rsm_ARG_mode]);
00318 } else {
00319
00320 plhs_rsm[MXT_rsm_ARG_s] = mxCreateDoubleMatrix(0, 0, mxREAL);
00321 }
00322
00323 mxDestroyArray((mxArray *) prhs_ssp[MXT_ssp_ARG_x]);
00324
00325
00326 plhs[ARG_bb ] = plhs_rbb[MXT_rbb_ARG_bb];
00327 plhs[ARG_stats] = plhs_rsm[MXT_rsm_ARG_s];
00328 plhs[ARG_yrgn ] = plhs_ccp[MXT_ccp_ARG_y];
00329 if (nlhs > ARG_crit)
00330 plhs[ARG_crit] = plhs_ssp[MXT_ssp_ARG_y];
00331 }
00332
00333
00334
00335
00336 #ifdef MEX2C_TAIL_HOOK
00337 #include "mex2c_tail.h"
00338 #endif
00339