write_output.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/imagery.h>
  5. #include <grass/segment.h> /* segmentation library */
  6. #include <grass/glocale.h>
  7. #include "iseg.h"
  8. int write_ids(struct globals *globals)
  9. {
  10. int out_fd, row, col, maxid;
  11. CELL *outbuf, rid;
  12. struct Colors colors;
  13. struct History hist;
  14. outbuf = Rast_allocate_c_buf();
  15. G_debug(1, "preparing output raster");
  16. /* open output raster map */
  17. out_fd = Rast_open_new(globals->out_name, CELL_TYPE);
  18. G_debug(1, "start data transfer from segmentation file to raster");
  19. G_message(_("Writing out segment IDs..."));
  20. maxid = 0;
  21. for (row = 0; row < globals->nrows; row++) {
  22. G_percent(row, globals->nrows, 9);
  23. Rast_set_c_null_value(outbuf, globals->ncols);
  24. for (col = 0; col < globals->ncols; col++) {
  25. if (!(FLAG_GET(globals->null_flag, row, col))) {
  26. Segment_get(&globals->rid_seg, (void *) &rid, row, col);
  27. if (rid > 0) {
  28. if (globals->method == ORM_RG)
  29. rid = globals->new_id[rid];
  30. outbuf[col] = rid;
  31. if (maxid < rid)
  32. maxid = rid;
  33. }
  34. }
  35. }
  36. Rast_put_row(out_fd, outbuf, CELL_TYPE);
  37. }
  38. G_percent(1, 1, 1);
  39. /* close and save segment id file */
  40. Rast_close(out_fd);
  41. G_free(outbuf);
  42. /* set colors */
  43. Rast_init_colors(&colors);
  44. Rast_make_random_colors(&colors, 1, maxid);
  45. Rast_write_colors(globals->out_name, G_mapset(), &colors);
  46. Rast_short_history(globals->out_name, "raster", &hist);
  47. Rast_command_history(&hist);
  48. Rast_write_history(globals->out_name, &hist);
  49. /* free memory */
  50. Rast_free_colors(&colors);
  51. return TRUE;
  52. }
  53. /* write goodness of fit */
  54. int write_gof_rg(struct globals *globals)
  55. {
  56. int row, col;
  57. int mean_fd;
  58. CELL rid;
  59. FCELL *meanbuf;
  60. double thresh, maxdev, sim, mingood;
  61. struct ngbr_stats Ri, Rk;
  62. DCELL **inbuf; /* buffers to store lines from each of the imagery group rasters */
  63. int n, *in_fd;
  64. struct FPRange *fp_range; /* min/max values of each input raster */
  65. struct Colors colors;
  66. struct History hist;
  67. DCELL *min, *max;
  68. mean_fd = Rast_open_new(globals->gof, FCELL_TYPE);
  69. meanbuf = Rast_allocate_f_buf();
  70. /* goodness of fit for each cell: 1 = good fit, 0 = bad fit */
  71. /* similarity of each cell to region mean
  72. * max possible difference: globals->threshold
  73. * if similarity < globals->alpha * globals->alpha * globals->threshold
  74. * 1
  75. * else
  76. * (similarity - globals->alpha * globals->alpha * globals->threshold) /
  77. * (globals->threshold * (1 - globals->alpha * globals->alpha) */
  78. thresh = globals->alpha * globals->alpha * globals->max_diff;
  79. maxdev = globals->max_diff * (1 - globals->alpha * globals->alpha);
  80. mingood = 1;
  81. /* open input bands */
  82. in_fd = G_malloc(globals->Ref.nfiles * sizeof(int));
  83. inbuf = (DCELL **) G_malloc(globals->Ref.nfiles * sizeof(DCELL *));
  84. fp_range = G_malloc(globals->Ref.nfiles * sizeof(struct FPRange));
  85. min = G_malloc(globals->Ref.nfiles * sizeof(DCELL));
  86. max = G_malloc(globals->Ref.nfiles * sizeof(DCELL));
  87. G_debug(1, "Opening input rasters...");
  88. for (n = 0; n < globals->Ref.nfiles; n++) {
  89. inbuf[n] = Rast_allocate_d_buf();
  90. in_fd[n] = Rast_open_old(globals->Ref.file[n].name, globals->Ref.file[n].mapset);
  91. /* returns -1 on error, 2 on empty range, quitting either way. */
  92. if (Rast_read_fp_range(globals->Ref.file[n].name, globals->Ref.file[n].mapset, &fp_range[n]) != 1)
  93. G_fatal_error(_("No min/max found in raster map <%s>"),
  94. globals->Ref.file[n].name);
  95. Rast_get_fp_range_min_max(&(fp_range[n]), &min[n], &max[n]);
  96. G_debug(1, "Range for layer %d: min = %f, max = %f",
  97. n, min[n], max[n]);
  98. }
  99. G_message(_("Writing out goodness of fit"));
  100. for (row = 0; row < globals->nrows; row++) {
  101. G_percent(row, globals->nrows, 9);
  102. Rast_set_f_null_value(meanbuf, globals->ncols);
  103. for (n = 0; n < globals->Ref.nfiles; n++) {
  104. Rast_get_d_row(in_fd[n], inbuf[n], row);
  105. }
  106. for (col = 0; col < globals->ncols; col++) {
  107. if (!(FLAG_GET(globals->null_flag, row, col))) {
  108. Segment_get(&globals->rid_seg, (void *) &rid, row, col);
  109. if (rid > 0) {
  110. Ri.row = Rk.row = row;
  111. Ri.col = Rk.col = col;
  112. /* get values for Ri = this region */
  113. globals->rs.id = rid;
  114. fetch_reg_stats(row, col, &globals->rs, globals);
  115. Ri.mean = globals->rs.mean;
  116. Ri.count = globals->rs.count;
  117. sim = 0.;
  118. /* region consists of more than one cell */
  119. if (Ri.count > 1) {
  120. /* get values for Rk = this cell */
  121. for (n = 0; n < globals->Ref.nfiles; n++) {
  122. if (globals->weighted == FALSE)
  123. /* scaled version */
  124. globals->second_val[n] = (inbuf[n][col] - min[n]) / (max[n] - min[n]);
  125. else
  126. globals->second_val[n] = inbuf[n][col];
  127. }
  128. Rk.mean = globals->second_val;
  129. /* calculate similarity */
  130. sim = (*globals->calculate_similarity) (&Ri, &Rk, globals);
  131. }
  132. if (0) {
  133. if (sim < thresh)
  134. meanbuf[col] = 1;
  135. else {
  136. sim = 1. - (sim - thresh) / maxdev;
  137. meanbuf[col] = sim;
  138. if (mingood > sim)
  139. mingood = sim;
  140. }
  141. }
  142. else {
  143. sim = 1 - sim;
  144. meanbuf[col] = sim;
  145. if (mingood > sim)
  146. mingood = sim;
  147. }
  148. }
  149. }
  150. }
  151. Rast_put_row(mean_fd, meanbuf, FCELL_TYPE);
  152. }
  153. Rast_close(mean_fd);
  154. Rast_init_colors(&colors);
  155. Rast_make_grey_scale_fp_colors(&colors, mingood, 1);
  156. Rast_write_colors(globals->gof, G_mapset(), &colors);
  157. Rast_short_history(globals->gof, "raster", &hist);
  158. Rast_command_history(&hist);
  159. Rast_write_history(globals->gof, &hist);
  160. G_free(meanbuf);
  161. G_debug(1, "Closing input rasters...");
  162. for (n = 0; n < globals->Ref.nfiles; n++) {
  163. Rast_close(in_fd[n]);
  164. G_free(inbuf[n]);
  165. }
  166. G_free(inbuf);
  167. G_free(in_fd);
  168. G_free(fp_range);
  169. G_free(min);
  170. G_free(max);
  171. return TRUE;
  172. }
  173. int write_bands_ms(struct globals *globals)
  174. {
  175. int *out_fd, row, col, n;
  176. DCELL **outbuf;
  177. char **name;
  178. struct Colors colors;
  179. struct History hist;
  180. struct ngbr_stats Rout;
  181. out_fd = G_malloc(sizeof(int) * globals->nbands);
  182. name = G_malloc(sizeof(char *) * globals->nbands);
  183. outbuf = G_malloc(sizeof(DCELL) * globals->nbands);
  184. for (n = 0; n < globals->nbands; n++) {
  185. outbuf[n] = Rast_allocate_d_buf();
  186. G_asprintf(&name[n], "%s%s", globals->Ref.file[n].name, globals->bsuf);
  187. out_fd[n] = Rast_open_new(name[n], DCELL_TYPE);
  188. }
  189. Rout.mean = G_malloc(globals->datasize);
  190. G_message(_("Writing out shifted band values..."));
  191. for (row = 0; row < globals->nrows; row++) {
  192. G_percent(row, globals->nrows, 9);
  193. for (n = 0; n < globals->nbands; n++)
  194. Rast_set_d_null_value(outbuf[n], globals->ncols);
  195. for (col = 0; col < globals->ncols; col++) {
  196. if (!(FLAG_GET(globals->null_flag, row, col))) {
  197. Segment_get(globals->bands_out, (void *) Rout.mean, row, col);
  198. for (n = 0; n < globals->nbands; n++) {
  199. outbuf[n][col] = Rout.mean[n];
  200. if (globals->weighted == FALSE)
  201. outbuf[n][col] = Rout.mean[n] * (globals->max[n] - globals->min[n]) + globals->min[n];
  202. }
  203. }
  204. }
  205. for (n = 0; n < globals->nbands; n++)
  206. Rast_put_row(out_fd[n], outbuf[n], DCELL_TYPE);
  207. }
  208. for (n = 0; n < globals->nbands; n++) {
  209. Rast_close(out_fd[n]);
  210. Rast_read_colors(globals->Ref.file[n].name, globals->Ref.file[n].mapset, &colors);
  211. Rast_write_colors(name[n], G_mapset(), &colors);
  212. Rast_short_history(name[n], "raster", &hist);
  213. Rast_command_history(&hist);
  214. Rast_write_history(name[n], &hist);
  215. }
  216. /* free */
  217. return TRUE;
  218. }
  219. int close_files(struct globals *globals)
  220. {
  221. G_debug(1, "Closing input rasters...");
  222. /* close segmentation files and output raster */
  223. G_debug(1, "closing files");
  224. Segment_close(&globals->bands_seg);
  225. if (globals->method == ORM_MS)
  226. Segment_close(&globals->bands_seg2);
  227. if (globals->bounds_map)
  228. Segment_close(&globals->bounds_seg);
  229. G_free(globals->bands_val);
  230. G_free(globals->second_val);
  231. Segment_close(&globals->rid_seg);
  232. flag_destroy(globals->null_flag);
  233. flag_destroy(globals->candidate_flag);
  234. rgtree_destroy(globals->reg_tree);
  235. /* anything else left to clean up? */
  236. return TRUE;
  237. }