a ¡ßek!ã@sddlZddlZddlZddlZddlmZddlmZddl m Z m Z ddl m Z ddlmZdd„Zd d „Zdd d „Ze ¡ddd„ƒZdS)éN)Úconvolve)Úgamma)Ú reorder_imageÚ to_y_channel)Úimresize)ÚMETRIC_REGISTRYc Cs8| ¡}t ddd¡}t |¡}t t|dƒ¡t|ƒt|dƒ}t t ||dkd¡¡}t t ||dkd¡¡}||}t t |¡¡dt |d¡}||dd|d|ddd}t  ||d¡} || } |t td| ƒtd| ƒ¡} |t td| ƒtd| ƒ¡} | | | fS)a*Estimate AGGD (Asymmetric Generalized Gaussian Distribution) parameters. Args: block (ndarray): 2D Image block. Returns: tuple: alpha (float), beta_l (float) and beta_r (float) for the AGGD distribution (Estimating the parames in Equation 7 in the paper). gš™™™™™É?g—nƒ$@gü©ñÒMbP?ééré) ÚflattenÚnpÚarangeÚ reciprocalÚsquarerÚsqrtÚmeanÚabsÚargmin) ÚblockZgamZgam_reciprocalZr_gamZleft_stdZ right_stdZgammahatZrhatZrhatnormZarray_positionÚalphaÚbeta_lÚbeta_r©rú$D:\face swap\basicsr\metrics\niqe.pyÚestimate_aggd_param s  &"(""rc Cs²g}t|ƒ\}}}| |||dg¡ddgddgddgddgg}tt|ƒƒD]\}tj|||dd}t||ƒ\}}}||td|ƒtd|ƒ}| ||||g¡qP|S)z‚Compute features. Args: block (ndarray): 2D Image block. Returns: list: Features with length of 18. rrr éÿÿÿÿ)rr ©Úaxis)rÚextendÚrangeÚlenr Úrollr) rÚfeatrrrÚshiftsÚiZ shifted_blockrrrrÚcompute_feature)s  r%é`c CsÚ|jdksJdƒ‚|j\}}t ||¡}t ||¡} |d||…d| |…f}g} dD]è} t||dd} t t tt |¡|ddt | ¡¡¡} || | d}g}t | ƒD]\}t |ƒD]N}|||| |d|| …||| |d|| …f}|  t |ƒ¡qÀq´|   t  |¡¡| dkr\t |dd d d }|d}q\tj| dd } tj| dd }| t | ¡jdd }tj|d d}tj ||d¡}t t |||¡t ||¡¡}t |¡}tt |¡ƒ}|S)a±Calculate NIQE (Natural Image Quality Evaluator) metric. ``Paper: Making a "Completely Blind" Image Quality Analyzer`` This implementation could produce almost the same results as the official MATLAB codes: http://live.ece.utexas.edu/research/quality/niqe_release.zip Note that we do not include block overlap height and width, since they are always 0 in the official implementation. For good performance, it is advisable by the official implementation to divide the distorted image in to the same size patched as used for the construction of multivariate Gaussian model. Args: img (ndarray): Input image whose quality needs to be computed. The image must be a gray or Y (of YCbCr) image with shape (h, w). Range [0, 255] with float type. mu_pris_param (ndarray): Mean of a pre-defined multivariate Gaussian model calculated on the pristine dataset. cov_pris_param (ndarray): Covariance of a pre-defined multivariate Gaussian model calculated on the pristine dataset. gaussian_window (ndarray): A 7x7 Gaussian window used for smoothing the image. block_size_h (int): Height of the blocks in to which image is divided. Default: 96 (the official recommended value). block_size_w (int): Width of the blocks in to which image is divided. Default: 96 (the official recommended value). rzCInput image must be a gray or Y (of YCbCr) image with shape (h, w).r)r rÚnearest)Úmoder çào@gà?T)ÚscaleZ antialiasingrF)Úrowvar)ÚndimÚshapeÚmathÚfloorrr rrrrÚappendr%ÚarrayrÚ concatenateÚnanmeanÚisnanÚanyÚcovÚlinalgÚpinvÚmatmulÚ transposeÚfloatÚsqueeze)ÚimgÚ mu_pris_paramÚcov_pris_paramÚgaussian_windowZ block_size_hZ block_size_wÚhÚwZ num_block_hZ num_block_wZ distparamr*ÚmuÚsigmaZ img_nomalizedr"Zidx_wZidx_hrZ mu_distparamZdistparam_no_nanZ cov_distparamZ invcov_paramÚqualityrrrÚniqeDs@ *  ÿ ÿ rFÚHWCÚyc KsÔtj tj t¡¡}t tj |d¡¡}|d}|d}|d} | tj ¡}|dkršt ||d}|dkrrt |ƒ}n|dkrt   |d t j¡d }t |¡}|d krº||| …|| …f}| ¡}t|||| ƒ} | S) aMCalculate NIQE (Natural Image Quality Evaluator) metric. ``Paper: Making a "Completely Blind" Image Quality Analyzer`` This implementation could produce almost the same results as the official MATLAB codes: http://live.ece.utexas.edu/research/quality/niqe_release.zip > MATLAB R2021a result for tests/data/baboon.png: 5.72957338 (5.7296) > Our re-implementation result for tests/data/baboon.png: 5.7295763 (5.7296) We use the official params estimated from the pristine dataset. We use the recommended block size (96, 96) without overlaps. Args: img (ndarray): Input image whose quality needs to be computed. The input image must be in range [0, 255] with float/int type. The input_order of image can be 'HW' or 'HWC' or 'CHW'. (BGR order) If the input order is 'HWC' or 'CHW', it will be converted to gray or Y (of YCbCr) image according to the ``convert_to`` argument. crop_border (int): Cropped pixels in each edge of an image. These pixels are not involved in the metric calculation. input_order (str): Whether the input order is 'HW', 'HWC' or 'CHW'. Default: 'HWC'. convert_to (str): Whether converted to 'y' (of MATLAB YCbCr) or 'gray'. Default: 'y'. Returns: float: NIQE result. zniqe_pris_params.npzr>r?r@ÚHW)Ú input_orderrHÚgrayr)r)ÚosÚpathÚdirnameÚabspathÚ__file__r ÚloadÚjoinÚastypeÚfloat32rrÚcv2ÚcvtColorÚCOLOR_BGR2GRAYr<ÚroundrF) r=Ú crop_borderrJÚ convert_toÚkwargsZROOT_DIRZniqe_pris_paramsr>r?r@Z niqe_resultrrrÚcalculate_niqes$    r\)r&r&)rGrH)rUr.Únumpyr rLÚ scipy.ndimagerÚ scipy.specialrZbasicsr.metrics.metric_utilrrZbasicsr.utils.matlab_functionsrÚbasicsr.utils.registryrrr%rFÚregisterr\rrrrÚs     L