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