write_output.c 8.0 KB

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