parse_args.c 11 KB

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