1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
/* PURPOSE: Parse and validate the input */
#include <stdlib.h>
#include <string.h>
#include <grass/gis.h>
#include <grass/glocale.h>
#include <grass/raster.h>
#include "iseg.h"
int parse_args(int argc, char *argv[], struct globals *globals)
{
struct Option *group, *seeds, *bounds, *output, *method, *similarity,
*threshold, *min_segment_size, *hs, *hr, *bsuf,
#ifdef _OR_SHAPE_
*shape_weight, *smooth_weight,
#endif
*mem;
struct Flag *diagonal, *weighted, *ms_a, *ms_p;
struct Option *gof, *endt;
int bands;
/* required parameters */
group = G_define_standard_option(G_OPT_R_INPUTS);
group->key = "group";
group->description = _("Name of input imagery group or raster maps");
output = G_define_standard_option(G_OPT_R_OUTPUT);
bsuf = G_define_standard_option(G_OPT_R_OUTPUT);
bsuf->key = "band_suffix";
bsuf->required = NO;
bsuf->label = _("Suffix for output bands with modified band values");
threshold = G_define_option();
threshold->key = "threshold";
threshold->type = TYPE_DOUBLE;
threshold->required = YES;
threshold->label = _("Difference threshold between 0 and 1");
threshold->description = _("Threshold = 0 merges only identical segments; "
"threshold = 1 merges all");
/* optional parameters */
hs = G_define_option();
hs->key = "radius";
hs->type = TYPE_DOUBLE;
hs->required = NO;
hs->answer = "1.5";
hs->label = _("Spatial radius in number of cells");
hs->description = _("Must be >= 1, only cells within spatial bandwidth are "
"considered for mean shift");
hr = G_define_option();
hr->key = "hr";
hr->type = TYPE_DOUBLE;
hr->required = NO;
hr->label = _("Range (spectral) bandwidth [0, 1]");
hr->description = _("Only cells within range (spectral) bandwidth are "
"considered for mean shift. Range bandwidth is used as "
"conductance parameter for adaptive bandwidth");
method = G_define_option();
method->key = "method";
method->type = TYPE_STRING;
method->required = NO;
method->answer = "region_growing";
method->options = "region_growing,mean_shift";
/*
Watershed method disabled, it's not implemented yet, see
https://trac.osgeo.org/grass/ticket/3181
method->options = "region_growing,mean_shift,watershed";
*/
method->description = _("Segmentation method");
method->guisection = _("Settings");
similarity = G_define_option();
similarity->key = "similarity";
similarity->type = TYPE_STRING;
similarity->required = NO;
similarity->answer = "euclidean";
similarity->options = "euclidean,manhattan";
similarity->description = _("Similarity calculation method");
similarity->guisection = _("Settings");
min_segment_size = G_define_option();
min_segment_size->key = "minsize";
min_segment_size->type = TYPE_INTEGER;
min_segment_size->required = NO;
min_segment_size->answer = "1";
min_segment_size->options = "1-100000";
min_segment_size->label = _("Minimum number of cells in a segment");
min_segment_size->description =
_("The final step will merge small segments with their best neighbor");
min_segment_size->guisection = _("Settings");
#ifdef _OR_SHAPE_
radio_weight = G_define_option();
radio_weight->key = "radio_weight";
radio_weight->type = TYPE_DOUBLE;
radio_weight->required = NO;
radio_weight->answer = "1";
radio_weight->options = "0-1";
radio_weight->label =
_("Importance of radiometric (input raster) values relative to shape");
radio_weight->guisection = _("Settings");
smooth_weight = G_define_option();
smooth_weight->key = "smooth_weight";
smooth_weight->type = TYPE_DOUBLE;
smooth_weight->required = NO;
smooth_weight->answer = "0.5";
smooth_weight->options = "0-1";
smooth_weight->label =
_("Importance of smoothness relative to compactness");
smooth_weight->guisection = _("Settings");
#endif
mem = G_define_standard_option(G_OPT_MEMORYMB);
/* TODO input for distance function */
/* debug parameters */
endt = G_define_option();
endt->key = "iterations";
endt->type = TYPE_INTEGER;
endt->required = NO;
endt->description = _("Maximum number of iterations");
endt->guisection = _("Settings");
/* Using raster for seeds
* Low priority TODO: allow vector points/centroids seed input. */
seeds = G_define_standard_option(G_OPT_R_INPUT);
seeds->key = "seeds";
seeds->required = NO;
seeds->description = _("Name for input raster map with starting seeds");
seeds->guisection = _("Settings");
/* Polygon constraints. */
bounds = G_define_standard_option(G_OPT_R_INPUT);
bounds->key = "bounds";
bounds->required = NO;
bounds->label = _("Name of input bounding/constraining raster map");
bounds->description = _("Must be integer values, each area will be "
"segmented independent of the others");
bounds->guisection = _("Settings");
gof = G_define_standard_option(G_OPT_R_OUTPUT);
gof->key = "goodness";
gof->required = NO;
gof->description = _("Name for output goodness of fit estimate map");
gof->guisection = _("Settings");
diagonal = G_define_flag();
diagonal->key = 'd';
diagonal->description = _("Use 8 neighbors (3x3 neighborhood) instead of "
"the default 4 neighbors for each pixel");
diagonal->guisection = _("Settings");
weighted = G_define_flag();
weighted->key = 'w';
weighted->description = _("Weighted input, do not perform the default "
"scaling of input raster maps");
weighted->guisection = _("Settings");
ms_a = G_define_flag();
ms_a->key = 'a';
ms_a->label = _("Use adaptive bandwidth for mean shift");
ms_a->description =
_("Range (spectral) bandwidth is adapted for each moving window");
ms_a->guisection = _("Settings");
ms_p = G_define_flag();
ms_p->key = 'p';
ms_p->label = _("Use progressive bandwidth for mean shift");
ms_p->description = _("Spatial bandwidth is increased, range (spectral) "
"bandwidth is decreased in each iteration");
ms_p->guisection = _("Settings");
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
/* Check and save parameters */
for (bands = 0; group->answers[bands] != NULL; bands++)
;
I_init_group_ref(&globals->Ref);
if (bands > 1 || !I_find_group(group->answers[0])) {
/* create group from input is raster map(s) */
char name[GNAME_MAX];
const char *mapset;
for (bands = 0; group->answers[bands] != NULL; bands++) {
/* strip @mapset, do not modify opt_in->answers */
strcpy(name, group->answers[bands]);
mapset = G_find_raster(name, "");
if (!mapset)
G_fatal_error(_("Raster map <%s> not found"),
group->answers[bands]);
/* Add input to group. */
I_add_file_to_group_ref(name, mapset, &globals->Ref);
}
globals->image_group = NULL;
}
else {
/* input is group. Try to read group file */
if (!I_get_group_ref(group->answers[0], &globals->Ref))
G_fatal_error(_("Group <%s> not found in the current mapset"),
group->answers[0]);
if (globals->Ref.nfiles <= 0)
G_fatal_error(_("Group <%s> contains no raster maps"),
globals->image_group);
globals->image_group = group->answers[0];
}
if (G_legal_filename(output->answer) == TRUE)
globals->out_name = output->answer;
else
G_fatal_error("Invalid output raster name");
globals->bsuf = bsuf->answer;
globals->alpha = atof(threshold->answer);
if (globals->alpha <= 0 || globals->alpha >= 1)
G_fatal_error(_("Threshold should be > 0 and < 1"));
globals->hs = -1;
if (hs->answer) {
globals->hs = atof(hs->answer);
if (globals->hs < 1) {
G_fatal_error(_("Option '%s' must be >= 1"), hs->key);
}
}
globals->hr = -1;
if (hr->answer) {
globals->hr = atof(hr->answer);
if (globals->hr < 0) {
G_warning(_("Negative value %s for option '%s': disabling"),
hr->answer, hr->key);
globals->hr = -1;
}
if (globals->hr >= 1) {
G_warning(_("Value %s for option '%s' is >= 1: disabling"),
hr->answer, hr->key);
globals->hr = -1;
}
}
globals->ms_adaptive = ms_a->answer;
globals->ms_progressive = ms_p->answer;
/* segmentation methods */
if (strcmp(method->answer, "region_growing") == 0) {
globals->method = ORM_RG;
globals->method_fn = region_growing;
}
else if (strcmp(method->answer, "mean_shift") == 0) {
globals->method = ORM_MS;
globals->method_fn = mean_shift;
}
else if (strcmp(method->answer, "watershed") == 0) {
globals->method = ORM_WS;
globals->method_fn = watershed;
}
else
G_fatal_error(_("Unable to assign segmentation method"));
G_debug(1, "segmentation method: %s (%d)", method->answer, globals->method);
/* distance methods for similarity measurement */
if (strcmp(similarity->answer, "euclidean") == 0)
globals->calculate_similarity = calculate_euclidean_similarity;
else if (strcmp(similarity->answer, "manhattan") == 0)
globals->calculate_similarity = calculate_manhattan_similarity;
else
G_fatal_error(_("Invalid similarity method"));
#ifdef _OR_SHAPE_
/* consider shape */
globals->radio_weight = atof(radio_weight->answer);
if (globals->radio_weight <= 0)
G_fatal_error(_("Option '%s' must be > 0"), radio_weight->key);
if (globals->radio_weight > 1)
G_fatal_error(_("Option '%s' must be <= 1"), radio_weight->key);
globals->smooth_weight = atof(smooth_weight->answer);
if (globals->smooth_weight < 0)
G_fatal_error(_("Option '%s' must be >= 0"), smooth_weight->key);
if (globals->smooth_weight > 1)
G_fatal_error(_("Option '%s' must be <= 1"), smooth_weight->key);
#else
globals->radio_weight = 1;
globals->smooth_weight = 0.5;
#endif
globals->min_segment_size = atoi(min_segment_size->answer);
if (diagonal->answer == FALSE) {
globals->find_neighbors = find_four_neighbors;
globals->nn = 4;
G_debug(1, "four pixel neighborhood");
}
else if (diagonal->answer == TRUE) {
globals->find_neighbors = find_eight_neighbors;
globals->nn = 8;
G_debug(1, "eight (3x3) pixel neighborhood");
}
/* default/0 for performing the scaling
* selected/1 if scaling should be skipped. */
globals->weighted = weighted->answer;
globals->seeds = seeds->answer;
if (globals->seeds) {
if (G_find_raster(globals->seeds, "") == NULL) {
G_fatal_error(_("Seeds raster map not found"));
}
if (Rast_map_type(globals->seeds, "") != CELL_TYPE) {
G_fatal_error(_("Seeeds raster map must be CELL type (integers)"));
}
}
if (bounds->answer == NULL) {
globals->bounds_map = NULL;
}
else {
globals->bounds_map = bounds->answer;
if ((globals->bounds_mapset = G_find_raster(globals->bounds_map, "")) ==
NULL) {
G_fatal_error(
_("Segmentation constraint/boundary raster map not found"));
}
if (Rast_map_type(globals->bounds_map, globals->bounds_mapset) !=
CELL_TYPE) {
G_fatal_error(_("Segmentation constraint raster map must be CELL "
"type (integers)"));
}
}
/* other data */
globals->nrows = Rast_window_rows();
globals->ncols = Rast_window_cols();
if (sizeof(LARGEINT) < 8) {
int i;
LARGEINT intmax;
intmax = ((LARGEINT)1 << (sizeof(LARGEINT) * 8 - 2)) - 1;
intmax += ((LARGEINT)1 << (sizeof(LARGEINT) * 8 - 2));
globals->ncells = globals->ncols;
for (i = 1; i < globals->nrows; i++) {
if (globals->ncols > intmax - globals->ncells)
G_fatal_error(
_("Integer overflow: too many cells in current region"));
globals->ncells += globals->ncols;
}
}
/* debug help */
if (gof->answer == NULL)
globals->gof = NULL;
else {
if (G_legal_filename(gof->answer) == TRUE)
globals->gof = gof->answer;
else
G_fatal_error(_("Invalid output raster name for goodness of fit"));
}
if (!endt->answer) {
globals->end_t = 50;
if (globals->method == ORM_MS)
globals->end_t = 10;
G_message(_("Maximum number of iterations set to %d"), globals->end_t);
}
else {
if (atoi(endt->answer) > 0)
globals->end_t = atoi(endt->answer);
else {
globals->end_t = 50;
if (globals->method == ORM_MS)
globals->end_t = 10;
G_warning(_("Invalid number of iterations, %d will be used"),
globals->end_t);
}
}
if (mem->answer && atoi(mem->answer) > 10)
globals->mb = atoi(mem->answer);
else {
globals->mb = 300;
G_warning(_("Invalid number of MB, 300 will be used"));
}
return TRUE;
}
|