fr.py 4.2 KB

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