i.oif.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: i.oif
  5. # AUTHOR(S): Markus Neteler
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: calculates the Optimum Index factor of all band combinations
  8. # for LANDSAT TM 1,2,3,4,5,7
  9. # COPYRIGHT: (C) 1999,2008 by the GRASS Development Team
  10. #
  11. # This program is free software under the GNU General Public
  12. # License (>=v2). Read the file COPYING that comes with GRASS
  13. # for details.
  14. #
  15. #############################################################################
  16. # Ref.: Jensen: Introductory digital image processing 1996, p.98
  17. #
  18. # Input: tm1 - tm5, tm7 (not tm6)
  19. #
  20. # written by Markus Neteler 21.July 1998
  21. # neteler geog.uni-hannover.de
  22. # updated for GRASS 5.7 by Michael Barton 2004/04/05
  23. #% Module
  24. #% description: Calculates Optimum-Index-Factor table for LANDSAT TM bands 1-5, & 7
  25. #% keywords: raster
  26. #% keywords: imagery
  27. #% keywords: statistics
  28. #% End
  29. #% option G_OPT_R_INPUT
  30. #% key: image1
  31. #% description: LANDSAT TM band 1.
  32. #% end
  33. #% option G_OPT_R_INPUT
  34. #% key: image2
  35. #% description: LANDSAT TM band 2.
  36. #% end
  37. #% option G_OPT_R_INPUT
  38. #% key: image3
  39. #% description: LANDSAT TM band 3.
  40. #% end
  41. #% option G_OPT_R_INPUT
  42. #% key: image4
  43. #% description: LANDSAT TM band 4.
  44. #% end
  45. #% option G_OPT_R_INPUT
  46. #% key: image5
  47. #% description: LANDSAT TM band 5.
  48. #% end
  49. #% option G_OPT_R_INPUT
  50. #% key: image7
  51. #% description: LANDSAT TM band 7.
  52. #% end
  53. #% Flag
  54. #% key: g
  55. #% description: Print in shell script style
  56. #% End
  57. import sys
  58. import os
  59. from grass.script import core as grass
  60. bands = [1,2,3,4,5,7]
  61. def oifcalc(sdev, corr, k1, k2, k3):
  62. # calculate SUM of Stddeviations:
  63. ssdev = [sdev[k1], sdev[k2], sdev[k3]]
  64. numer = sum(ssdev)
  65. # calculate SUM of absolute(Correlation values):
  66. scorr = [corr[k1,k2], corr[k1,k3], corr[k2,k3]]
  67. denom = sum(map(abs, scorr))
  68. # Calculate OIF index:
  69. # Divide (SUM of Stddeviations) and (SUM of Correlation)
  70. return numer / denom
  71. def perms():
  72. for i in range(0,4):
  73. for j in range(i+1,5):
  74. for k in range(j+1,6):
  75. yield (bands[i], bands[j], bands[k])
  76. def main():
  77. shell = flags['g']
  78. image = {}
  79. for band in bands:
  80. image[band] = options['image%d' % band]
  81. # calculate the Stddev for TM bands
  82. grass.message(_("Calculating Standard deviations for all bands..."))
  83. stddev = {}
  84. for band in bands:
  85. grass.verbose("band %d" % band)
  86. s = grass.read_command('r.univar', flags = 'g', map = image[band])
  87. kv = grass.parse_key_val(s)
  88. stddev[band] = float(kv['stddev'])
  89. grass.message(_("Calculating Correlation Matrix..."))
  90. correlation = {}
  91. s = grass.read_command('r.covar', flags = 'r', map = [image[band] for band in bands])
  92. for i, row in zip(bands, s.splitlines()):
  93. for j, cell in zip(bands, row.split(' ')):
  94. correlation[i,j] = float(cell)
  95. # Calculate all combinations
  96. grass.message(_("Calculating OIF for the 20 band combinations..."))
  97. oif = []
  98. for p in perms():
  99. oif.append((oifcalc(stddev, correlation, *p), p))
  100. oif.sort(reverse = True)
  101. grass.verbose(_("The Optimum Index Factor analysis result "
  102. "(Best combination comes first):"))
  103. if shell:
  104. fmt = "%d%d%d:%f\n"
  105. else:
  106. fmt = "%d%d%d: %f\n"
  107. outf = file('i.oif.result', 'w')
  108. for v, p in oif:
  109. sys.stdout.write(fmt % (p + (v,)))
  110. outf.write(fmt % (p + (v,)))
  111. outf.close()
  112. if __name__ == "__main__":
  113. options, flags = grass.parser()
  114. main()