Abhisek92

ImageFusion_Statistics.py

Mar 22nd, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.73 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import gdal
  5. import numpy
  6. import os
  7.  
  8.  
  9. def __main():
  10.     abspath = os.path.abspath(__file__)
  11.     dir_name = os.path.dirname(abspath)
  12.     os.chdir(dir_name)
  13.  
  14.     #Change The Filenames Accordingly
  15.     original = gdal.Open(r'aoi.img')
  16.     brovey = gdal.Open(r'brovey.img')
  17.     ehlers = gdal.Open(r'ehlers.img')
  18.     hpf = gdal.Open(r'hpf.img')
  19.     multi = gdal.Open(r'multiplicative.img')
  20.     pca = gdal.Open(r'pca.img')
  21.     wavelet = gdal.Open(r'wavelet.img')
  22.    
  23.     print(get_rmse(original, multi))
  24.     print(get_quality(original, brovey))
  25.     print(get_correlation(original, brovey))
  26.     print(get_relative_mean(original, brovey))
  27.     print(get_entropy(brovey))
  28.     print(get_std_dev(original))
  29.  
  30.  
  31. def get_rmse(orig, ref):
  32.     try:
  33.         if orig.RasterCount == ref.RasterCount:
  34.             count = orig.RasterCount
  35.             rmse_vector = list()
  36.             for i in range(count):
  37.                 current_band = (numpy.array(orig.GetRasterBand(i+1).ReadAsArray())).astype(float)
  38.                 ref_band = (numpy.array(ref.GetRasterBand(i+1).ReadAsArray())).astype(float)
  39.                 rmse_vector.append(numpy.sqrt(((numpy.subtract(current_band, ref_band)) ** 2).mean()))
  40.             return rmse_vector
  41.         else:
  42.             raise IOError("No of Bands does not match")
  43.     except IOError as dim_error:
  44.         print(dim_error)
  45.         return None
  46.  
  47.  
  48. def covarience(orig, ref):
  49.     try:
  50.         if orig.shape == ref.shape:
  51.             return ((numpy.sum(numpy.multiply((orig - orig.mean()), (ref - ref.mean())))).prod()) / (ref.size - 1)
  52.         else:
  53.             raise IOError("Dimension Mismatch")
  54.     except IOError as dim_error:
  55.         print(dim_error)
  56.         return None
  57.  
  58.  
  59. def quality(orig, ref):
  60.     try:
  61.         if orig.shape == ref.shape:
  62.             numerator = 4 * covarience(orig, ref) * (orig.mean() * ref.mean())
  63.             denominator = ((orig.mean()**2) + ((ref.mean()**2))) * ((numpy.var(orig.ravel())) + (numpy.var(ref.ravel())))
  64.             return numerator / denominator
  65.         else:
  66.             raise IOError("Dimension Mismatch")
  67.     except IOError as dim_error:
  68.         print(dim_error)
  69.         return None
  70.  
  71.  
  72. def get_covariance(orig, ref):
  73.     try:
  74.         if orig.RasterCount == ref.RasterCount:
  75.             count = orig.RasterCount
  76.             cov_vector = list()
  77.             for i in range(count):
  78.                 current_band = (numpy.array(orig.GetRasterBand(i+1).ReadAsArray())).astype(float)
  79.                 ref_band = (numpy.array(ref.GetRasterBand(i+1).ReadAsArray())).astype(float)
  80.                 cov_vector.append(covarience(current_band, ref_band))
  81.             return cov_vector
  82.         else:
  83.             raise IOError("No of Bands does not match")
  84.     except IOError as dim_error:
  85.         print(dim_error)
  86.         return None
  87.  
  88.  
  89. def get_quality(orig, ref):
  90.     try:
  91.         if orig.RasterCount == ref.RasterCount:
  92.             count = orig.RasterCount
  93.             quality_vector = list()
  94.             for i in range(count):
  95.                 current_band = (numpy.array(orig.GetRasterBand(i+1).ReadAsArray())).astype(float)
  96.                 ref_band = (numpy.array(ref.GetRasterBand(i+1).ReadAsArray())).astype(float)
  97.                 quality_vector.append(quality(current_band, ref_band))
  98.             return quality_vector
  99.            
  100.         else:
  101.             raise IOError("No of Bands does not match")
  102.     except IOError as dim_error:
  103.         print(dim_error)
  104.         return None
  105.  
  106.      
  107. def correlation(orig, ref):
  108.     try:
  109.         if orig.shape == ref.shape:
  110.                         numerator = (2 * numpy.sum(numpy.multiply(orig, ref)))
  111.                         denominator = (numpy.sum((orig**2)) + numpy.sum((ref**2)))
  112.                         return numerator / denominator
  113.         else:
  114.             raise IOError("Dimension Mismatch")
  115.     except IOError as dim_error:
  116.         print(dim_error)
  117.         return None
  118.  
  119.  
  120. def get_correlation(orig, ref):
  121.     try:
  122.         if orig.RasterCount == ref.RasterCount:
  123.             count = orig.RasterCount
  124.             correlation_vector = list()
  125.             for i in range(count):
  126.                 current_band = (numpy.array(orig.GetRasterBand(i+1).ReadAsArray())).astype(float)
  127.                 ref_band = (numpy.array(ref.GetRasterBand(i+1).ReadAsArray())).astype(float)
  128.                 correlation_vector.append(correlation(current_band, ref_band))
  129.             return correlation_vector
  130.            
  131.         else:
  132.             raise IOError("No of Bands does not match")
  133.     except IOError as dim_error:
  134.         print(dim_error)
  135.         return None
  136.  
  137.  
  138. def relative_mean(orig, ref):
  139.     try:
  140.         if orig.shape == ref.shape:
  141.             return ((ref.mean() - orig.mean()) * 100) / ref.mean()
  142.         else:
  143.             raise IOError("Dimension Mismatch")
  144.     except IOError as dim_error:
  145.         print(dim_error)
  146.         return None
  147.  
  148.  
  149. def get_relative_mean(orig, ref):
  150.     try:
  151.         if orig.RasterCount == ref.RasterCount:
  152.             count = orig.RasterCount
  153.             relative_vector = list()
  154.             for i in range(count):
  155.                 current_band = (numpy.array(orig.GetRasterBand(i+1).ReadAsArray())).astype(float)
  156.                 ref_band = (numpy.array(ref.GetRasterBand(i+1).ReadAsArray())).astype(float)
  157.                 relative_vector.append(relative_mean(current_band, ref_band))
  158.             return relative_vector
  159.            
  160.         else:
  161.             raise IOError("No of Bands does not match")
  162.     except IOError as dim_error:
  163.         print(dim_error)
  164.         return None
  165.  
  166.  
  167. def entropy(img):
  168.     try:
  169.         size_dict = {1:8, 2:16, 3:32}
  170.         if img.DataType in (1, 2, 4):
  171.             img_matrix = (numpy.array(img.ReadAsArray())).astype(float)
  172.             upper = (2 ** size_dict[img.DataType]) - 1
  173.             p_dist = numpy.histogram(img_matrix, bins=upper, range=(0, upper), density=True)[0]
  174.             return -1 * numpy.sum(numpy.multiply(p_dist, numpy.log2(p_dist, out=numpy.zeros_like(p_dist, dtype=numpy.float), where=p_dist > 0)))
  175.         else:
  176.             raise TypeError("Only Unsigned Integer Datatypes are Supported")
  177.     except TypeError as type_error:
  178.         print(type_error)
  179.         return None
  180.  
  181.  
  182. def get_entropy(orig):  
  183.     count = orig.RasterCount
  184.     entropy_vector = list()
  185.     for i in range(count):
  186.         current_band = orig.GetRasterBand(i+1)
  187.         entropy_vector.append(entropy(current_band))
  188.     return entropy_vector
  189.  
  190.  
  191. def get_std_dev(img):
  192.     count = img.RasterCount
  193.     sd_vector = list()
  194.     for i in range(count):
  195.         current_band = (numpy.array(img.GetRasterBand(i+1).ReadAsArray())).astype(float)
  196.         sd_vector.append(numpy.std(current_band))
  197.     return sd_vector
  198.  
  199. if __name__ == '__main__':
  200.     __main()
Add Comment
Please, Sign In to add comment