fr.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. """
  2. Full-reference Image Quality Assessment (FR-IQA) methods
  3. """
  4. from skimage.measure import compare_ssim, compare_psnr
  5. from ipfml.exceptions import NumpyShapeComparisonException
  6. import numpy as np
  7. def _prepare_arrays(img_true, img_test, p_dtype='float32'):
  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=p_dtype)
  14. img_test = np.asarray(img_test, dtype=p_dtype)
  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 psnr(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(100).reshape(10, 10)
  88. >>> arr2 = np.arange(5, 105).reshape(10, 10)
  89. >>> psnr_score = fr.psnr(arr1, arr2)
  90. >>> int(psnr_score)
  91. 34
  92. """
  93. img_true, img_test = _prepare_arrays(img_true, img_test, 'uint8')
  94. return compare_psnr(img_true, img_test)
  95. def ssim(img_true, img_test):
  96. """Returns the computed Structural Similarity (SSIM) between two images
  97. Args:
  98. img_true: Image, numpy array of any dimension
  99. img_test: Image, numpy array of any dimension
  100. Returns:
  101. Computed SSIM score
  102. Example:
  103. >>> from ipfml.iqa import fr
  104. >>> import numpy as np
  105. >>> arr1 = np.arange(100).reshape(10, 10)
  106. >>> arr2 = np.arange(5, 105).reshape(10, 10)
  107. >>> ssim_score = fr.ssim(arr1, arr2)
  108. >>> int(ssim_score)
  109. 0
  110. """
  111. img_true, img_test = _prepare_arrays(img_true, img_test)
  112. if img_true.ndim == 3:
  113. return compare_ssim(img_true, img_test, multichannel=True)
  114. else:
  115. return compare_ssim(img_true, img_test)
  116. def ms_ssim(img_true, img_test):
  117. """
  118. Implemented later..
  119. """
  120. pass
  121. def vif(img_true, img_test):
  122. """
  123. Implemented later..
  124. """
  125. pass