fr.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from skimage.measure import compare_ssim, compare_psnr
  2. from ipfml.exceptions import NumpyShapeComparisonException
  3. import numpy as np
  4. """
  5. Full-reference Image Quality Assessment (FR-IQA) methods
  6. """
  7. def _prepare_arrays(img_true, img_test):
  8. """
  9. Prepare image data
  10. Raises:
  11. NumpyShapeComparisonException: if shape of images are not the same
  12. """
  13. img_true = np.asarray(img_true, dtype='float32')
  14. img_test = np.asarray(img_test, dtype='float32')
  15. if img_true.shape != img_test.shape:
  16. raise NumpyShapeComparisonException
  17. return img_true, img_test
  18. def mse(img_true, img_test):
  19. """Returns Mean-Squared Error score between two Numpy arrays
  20. Args:
  21. img_true: Image, numpy array of any dimension
  22. img_test: Image, numpy array of any dimension
  23. Returns:
  24. Computed MSE score
  25. Raises:
  26. NumpyShapeComparisonException: if shape of images are not the same
  27. Example:
  28. >>> from ipfml.iqa import fr
  29. >>> import numpy as np
  30. >>> arr1 = np.arange(10)
  31. >>> arr2 = np.arange(5, 15)
  32. >>> mse_score = fr.mse(arr1, arr2)
  33. >>> mse_score
  34. 25.0
  35. """
  36. img_true, img_test = _prepare_arrays(img_true, img_test)
  37. return np.mean(np.square(img_true - img_test), dtype=np.float64)
  38. def rmse(img_true, img_test):
  39. """Returns Root Mean-Squared Error score between two Numpy arrays
  40. Args:
  41. img_true: Image, numpy array of any dimension
  42. img_test: Image, numpy array of any dimension
  43. Returns:
  44. Computed RMSE score
  45. Raises:
  46. NumpyShapeComparisonException: if shape of images are not the same
  47. Example:
  48. >>> from ipfml.iqa import fr
  49. >>> import numpy as np
  50. >>> arr1 = np.arange(10)
  51. >>> arr2 = np.arange(5, 15)
  52. >>> rmse_score = fr.rmse(arr1, arr2)
  53. >>> rmse_score
  54. 5.0
  55. """
  56. return np.sqrt(mse(img_true, img_test))
  57. def mae(img_true, img_test):
  58. """Returns Mean Absolute Error between two Numpy arrays
  59. Args:
  60. img_true: Image, numpy array of any dimension
  61. img_test: Image, numpy array of any dimension
  62. Returns:
  63. Computed MAE score
  64. Raises:
  65. NumpyShapeComparisonException: if shape of images are not the same
  66. Example:
  67. >>> from ipfml.iqa import fr
  68. >>> import numpy as np
  69. >>> arr1 = np.arange(10)
  70. >>> arr2 = np.arange(5, 15)
  71. >>> mae_score = fr.mae(arr1, arr2)
  72. >>> mae_score
  73. 5.0
  74. """
  75. img_true, img_test = _prepare_arrays(img_true, img_test)
  76. return np.mean(np.absolute(img_true - img_test), dtype=np.float64)
  77. def pnsr(img_true, img_test):
  78. """Returns the computed Peak Signal to Noise Ratio (PSNR) between two images
  79. Args:
  80. img_true: Image, numpy array of any dimension
  81. img_test: Image, numpy array of any dimension
  82. Returns:
  83. Computed PSNR score
  84. Example:
  85. >>> from ipfml.iqa import fr
  86. >>> import numpy as np
  87. >>> arr1 = np.arange(10)
  88. >>> arr2 = np.arange(5, 15)
  89. >>> pnsr_score = fr.pnsr(arr1, arr2)
  90. >>> int(pnsr_score)
  91. 365
  92. """
  93. return compare_psnr(img_true, img_test)
  94. def ms_ssim(img_true, img_test):
  95. """
  96. Implemented later..
  97. """
  98. pass
  99. def vif(img_true, img_test):
  100. """
  101. Implemented later..
  102. """
  103. pass