oif.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import numpy as np
  15. import pandas as pd
  16. import itertools
  17. import argparse
  18. from utils import Raster, Timer
  19. from easydict import EasyDict as edict
  20. def _calcOIF(rgb, stds, rho):
  21. r, g, b = rgb
  22. s1 = stds[int(r)]
  23. s2 = stds[int(g)]
  24. s3 = stds[int(b)]
  25. r12 = rho[int(r), int(g)]
  26. r23 = rho[int(g), int(b)]
  27. r31 = rho[int(b), int(r)]
  28. return (s1 + s2 + s3) / (abs(r12) + abs(r23) + abs(r31))
  29. @Timer
  30. def oif(img_path, topk=5):
  31. raster = Raster(img_path)
  32. img = raster.getArray()
  33. img_flatten = img.reshape([-1, raster.bands])
  34. stds = np.std(img_flatten, axis=0)
  35. datas = edict()
  36. for c in range(raster.bands):
  37. datas[str(c + 1)] = img_flatten[:, c]
  38. datas = pd.DataFrame(datas)
  39. rho = datas.corr().values
  40. band_combs = edict()
  41. for rgb in itertools.combinations(list(range(raster.bands)), 3):
  42. band_combs[str(rgb)] = _calcOIF(rgb, stds, rho)
  43. band_combs = sorted(
  44. band_combs.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
  45. print("== Optimal band combination ==")
  46. for i in range(topk):
  47. k, v = band_combs[i]
  48. print("Bands: {0}, OIF value: {1}.".format(k, v))
  49. parser = argparse.ArgumentParser(description="input parameters")
  50. parser.add_argument("--im_path", type=str, required=True, \
  51. help="The path of HSIs image.")
  52. parser.add_argument("--topk", type=int, default=5, \
  53. help="Number of top results, `5` is the default.")
  54. if __name__ == "__main__":
  55. args = parser.parse_args()
  56. oif(args.im_path, args.topk)