00001 #include "mex.h"
00002 #include <stdio.h>
00003 #include <math.h>
00004 #include <string.h>
00005 #include <limits.h>
00006 #include "mexhead.h"
00007 #include "Doc/cstat_docstring.h"
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
00049
00050 #define NARGIN_MIN 2
00051 #define NARGIN_MAX 4
00052 #define NARGOUT_MIN 0
00053 #define NARGOUT_MAX 3
00054
00055 #define ARG_IMAGE 0
00056 #define ARG_LABEL 1
00057 #define ARG_K 2
00058 #define ARG_MODE 3
00059
00060 #define ARG_MU 0
00061 #define ARG_SIGMA 1
00062 #define ARG_CT 2
00063
00064 static const char *progname = "cstat";
00065 #define PROGNAME cstat
00066 static const char *in_specs[NARGIN_MAX] = {
00067 "RM",
00068 "IM",
00069 "IS",
00070 "SV"};
00071 static const char *in_names[NARGIN_MAX] = {
00072 "image",
00073 "label",
00074 "K",
00075 "mode"};
00076 static const char *out_names[NARGOUT_MAX] = {
00077 "mu",
00078 "sigma",
00079 "ct"};
00080
00081
00082
00083
00084
00085
00086 static
00087 char *
00088 extract_mode(char *mode, int *Munscaled, int *Mnomu, int *Mnosigma)
00089 {
00090 char *word;
00091 char *sep = ", \t";
00092
00093 if (!mode) return "null";
00094 for (word = strtok(mode, sep); word; word = strtok(NULL, sep)) {
00095
00096 if (strcasecmp(word, "unscaled") == 0) { *Munscaled = *Mnosigma = 1; }
00097
00098 else if (strcasecmp(word, "no-mu" ) == 0) { *Mnomu = *Mnosigma = 1;}
00099 else if (strcasecmp(word, "no-sigma") == 0) { *Mnosigma = 1; }
00100
00101 else return word;
00102 }
00103 return 0;
00104 }
00105
00106
00107
00108 static
00109 void
00110 computeminmax(double *input, mwSize n, double *minP, double *maxP)
00111 {
00112 mwSize i;
00113 double min, max;
00114
00115
00116 min = max = mxt_getnand();
00117
00118 for (i = 0; i < n && isnan(min); i++)
00119 min = max = input[i];
00120
00121 for (; i < n; i++) {
00122
00123 if (input[i] < min) min = input[i];
00124 if (input[i] > max) max = input[i];
00125 }
00126 *minP = min;
00127 *maxP = max;
00128 }
00129
00130
00131 #define CLASS2INDEX(x) (((int) floor(x)) - 1)
00132
00133
00134
00135
00136
00137
00138 static
00139 void
00140 computesize(double *image, double *class, const mwSize n,
00141 double *size, const int nclasses)
00142 {
00143 mwSize i;
00144 int index;
00145
00146
00147 for (index = 0; index < nclasses; index++)
00148 size[index] = 0;
00149
00150 for (i = 0; i < n; i++) {
00151
00152 if (isnan(class[i])) continue;
00153 if (image && isnan(image[i])) continue;
00154
00155 index = CLASS2INDEX(class[i]);
00156 if (index >= 0 && index < nclasses)
00157 size[index]++;
00158 }
00159 }
00160
00161
00162
00163
00164
00165
00166 static
00167 void
00168 computemean(double *image,
00169 double *class,
00170 mwSize n,
00171 double *size,
00172 double *mean,
00173 int nclasses,
00174 int unscaled)
00175 {
00176 mwSize i;
00177 int index;
00178
00179
00180 for (index = 0; index < nclasses; index++) {
00181 size[index] = 0;
00182 if (image)
00183 mean[index] = 0;
00184 }
00185
00186 for (i = 0; i < n; i++) {
00187
00188 if (isnan(class[i])) continue;
00189 if (image && isnan(image[i])) continue;
00190
00191 index = CLASS2INDEX(class[i]);
00192 if (index >= 0 && index < nclasses) {
00193 size[index] += 1.0;
00194 if (image)
00195 mean[index] += image[i];
00196 }
00197 }
00198
00199 if (image && !unscaled)
00200 for (index = 0; index < nclasses; index++)
00201 mean[index] /= size[index];
00202 }
00203
00204
00205
00206
00207
00208
00209
00210 static
00211 void
00212 computestd(double *image,
00213 double *class,
00214 mwSize n,
00215 double *mean,
00216 double *size,
00217 double *std,
00218 int nclasses)
00219 {
00220 mwSize i;
00221 int index;
00222
00223
00224 for (index = 0; index < nclasses; index++)
00225 std[index] = 0;
00226
00227 for (i = 0; i < n; i++) {
00228
00229 if (isnan(class[i]) || isnan(image[i])) continue;
00230
00231 index = CLASS2INDEX(class[i]);
00232 if (index >= 0 && index < nclasses) {
00233 std[index] += (image[i] - mean[index]) * (image[i] - mean[index]);
00234 }
00235 }
00236
00237 for (index = 0; index < nclasses; index++) {
00238 if (size[index] <= 1)
00239 std[index] = mxt_getnand();
00240 else
00241 std[index] = sqrt(std[index]/(size[index]-1));
00242 }
00243 }
00244
00245
00246
00247 #ifdef StaticP
00248 StaticP
00249 #endif
00250 void
00251 mexFunction(int nlhs,
00252 mxArray *plhs[],
00253 int nrhs,
00254 const mxArray *prhs[])
00255 {
00256 int nclasses;
00257 mwSize npix;
00258 double junk, nclasses_dbl;
00259 char *mode, *word;
00260 int empty_image;
00261 int mode_unscaled, mode_nomu, mode_nosigma;
00262 mxArray *arg_ct, *arg_mu, *arg_sigma;
00263 char errstr[256];
00264
00265
00266 if (nrhs < 0) {
00267 plhs[0] = mxt_PackSignature((mxt_Signature) (-nrhs),
00268 NARGIN_MIN, NARGIN_MAX,
00269 NARGOUT_MIN, NARGOUT_MAX,
00270 in_names, in_specs, out_names, docstring);
00271 return;
00272 }
00273
00274 if ((nrhs < NARGIN_MIN) || (nrhs > NARGIN_MAX))
00275 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00276 "%s: Expect %d <= input args <= %d",
00277 progname, NARGIN_MIN, NARGIN_MAX), errstr));
00278 if ((nlhs < NARGOUT_MIN) || (nlhs > NARGOUT_MAX))
00279 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00280 "%s: Expect %d <= output args <= %d",
00281 progname, NARGOUT_MIN, NARGOUT_MAX), errstr));
00282 mexargparse(nrhs, prhs, in_names, in_specs, NULL, progname);
00283 start_sizechecking();
00284 if (mxGetNumberOfElements(prhs[ARG_IMAGE]) > 0) {
00285
00286 sizeinit(prhs[ARG_IMAGE]);
00287 sizeagree(prhs[ARG_LABEL]);
00288 sizecheck_msg(progname, in_names, ARG_IMAGE);
00289 }
00290
00291 npix = mxGetNumberOfElements(prhs[ARG_LABEL]);
00292
00293
00294 if (nrhs <= ARG_K || mxGetNumberOfElements(prhs[ARG_K]) == 0) {
00295
00296 computeminmax(mxGetPr(prhs[ARG_LABEL]), npix, &junk, &nclasses_dbl);
00297 } else {
00298 nclasses_dbl = mxGetScalar(prhs[ARG_K]);
00299 }
00300
00301 nclasses = (int) nclasses_dbl;
00302 if (isnan(nclasses_dbl) || (nclasses_dbl != (double) nclasses) || (nclasses < 0))
00303 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00304 "%s: bad supplied/inferred K: %g",
00305 progname, nclasses_dbl), errstr));
00306
00307
00308
00309
00310 empty_image = (mxGetNumberOfElements(prhs[ARG_IMAGE]) == 0);
00311 mode_unscaled = 0;
00312 mode_nomu = empty_image;
00313 mode_nosigma = empty_image;
00314 if (nrhs > ARG_MODE) {
00315
00316 if ((mode = mxArrayToString(prhs[ARG_MODE])) == NULL)
00317 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00318 "%s: bad mode (non-string?). "
00319 "Could not convert mode arg to string.",
00320 progname), errstr));
00321 if ((word = extract_mode(mode, &mode_unscaled, &mode_nomu, &mode_nosigma)) != NULL)
00322 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00323 "%s: bad word <%s> in mode <%.80s>",
00324 progname, word,
00325 mxArrayToString(prhs[ARG_MODE])), errstr));
00326 mxFree(mode);
00327 }
00328
00329
00330
00331 if (mode_unscaled || mode_nomu)
00332 mode_nosigma = 1;
00333
00334
00335 arg_ct = mxCreateDoubleMatrix(1, nclasses, mxREAL);
00336 if (mode_nomu)
00337 arg_mu = mxCreateDoubleMatrix(0, 0, mxREAL);
00338 else
00339 arg_mu = mxCreateDoubleMatrix(1, nclasses, mxREAL);
00340 if (mode_nosigma)
00341 arg_sigma = mxCreateDoubleMatrix(0, 0, mxREAL);
00342 else
00343 arg_sigma = mxCreateDoubleMatrix(1, nclasses, mxREAL);
00344 if (!arg_ct || !arg_mu || !arg_sigma)
00345 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00346 "%s: Could not allocate space for results",
00347 progname), errstr));
00348
00349
00350 computemean(mode_nomu ? NULL : mxGetPr(prhs[ARG_IMAGE]),
00351 mxGetPr(prhs[ARG_LABEL]),
00352 npix,
00353 mxGetPr(arg_ct),
00354 mxGetPr(arg_mu),
00355 nclasses,
00356 mode_unscaled);
00357 if (!mode_nosigma) {
00358
00359 computestd(mxGetPr(prhs[ARG_IMAGE]),
00360 mxGetPr(prhs[ARG_LABEL]),
00361 npix,
00362 mxGetPr(arg_mu),
00363 mxGetPr(arg_ct),
00364 mxGetPr(arg_sigma),
00365 nclasses);
00366 }
00367
00368
00369
00370
00371
00372 plhs[ARG_MU] = arg_mu;
00373
00374 if (nlhs > ARG_SIGMA)
00375 plhs[ARG_SIGMA] = arg_sigma;
00376 else
00377 mxDestroyArray(arg_sigma);
00378
00379 if (nlhs > ARG_CT)
00380 plhs[ARG_CT] = arg_ct;
00381 else
00382 mxDestroyArray(arg_ct);
00383 }
00384
00385
00386
00387
00388 #ifdef MEX2C_TAIL_HOOK
00389 #include "mex2c_tail.h"
00390 #endif
00391