00001 #include "mex.h"
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include <math.h>
00006 #include "mexhead.h"
00007 #include "Doc/clean_edge_label_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
00051
00052
00053
00054
00055
00056
00057
00058
00059 #define NARGIN_MAX 5
00060 #define NARGIN_MIN 5
00061 #define NARGOUT_MIN 1
00062 #define NARGOUT_MAX 2
00063
00064 #define ARG_img 0 // input image
00065 #define ARG_center 1 // apparent center and radius of the sun
00066 #define ARG_delta 2 // width of annulus
00067 #define ARG_fill 3 // infill value
00068 #define ARG_mode 4 // mode of operation
00069
00070 #define ARG_out 0
00071 #define ARG_nclean 1
00072
00073 static const char *progname = "clean_edge_label";
00074 #define PROGNAME clean_edge_label
00075 static const char *in_specs[NARGIN_MAX] = {
00076 "RM",
00077 "RV(3)|RV(5)",
00078 "RS",
00079 "RS",
00080 "SV" };
00081 static const char *in_names[NARGIN_MAX] = {
00082 "image",
00083 "center",
00084 "delta",
00085 "fill",
00086 "mode"};
00087 static const char *out_names[NARGOUT_MAX] = {
00088 "res",
00089 "nclean"};
00090
00091
00092 #define SHORTNAME cel
00093
00094
00095
00096
00097
00098
00099 static
00100 char *
00101 extract_mode(char *mode, int *Msesw, int *Madapt)
00102 {
00103 char *word, *word2, *endptr;
00104 char *sep = ", \t";
00105
00106 if (!mode) return "null";
00107 for (word = strtok(mode, sep); word; word = strtok(NULL, sep)) {
00108
00109 if (strcasecmp(word, "sesw" ) == 0) *Msesw = 1;
00110 else if (strcasecmp(word, "sene" ) == 0) *Msesw = 0;
00111
00112 else if (strcasecmp(word, "adaptive") == 0) *Madapt = 1;
00113
00114 else return word;
00115 }
00116 return 0;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 static
00128 void
00129 propagate(int *count,
00130 double *image,
00131 double *imagep,
00132 int maxx, int maxy,
00133 int strx,
00134 int stry,
00135 double cenx,
00136 double ceny,
00137 double radius,
00138 double delta,
00139 double fill_val,
00140 int mode_adapt
00141 )
00142 {
00143 int x, y;
00144 int offset, inset;
00145 double r2;
00146 double dx, dy;
00147 int x_off, y_off;
00148 int ct = 0;
00149 double scale;
00150 double offdisk;
00151 const double radius2 = radius*radius;
00152 const double radius_inside2 = (radius - delta)*(radius - delta);
00153
00154
00155 if (maxx * maxy > 0)
00156 offdisk = image[0];
00157 else
00158 offdisk = 0;
00159 for (x = 0; x < maxx; x++)
00160 for (y = 0; y < maxy; y++) {
00161 offset = x*strx + y*stry;
00162
00163 dx = x - cenx;
00164 dy = y - ceny;
00165
00166 r2 = dx*dx + dy*dy;
00167
00168 if (r2 > radius2) {
00169
00170 imagep[offset] = offdisk;
00171
00172
00173 } else if (r2 < radius_inside2) {
00174
00175 imagep[offset] = image[offset];
00176 } else {
00177
00178 if (!mode_adapt) {
00179
00180 imagep[offset] = fill_val;
00181 } else {
00182
00183 scale = sqrt(radius_inside2/r2);
00184
00185 dx *= scale; dy *= scale;
00186
00187 if (dx > 0)
00188 x_off = floor(cenx + dx);
00189 else
00190 x_off = ceil(cenx + dx);
00191 if (dy > 0)
00192 y_off = floor(ceny + dy);
00193 else
00194 y_off = ceil(ceny + dy);
00195
00196 inset = x_off*strx + y_off*stry;
00197
00198 imagep[offset] = image[inset];
00199 }
00200
00201
00202 if (image[offset] != imagep[offset])
00203 ct++;
00204 }
00205 }
00206 *count = ct;
00207
00208
00209
00210 }
00211
00212
00213
00214 #ifdef StaticP
00215 StaticP
00216 #endif
00217 void
00218 mexFunction(int nlhs,
00219 mxArray *plhs[],
00220 int nrhs,
00221 const mxArray *prhs[])
00222 {
00223 int m, n, maxx, maxy;
00224 int strx, stry;
00225 double *center;
00226 int mode_sesw;
00227 int mode_adapt;
00228 char *mode, *word;
00229 char errstr[200];
00230 double datamin, datamax;
00231 int count;
00232
00233
00234 if (nrhs < 0) {
00235 plhs[0] = mxt_PackSignature((mxt_Signature) (-nrhs),
00236 NARGIN_MIN, NARGIN_MAX,
00237 NARGOUT_MIN, NARGOUT_MAX,
00238 in_names, in_specs, out_names, docstring);
00239 return;
00240 }
00241
00242 if ((nrhs < NARGIN_MIN) || (nrhs > NARGIN_MAX))
00243 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00244 "%s: Expect %d <= input args <= %d",
00245 progname, NARGIN_MIN, NARGIN_MAX), errstr));
00246 if (nlhs < NARGOUT_MIN || nlhs > NARGOUT_MAX)
00247 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00248 "%s: Expect %d <= output args <= %d",
00249 progname, NARGOUT_MIN, NARGOUT_MAX), errstr));
00250 mexargparse(nrhs, prhs, in_names, in_specs, NULL, progname);
00251
00252
00253
00254
00255
00256 if ((mode = mxArrayToString(prhs[ARG_mode])) == NULL)
00257 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00258 "%s: bad mode (non-string?). "
00259 "Could not convert mode arg to string.",
00260 progname), errstr));
00261 mode_sesw = -1;
00262 mode_adapt = 0;
00263 if ((word = extract_mode(mode, &mode_sesw, &mode_adapt)) != NULL)
00264 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00265 "%s: bad word <%s> in mode <%.80s>",
00266 progname, word,
00267 mxArrayToString(prhs[ARG_mode])), errstr));
00268 if (mode_sesw < 0)
00269 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00270 "%s: need sesw/sene in mode <%.80s>",
00271 progname,
00272 mxArrayToString(prhs[ARG_mode])), errstr));
00273 mxFree(mode);
00274
00275
00276 center = mxGetPr(prhs[ARG_center]);
00277
00278
00279 m = mxGetM(prhs[ARG_img]);
00280 n = mxGetN(prhs[ARG_img]);
00281
00282
00283
00284
00285
00286
00287
00288 if (mode_sesw) {
00289
00290 strx = 1; stry = m;
00291 maxx = m; maxy = n;
00292 } else {
00293
00294 strx = m; stry = 1;
00295 maxx = n; maxy = m;
00296 }
00297
00298
00299 plhs[ARG_out] = mxCreateDoubleMatrix(m, n, mxREAL);
00300 if (!plhs[ARG_out])
00301 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00302 "%s: Failed calloc for output image",
00303 progname), errstr));
00304 if (nlhs > 1) {
00305 plhs[ARG_nclean] = mxCreateDoubleMatrix(1, 1, mxREAL);
00306 if (!plhs[ARG_nclean])
00307 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00308 "%s: Failed calloc for output count",
00309 progname), errstr));
00310 }
00311
00312
00313
00314 setrange(plhs[ARG_out], 0.0, 254.0);
00315
00316
00317 if (mxGetScalar(prhs[ARG_delta]) >= 0.0) {
00318 propagate(&count,
00319 mxGetPr(prhs[ARG_img]),
00320 mxGetPr(plhs[ARG_out]),
00321 maxx, maxy,
00322 strx, stry,
00323 center[0]-1,
00324 center[1]-1,
00325 center[2],
00326 mxGetScalar(prhs[ARG_delta]),
00327 mxGetScalar(prhs[ARG_fill ]),
00328 mode_adapt
00329 );
00330 } else {
00331
00332 memcpy(mxGetPr(plhs[ARG_out]),
00333 mxGetPr(prhs[ARG_img]),
00334 m * n * sizeof(double));
00335 count = 0;
00336 }
00337
00338 if (nlhs > 1)
00339 *mxGetPr(plhs[ARG_nclean]) = (double) count;
00340 }
00341
00342
00343
00344
00345 #ifdef MEX2C_TAIL_HOOK
00346 #include "mex2c_tail.h"
00347 #endif
00348