iseg.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /****************************************************************************
  2. *
  3. * MODULE: i.segment
  4. * AUTHOR(S): Eric Momsen <eric.momsen at gmail com>
  5. * PURPOSE: structure definition and function listing
  6. * COPYRIGHT: (C) 2012 by Eric Momsen, and the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the COPYING file that comes with GRASS
  10. * for details.
  11. *
  12. *****************************************************************************/
  13. #include <grass/segment.h>
  14. #include <grass/imagery.h>
  15. #include "flag.h"
  16. #include "regtree.h"
  17. #include "ngbrtree.h"
  18. /* #def _OR_SHAPE_ */
  19. #ifdef HAVE_LONG_LONG_INT
  20. #define LARGEINT long long
  21. #elif defined HAVE_LARGEFILES
  22. #define LARGEINT off_t
  23. #else
  24. #define LARGEINT long
  25. #endif
  26. /* methods */
  27. #define ORM_RG 1 /* region growing */
  28. #define ORM_MS 2 /* mean shift */
  29. #define ORM_WS 3 /* watershed */
  30. /* row/col list */
  31. struct rc
  32. {
  33. struct rc *next;
  34. int row;
  35. int col;
  36. };
  37. struct rclist
  38. {
  39. struct rc *tail, *head;
  40. };
  41. /* input and output files, as well as some other processing info */
  42. struct globals
  43. {
  44. /* input */
  45. char *image_group;
  46. struct Ref Ref; /* group reference list */
  47. DCELL *min, *max;
  48. int weighted; /* 0 if false/not selected, so we should scale input.
  49. * 1 if the scaling should be skipped */
  50. int nbands; /* number of rasters in the image group */
  51. size_t datasize; /* nbands * sizeof(double) */
  52. int mb; /* amount of memory to use in MB */
  53. char *seeds, *bounds_map; /* optional segment seeds and polygon constraints/boundaries */
  54. CELL lower_bound, upper_bound;
  55. const char *bounds_mapset;
  56. /* output */
  57. /* region growing */
  58. char *out_name; /* name of output raster map with regions */
  59. char *gof; /* indicator for segment heterogeneity / goodness of fit */
  60. char *bsuf; /* suffix to be appended to input bands */
  61. /* general segmentation */
  62. int method; /* Segmentation method code */
  63. int (*method_fn)(); /* Segmentation method function */
  64. int nn; /* number of neighbors, 4 or 8 */
  65. double max_diff; /* max possible difference */
  66. double alpha; /* similarity threshold */
  67. int end_t; /* maximum number of iterations */
  68. /* region growing */
  69. int min_segment_size; /* smallest number of pixels/cells allowed in a final segment */
  70. /* inactive options for region growing */
  71. double radio_weight; /* weighing factor radiometric - shape */
  72. double smooth_weight; /* weighing factor smoothness - compactness */
  73. /* mean shift */
  74. double hs, hr; /* spectral and range bandwidth */
  75. int ms_adaptive; /* use adaptive bandwidth */
  76. int ms_progressive; /* use progressive bandwidth */
  77. /* region info */
  78. int nrows, ncols;
  79. int row_min, row_max, col_min, col_max; /* region constraints */
  80. LARGEINT ncells, notnullcells;
  81. /* file processing */
  82. SEGMENT bands_seg, /* input group with one or more bands */
  83. bands_seg2, /* copy of bands_seg for mean shift */
  84. bounds_seg,
  85. rid_seg;
  86. SEGMENT *bands_in, *bands_out; /* pointers to input/output bands_seg, for mean shift */
  87. DCELL *bands_min, *bands_max;
  88. DCELL *bands_val; /* array to hold all input values for one cell */
  89. DCELL *second_val; /* array to hold all input values for another cell */
  90. /* maximum used region ID */
  91. CELL max_rid;
  92. /* region growing internal structure */
  93. struct RG_TREE *reg_tree; /* search tree with region stats */
  94. int min_reg_size; /* minimum region size */
  95. struct reg_stats rs, rs_i, rs_k;
  96. struct ngbr_stats ns;
  97. /* processing flags */
  98. FLAG *candidate_flag, *null_flag; /*TODO, need some way to remember MASK/NULL values. Was using -1, 0, 1 in int array. Better to use 2 FLAG structures, better readibility? */
  99. /* number of remaining cells to check */
  100. LARGEINT candidate_count;
  101. /* functions */
  102. void (*find_neighbors) (int, int, int[8][2]); /*parameters: row, col, neighbors */
  103. double (*calculate_similarity) (struct ngbr_stats *,
  104. struct ngbr_stats *,
  105. struct globals *); /*parameters: two regions to compare */
  106. };
  107. /* parse_args.c */
  108. /* gets input from user, validates, and sets up functions */
  109. int parse_args(int, char *[], struct globals *);
  110. /* open_files.c */
  111. int open_files(struct globals *);
  112. /* create_isegs.c */
  113. int create_isegs(struct globals *);
  114. void find_four_neighbors(int, int, int[][2]);
  115. void find_eight_neighbors(int, int, int[8][2]);
  116. double calculate_euclidean_similarity(struct ngbr_stats *,
  117. struct ngbr_stats *,
  118. struct globals *);
  119. double calculate_manhattan_similarity(struct ngbr_stats *,
  120. struct ngbr_stats *,
  121. struct globals *);
  122. double calculate_shape(struct reg_stats *, struct reg_stats *,
  123. int, struct globals *);
  124. int fetch_reg_stats(int , int , struct reg_stats *,
  125. struct globals *);
  126. int update_band_vals(int, int, struct reg_stats *, struct globals *);
  127. /* void calculate_reg_stats(int, int, struct reg_stats *,
  128. struct globals *); */
  129. /* region_growing.c */
  130. int region_growing(struct globals *);
  131. /* mean_shift.c */
  132. int mean_shift(struct globals *);
  133. /* cluster.c */
  134. CELL cluster_bands(struct globals *globals);
  135. /* watershed.c */
  136. int watershed(struct globals *);
  137. /* rclist.c */
  138. void rclist_init(struct rclist *);
  139. void rclist_add(struct rclist *, int, int);
  140. int rclist_drop(struct rclist *, struct rc *);
  141. void rclist_destroy(struct rclist *);
  142. /* write_output.c */
  143. int write_ids(struct globals *);
  144. int write_gof_rg(struct globals *);
  145. int write_bands_ms(struct globals *);
  146. int close_files(struct globals *);