parse_args.c 11 KB

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