write_output.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/segment.h> /* segmentation library */
  7. #include <grass/glocale.h>
  8. #include "iseg.h"
  9. /* TODO some time delay here with meanbuf, etc being processed. I only put if statements on the actual files
  10. * to try and keep the code more clear. Need to see if this raster makes stats processing easier? Or IFDEF it out?
  11. */
  12. int write_output(struct globals *globals)
  13. {
  14. int out_fd, row, col;
  15. CELL *outbuf, rid;
  16. struct Colors colors;
  17. struct History hist;
  18. outbuf = Rast_allocate_c_buf();
  19. G_debug(1, "preparing output raster");
  20. /* open output raster map */
  21. out_fd = Rast_open_new(globals->out_name, CELL_TYPE);
  22. G_debug(1, "start data transfer from segmentation file to raster");
  23. G_message(_("Writing out segment IDs"));
  24. for (row = 0; row < globals->nrows; row++) {
  25. G_percent(row, globals->nrows, 9);
  26. Rast_set_c_null_value(outbuf, globals->ncols);
  27. for (col = 0; col < globals->ncols; col++) {
  28. if (!(FLAG_GET(globals->null_flag, row, col))) {
  29. segment_get(&globals->rid_seg, (void *) &rid, row, col);
  30. if (rid > 0) {
  31. outbuf[col] = rid;
  32. }
  33. }
  34. }
  35. Rast_put_row(out_fd, outbuf, CELL_TYPE);
  36. }
  37. /* close and save segment id file */
  38. Rast_close(out_fd);
  39. /* set colors */
  40. Rast_init_colors(&colors);
  41. Rast_make_random_colors(&colors, 1, globals->nrows * globals->ncols);
  42. Rast_write_colors(globals->out_name, G_mapset(), &colors);
  43. Rast_short_history(globals->out_name, "raster", &hist);
  44. Rast_command_history(&hist);
  45. Rast_write_history(globals->out_name, &hist);
  46. /* write goodness of fit */
  47. if (globals->out_band) {
  48. int mean_fd;
  49. FCELL *meanbuf;
  50. double thresh, maxdev, sim, mingood;
  51. struct ngbr_stats Ri, Rk;
  52. mean_fd = Rast_open_new(globals->out_band, FCELL_TYPE);
  53. meanbuf = Rast_allocate_f_buf();
  54. /* goodness of fit for each cell: 1 = good fit, 0 = bad fit */
  55. /* similarity of each cell to region mean
  56. * max possible difference: globals->threshold
  57. * if similarity < globals->alpha * globals->alpha * globals->threshold
  58. * 1
  59. * else
  60. * (similarity - globals->alpha * globals->alpha * globals->threshold) /
  61. * (globals->threshold * (1 - globals->alpha * globals->alpha) */
  62. thresh = globals->alpha * globals->alpha * globals->max_diff;
  63. maxdev = globals->max_diff * (1 - globals->alpha * globals->alpha);
  64. mingood = 1;
  65. G_message(_("Writing out goodness of fit"));
  66. for (row = 0; row < globals->nrows; row++) {
  67. G_percent(row, globals->nrows, 9);
  68. Rast_set_f_null_value(meanbuf, globals->ncols);
  69. for (col = 0; col < globals->ncols; col++) {
  70. if (!(FLAG_GET(globals->null_flag, row, col))) {
  71. segment_get(&globals->rid_seg, (void *) &rid, row, col);
  72. if (rid > 0) {
  73. /* get values for Ri = this region */
  74. globals->rs.id = rid;
  75. fetch_reg_stats(Ri.row, Ri.col, &globals->rs, globals);
  76. Ri.mean = globals->rs.mean;
  77. Ri.count = globals->rs.count;
  78. sim = 0.;
  79. /* region consists of more than one cell */
  80. if (Ri.count > 1) {
  81. /* get values for Rk = this cell */
  82. segment_get(&globals->bands_seg,
  83. (void *)globals->second_val, row, col);
  84. Rk.mean = globals->second_val;
  85. /* calculate similarity */
  86. sim = (*globals->calculate_similarity) (&Ri, &Rk, globals);
  87. }
  88. if (0) {
  89. if (sim < thresh)
  90. meanbuf[col] = 1;
  91. else {
  92. sim = 1. - (sim - thresh) / maxdev;
  93. meanbuf[col] = sim;
  94. if (mingood > sim)
  95. mingood = sim;
  96. }
  97. }
  98. else {
  99. sim = 1 - sim / globals->max_diff;
  100. meanbuf[col] = sim;
  101. if (mingood > sim)
  102. mingood = sim;
  103. }
  104. }
  105. }
  106. }
  107. Rast_put_row(mean_fd, meanbuf, FCELL_TYPE);
  108. }
  109. Rast_close(mean_fd);
  110. Rast_init_colors(&colors);
  111. Rast_make_grey_scale_fp_colors(&colors, mingood, 1);
  112. Rast_write_colors(globals->out_band, G_mapset(), &colors);
  113. Rast_short_history(globals->out_band, "raster", &hist);
  114. Rast_command_history(&hist);
  115. Rast_write_history(globals->out_band, &hist);
  116. G_free(meanbuf);
  117. }
  118. /* free memory */
  119. G_free(outbuf);
  120. Rast_free_colors(&colors);
  121. return TRUE;
  122. }
  123. int close_files(struct globals *globals)
  124. {
  125. /* close segmentation files and output raster */
  126. G_debug(1, "closing files");
  127. segment_close(&globals->bands_seg);
  128. if (globals->bounds_map)
  129. segment_close(&globals->bounds_seg);
  130. G_free(globals->bands_val);
  131. G_free(globals->second_val);
  132. segment_close(&globals->rid_seg);
  133. flag_destroy(globals->null_flag);
  134. flag_destroy(globals->candidate_flag);
  135. /* anything else left to clean up? */
  136. return TRUE;
  137. }