|
| 1 | +#Video Playlist: https://www.youtube.com/playlist?list=PLHae9ggVvqPgyRQQOtENr6hK0m1UquGaG |
| 2 | + |
| 3 | +""" |
| 4 | +Older versions of scikit-image: older than 0.16 |
| 5 | +https://scikit-image.org/docs/dev/api/skimage.measure.html |
| 6 | +
|
| 7 | +Newer scikit-image: 0.17 or later |
| 8 | +https://scikit-image.org/docs/stable/api/skimage.metrics.html |
| 9 | +
|
| 10 | +https://pypi.org/project/sewar/ |
| 11 | +https://sewar.readthedocs.io/en/latest/_modules/sewar/full_ref.html#ergas |
| 12 | +
|
| 13 | +""" |
| 14 | + |
| 15 | +import cv2 |
| 16 | +import numpy as np |
| 17 | +from sewar import full_ref |
| 18 | +from skimage import measure, metrics |
| 19 | + |
| 20 | + |
| 21 | +#Reference and image to be compared must be of the same size |
| 22 | +ref_img = cv2.imread("images/noisy_images/sandstone.tif", 1) |
| 23 | +img = cv2.imread("images/noisy_images/sandstone_25sigma_noisy.tif", 1) |
| 24 | + |
| 25 | +################################################################ |
| 26 | +#skimage tools |
| 27 | +#Mean square error |
| 28 | +#Older skimage versions, older than 0.17: skimage.measure.compare_mse |
| 29 | +#skimage.metrics.mean_squared_error |
| 30 | + |
| 31 | +mse_skimg = metrics.mean_squared_error(ref_img, img) |
| 32 | +print("MSE: based on scikit-image = ", mse_skimg) |
| 33 | + |
| 34 | +#Same as PSNR available in sewar |
| 35 | +#Older versions of skimage: skimage.measure.compare_psnr |
| 36 | +#skimage.metrics.peak_signal_noise_ratio |
| 37 | +psnr_skimg = metrics.peak_signal_noise_ratio(ref_img, img, data_range=None) |
| 38 | +print("PSNR: based on scikit-image = ", psnr_skimg) |
| 39 | + |
| 40 | +#Normalized root mean squared error |
| 41 | +#Older versions of skimage: skimage.measure.compare_nrmse |
| 42 | +rmse_skimg = metrics.normalized_root_mse(ref_img, img) |
| 43 | +print("RMSE: based on scikit-image = ", rmse_skimg) |
| 44 | + |
| 45 | +from skimage.metrics import structural_similarity as ssim |
| 46 | +ssim_skimg = ssim(ref_img, img, |
| 47 | + data_range = img.max() - img.min(), |
| 48 | + multichannel = True) |
| 49 | +print("SSIM: based on scikit-image = ", ssim_skimg) |
| 50 | + |
| 51 | +############################################################### |
| 52 | +#ERGAS Global relative error |
| 53 | +"""calculates global relative error |
| 54 | +GT: first (original) input image. |
| 55 | +P: second (deformed) input image. |
| 56 | +r: ratio of high resolution to low resolution (default=4). |
| 57 | +ws: sliding window size (default = 8). |
| 58 | +
|
| 59 | + :returns: float -- ergas value. |
| 60 | + """ |
| 61 | +ergas_img = full_ref.ergas(ref_img, img, r=4, ws=8) |
| 62 | +print("EGRAS: global relative error = ", ergas_img) |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +#################################################################### |
| 67 | +#Multiscale structural similarity index |
| 68 | +"""calculates multi-scale structural similarity index (ms-ssim). |
| 69 | +
|
| 70 | + :param GT: first (original) input image. |
| 71 | + :param P: second (deformed) input image. |
| 72 | + :param weights: weights for each scale (default = [0.0448, 0.2856, 0.3001, 0.2363, 0.1333]). |
| 73 | + :param ws: sliding window size (default = 11). |
| 74 | + :param K1: First constant for SSIM (default = 0.01). |
| 75 | + :param K2: Second constant for SSIM (default = 0.03). |
| 76 | + :param MAX: Maximum value of datarange (if None, MAX is calculated using image dtype). |
| 77 | +
|
| 78 | + :returns: float -- ms-ssim value. |
| 79 | + """ |
| 80 | +msssim_img=full_ref.msssim(ref_img, img, weights=[0.0448, 0.2856, 0.3001, 0.2363, 0.1333], ws=11, K1=0.01, K2=0.03, MAX=None) |
| 81 | + |
| 82 | +print("MSSSIM: multi-scale structural similarity index = ", msssim_img) |
| 83 | + |
| 84 | + |
| 85 | +############################################################################## |
| 86 | +#PSNR |
| 87 | +"""calculates peak signal-to-noise ratio (psnr). |
| 88 | +
|
| 89 | + :param GT: first (original) input image. |
| 90 | + :param P: second (deformed) input image. |
| 91 | + :param MAX: maximum value of datarange (if None, MAX is calculated using image dtype). |
| 92 | +
|
| 93 | + :returns: float -- psnr value in dB. |
| 94 | + """ |
| 95 | +psnr_img=full_ref.psnr(ref_img, img, MAX=None) |
| 96 | + |
| 97 | +print("PSNR: peak signal-to-noise ratio = ", psnr_img) |
| 98 | + |
| 99 | + |
| 100 | +########################################################################## |
| 101 | +#PSNRB: Calculates PSNR with Blocking Effect Factor for a given pair of images (PSNR-B) |
| 102 | +"""Calculates PSNR with Blocking Effect Factor for a given pair of images (PSNR-B) |
| 103 | +
|
| 104 | + :param GT: first (original) input image in YCbCr format or Grayscale. |
| 105 | + :param P: second (corrected) input image in YCbCr format or Grayscale.. |
| 106 | + :return: float -- psnr_b. |
| 107 | + """ |
| 108 | +#psnrb_img = full_ref.psnrb(ref_img, img) |
| 109 | + |
| 110 | +#print("PSNRB: peak signal-to-noise ratio with blocking effect = ", psnrb_img) |
| 111 | + |
| 112 | +####################################################################### |
| 113 | +#relative average spectral error (rase) |
| 114 | +"""calculates relative average spectral error (rase). |
| 115 | +
|
| 116 | + :param GT: first (original) input image. |
| 117 | + :param P: second (deformed) input image. |
| 118 | + :param ws: sliding window size (default = 8). |
| 119 | +
|
| 120 | + :returns: float -- rase value. |
| 121 | + """ |
| 122 | +RASE_img = full_ref.rase(ref_img, img, ws=8) |
| 123 | +#print("RASE: relative average spectral error = ", RASE_img) |
| 124 | + |
| 125 | + |
| 126 | +###################################################################### |
| 127 | +#RMSE |
| 128 | +"""calculates root mean squared error (rmse). |
| 129 | +
|
| 130 | + :param GT: first (original) input image. |
| 131 | + :param P: second (deformed) input image. |
| 132 | +
|
| 133 | + :returns: float -- rmse value. |
| 134 | + """ |
| 135 | +rmse_img = full_ref.rmse(ref_img, img) |
| 136 | +print("RMSE: root mean squared error = ", rmse_img) |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +###################################################################### |
| 141 | +#root mean squared error (rmse) using sliding window |
| 142 | +"""calculates root mean squared error (rmse) using sliding window. |
| 143 | +
|
| 144 | + :param GT: first (original) input image. |
| 145 | + :param P: second (deformed) input image. |
| 146 | + :param ws: sliding window size (default = 8). |
| 147 | +
|
| 148 | + :returns: tuple -- rmse value,rmse map. |
| 149 | + """ |
| 150 | +rmse_sw_img = full_ref.rmse_sw(ref_img, img, ws=8) |
| 151 | +#print("RMSE_SW: root mean squared error with sliding window = ", rmse_sw_img) |
| 152 | + |
| 153 | + |
| 154 | +######################################################################### |
| 155 | +#calculates spectral angle mapper (sam). |
| 156 | +"""calculates spectral angle mapper (sam). |
| 157 | +
|
| 158 | + :param GT: first (original) input image. |
| 159 | + :param P: second (deformed) input image. |
| 160 | +
|
| 161 | + :returns: float -- sam value. |
| 162 | + """ |
| 163 | +ref_sam_img = full_ref.sam(ref_img, img) |
| 164 | +print("REF_SAM: spectral angle mapper = ", ref_sam_img) |
| 165 | + |
| 166 | + |
| 167 | +###################################################################### |
| 168 | +#Spatial correlation coefficient |
| 169 | +#full_ref.scc(ref_img, img, win=[[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], ws=8) |
| 170 | + |
| 171 | +#Structural similarity index |
| 172 | +"""calculates structural similarity index (ssim). |
| 173 | +
|
| 174 | + :param GT: first (original) input image. |
| 175 | + :param P: second (deformed) input image. |
| 176 | + :param ws: sliding window size (default = 8). |
| 177 | + :param K1: First constant for SSIM (default = 0.01). |
| 178 | + :param K2: Second constant for SSIM (default = 0.03). |
| 179 | + :param MAX: Maximum value of datarange (if None, MAX is calculated using image dtype). |
| 180 | +
|
| 181 | + :returns: tuple -- ssim value, cs value. |
| 182 | + """ |
| 183 | +ssim_img = full_ref.ssim(ref_img, img, ws=11, K1=0.01, K2=0.03, MAX=None, fltr_specs=None, mode='valid') |
| 184 | +print("SSIM: structural similarity index = ", ssim_img) |
| 185 | + |
| 186 | +############################################################################## |
| 187 | +#Universal image quality index |
| 188 | +"""calculates universal image quality index (uqi). |
| 189 | +
|
| 190 | + :param GT: first (original) input image. |
| 191 | + :param P: second (deformed) input image. |
| 192 | + :param ws: sliding window size (default = 8). |
| 193 | +
|
| 194 | + :returns: float -- uqi value. |
| 195 | + """ |
| 196 | +UQI_img = full_ref.uqi(ref_img, img, ws=8) |
| 197 | +print("UQI: universal image quality index = ", UQI_img) |
| 198 | + |
| 199 | +############################################################################## |
| 200 | +#Pixel Based Visual Information Fidelity (vif-p) |
| 201 | +"""calculates Pixel Based Visual Information Fidelity (vif-p). |
| 202 | +
|
| 203 | + :param GT: first (original) input image. |
| 204 | + :param P: second (deformed) input image. |
| 205 | + :param sigma_nsq: variance of the visual noise (default = 2) |
| 206 | +
|
| 207 | + :returns: float -- vif-p value. |
| 208 | + """ |
| 209 | +VIFP_img = full_ref.vifp(ref_img, img, sigma_nsq=2) |
| 210 | +print("VIFP: Pixel Based Visual Information Fidelity = ", VIFP_img) |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + |
0 commit comments