write_output.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* transfer the segmented regions from the segmented data file to a raster file */
  2. /* close_files() function is at bottom */
  3. #include <stdlib.h>
  4. #include <grass/gis.h>
  5. #include <grass/raster.h>
  6. #include <grass/imagery.h>
  7. #include <grass/segment.h> /* segmentation library */
  8. #include <grass/glocale.h>
  9. #include "iseg.h"
  10. /* TODO some time delay here with meanbuf, etc being processed. I only put if statements on the actual files
  11. * to try and keep the code more clear. Need to see if this raster makes stats processing easier? Or IFDEF it out?
  12. */
  13. int write_output(struct globals *globals)
  14. {
  15. int out_fd, row, col, maxid;
  16. CELL *outbuf, rid;
  17. struct Colors colors;
  18. struct History hist;
  19. outbuf = Rast_allocate_c_buf();
  20. G_debug(1, "preparing output raster");
  21. /* open output raster map */
  22. out_fd = Rast_open_new(globals->out_name, CELL_TYPE);
  23. G_debug(1, "start data transfer from segmentation file to raster");
  24. G_message(_("Writing out segment IDs..."));
  25. maxid = 0;
  26. for (row = 0; row < globals->nrows; row++) {
  27. G_percent(row, globals->nrows, 9);
  28. Rast_set_c_null_value(outbuf, globals->ncols);
  29. for (col = 0; col < globals->ncols; col++) {
  30. if (!(FLAG_GET(globals->null_flag, row, col))) {
  31. segment_get(&globals->rid_seg, (void *) &rid, row, col);
  32. if (rid > 0) {
  33. outbuf[col] = rid;
  34. if (maxid < rid)
  35. maxid = rid;
  36. }
  37. }
  38. }
  39. Rast_put_row(out_fd, outbuf, CELL_TYPE);
  40. }
  41. G_percent(1, 1, 1);
  42. /* close and save segment id file */
  43. Rast_close(out_fd);
  44. G_free(outbuf);
  45. /* set colors */
  46. Rast_init_colors(&colors);
  47. Rast_make_random_colors(&colors, 1, maxid);
  48. Rast_write_colors(globals->out_name, G_mapset(), &colors);
  49. Rast_short_history(globals->out_name, "raster", &hist);
  50. Rast_command_history(&hist);
  51. Rast_write_history(globals->out_name, &hist);
  52. /* write goodness of fit */
  53. if (globals->out_band) {
  54. int mean_fd;
  55. FCELL *meanbuf;
  56. double thresh, maxdev, sim, mingood;
  57. struct ngbr_stats Ri, Rk;
  58. struct Ref Ref; /* group reference list */
  59. DCELL **inbuf; /* buffers to store lines from each of the imagery group rasters */
  60. int n, *in_fd;
  61. mean_fd = Rast_open_new(globals->out_band, FCELL_TYPE);
  62. meanbuf = Rast_allocate_f_buf();
  63. /* goodness of fit for each cell: 1 = good fit, 0 = bad fit */
  64. /* similarity of each cell to region mean
  65. * max possible difference: globals->threshold
  66. * if similarity < globals->alpha * globals->alpha * globals->threshold
  67. * 1
  68. * else
  69. * (similarity - globals->alpha * globals->alpha * globals->threshold) /
  70. * (globals->threshold * (1 - globals->alpha * globals->alpha) */
  71. thresh = globals->alpha * globals->alpha * globals->max_diff;
  72. maxdev = globals->max_diff * (1 - globals->alpha * globals->alpha);
  73. mingood = 1;
  74. /* open input bands */
  75. if (!I_get_group_ref(globals->image_group, &Ref))
  76. G_fatal_error(_("Unable to read REF file for group <%s>"),
  77. globals->image_group);
  78. if (Ref.nfiles <= 0)
  79. G_fatal_error(_("Group <%s> contains no raster maps"),
  80. globals->image_group);
  81. in_fd = G_malloc(Ref.nfiles * sizeof(int));
  82. inbuf = (DCELL **) G_malloc(Ref.nfiles * sizeof(DCELL *));
  83. G_debug(1, "Opening input rasters...");
  84. for (n = 0; n < Ref.nfiles; n++) {
  85. inbuf[n] = Rast_allocate_d_buf();
  86. in_fd[n] = Rast_open_old(Ref.file[n].name, Ref.file[n].mapset);
  87. }
  88. G_message(_("Writing out goodness of fit"));
  89. for (row = 0; row < globals->nrows; row++) {
  90. G_percent(row, globals->nrows, 9);
  91. Rast_set_f_null_value(meanbuf, globals->ncols);
  92. for (n = 0; n < Ref.nfiles; n++) {
  93. Rast_get_d_row(in_fd[n], inbuf[n], row);
  94. }
  95. for (col = 0; col < globals->ncols; col++) {
  96. if (!(FLAG_GET(globals->null_flag, row, col))) {
  97. segment_get(&globals->rid_seg, (void *) &rid, row, col);
  98. if (rid > 0) {
  99. Ri.row = Rk.row = row;
  100. Ri.col = Rk.col = col;
  101. /* get values for Ri = this region */
  102. globals->rs.id = rid;
  103. fetch_reg_stats(row, col, &globals->rs, globals);
  104. Ri.mean = globals->rs.mean;
  105. Ri.count = globals->rs.count;
  106. sim = 0.;
  107. /* region consists of more than one cell */
  108. if (Ri.count > 1) {
  109. /* get values for Rk = this cell */
  110. for (n = 0; n < Ref.nfiles; n++) {
  111. globals->second_val[n] = inbuf[n][col];
  112. }
  113. Rk.mean = globals->second_val;
  114. /* calculate similarity */
  115. sim = (*globals->calculate_similarity) (&Ri, &Rk, globals);
  116. }
  117. if (0) {
  118. if (sim < thresh)
  119. meanbuf[col] = 1;
  120. else {
  121. sim = 1. - (sim - thresh) / maxdev;
  122. meanbuf[col] = sim;
  123. if (mingood > sim)
  124. mingood = sim;
  125. }
  126. }
  127. else {
  128. sim = 1 - sim / globals->max_diff;
  129. meanbuf[col] = sim;
  130. if (mingood > sim)
  131. mingood = sim;
  132. }
  133. }
  134. }
  135. }
  136. Rast_put_row(mean_fd, meanbuf, FCELL_TYPE);
  137. }
  138. Rast_close(mean_fd);
  139. Rast_init_colors(&colors);
  140. Rast_make_grey_scale_fp_colors(&colors, mingood, 1);
  141. Rast_write_colors(globals->out_band, G_mapset(), &colors);
  142. Rast_short_history(globals->out_band, "raster", &hist);
  143. Rast_command_history(&hist);
  144. Rast_write_history(globals->out_band, &hist);
  145. G_free(meanbuf);
  146. G_debug(1, "Closing input rasters...");
  147. for (n = 0; n < Ref.nfiles; n++) {
  148. Rast_close(in_fd[n]);
  149. G_free(inbuf[n]);
  150. }
  151. G_free(inbuf);
  152. G_free(in_fd);
  153. }
  154. /* free memory */
  155. Rast_free_colors(&colors);
  156. return TRUE;
  157. }
  158. int close_files(struct globals *globals)
  159. {
  160. /* close segmentation files and output raster */
  161. G_debug(1, "closing files");
  162. segment_close(&globals->bands_seg);
  163. if (globals->bounds_map)
  164. segment_close(&globals->bounds_seg);
  165. G_free(globals->bands_val);
  166. G_free(globals->second_val);
  167. segment_close(&globals->rid_seg);
  168. flag_destroy(globals->null_flag);
  169. flag_destroy(globals->candidate_flag);
  170. rgtree_destroy(globals->reg_tree);
  171. /* anything else left to clean up? */
  172. return TRUE;
  173. }