128 lines
4.1 KiB
Python
128 lines
4.1 KiB
Python
'''
|
|
Filename: traditional.py
|
|
Path: /home/netoa/Desktop/ISR/metrics/objective/no_reference
|
|
Created Date: Wednesday May 21st 2025
|
|
Author: Andre Neto
|
|
-----
|
|
Last Modified: Wed May 21 2025
|
|
Modified By: Andre Neto
|
|
-----
|
|
Copyright (c) 2025 Institute of Systems and Robotics
|
|
'''
|
|
|
|
|
|
"""
|
|
This module contains functions for computing the NIQE (Natural Image Quality Evaluator),
|
|
a no-reference image quality assessment metric based on statistical features
|
|
of natural scene statistics.
|
|
|
|
Functions:
|
|
- compute_all: Computes NIQE for a given distorted image.
|
|
- load_grayscale_image: Loads an image and converts it to grayscale float.
|
|
- extract_features: Extracts normalized statistical features from image patches.
|
|
- compute_niqe: Computes the NIQE score based on the pristine model.
|
|
"""
|
|
|
|
|
|
from time import time as now
|
|
import numpy as np
|
|
import cv2
|
|
from skimage import io, img_as_float
|
|
from skimage.util import view_as_windows
|
|
from skimage.data import load as skimage_load
|
|
from skimage.feature import greycomatrix, greycoprops
|
|
from sklearn.svm import SVR
|
|
from joblib import load as joblib_load
|
|
import os
|
|
|
|
|
|
def compute_all(_, dist_path):
|
|
"""
|
|
Computes NIQE score for a distorted image.
|
|
Parameters:
|
|
_ (str): Placeholder for compatibility with other compute_all functions.
|
|
dist_path (str): Path to the distorted image.
|
|
Returns:
|
|
dict: Dictionary with NIQE score and computation time.
|
|
"""
|
|
image = load_grayscale_image(dist_path)
|
|
mu_prisparam, cov_prisparam = skimage_load("niqe_model").values()
|
|
|
|
start_time = now()
|
|
score = compute_niqe(image, mu_prisparam, cov_prisparam)
|
|
return {"NIQE": (score, now() - start_time)}
|
|
|
|
|
|
def load_grayscale_image(path):
|
|
"""
|
|
Loads an image and converts it to grayscale floating point format.
|
|
"""
|
|
image = io.imread(path)
|
|
if image.ndim == 3:
|
|
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
|
return img_as_float(image)
|
|
|
|
|
|
def extract_features(image, patch_size=96):
|
|
"""
|
|
Extracts normalized statistical features from non-overlapping patches.
|
|
"""
|
|
h, w = image.shape
|
|
image = image[:h - h % patch_size, :w - w % patch_size]
|
|
patches = view_as_windows(image, (patch_size, patch_size), step=patch_size)
|
|
patches = patches.reshape(-1, patch_size, patch_size)
|
|
|
|
features = []
|
|
for patch in patches:
|
|
mu = np.mean(patch)
|
|
sigma = np.std(patch)
|
|
if sigma == 0:
|
|
continue
|
|
normalized = (patch - mu) / sigma
|
|
features.append([np.mean(normalized), np.var(normalized)])
|
|
return np.array(features)
|
|
|
|
|
|
def compute_niqe(image, mu_prisparam, cov_prisparam, patch_size=96):
|
|
"""
|
|
Computes the NIQE score using a multivariate Gaussian model.
|
|
"""
|
|
features = extract_features(image, patch_size)
|
|
if features.shape[0] == 0:
|
|
return np.nan
|
|
|
|
mu_test = np.mean(features, axis=0)
|
|
cov_test = np.cov(features, rowvar=False)
|
|
inv_cov = np.linalg.pinv((cov_prisparam + cov_test) / 2)
|
|
diff = mu_prisparam - mu_test
|
|
score = np.sqrt(np.dot(np.dot(diff, inv_cov), diff.T))
|
|
return score
|
|
|
|
|
|
def compute_brisque(image):
|
|
"""
|
|
Computes the BRISQUE score using handcrafted statistical features
|
|
and a pretrained SVR model.
|
|
"""
|
|
# Extract basic features: mean, variance, skewness, kurtosis
|
|
features = []
|
|
mu = np.mean(image)
|
|
sigma = np.std(image)
|
|
skew = np.mean(((image - mu) / (sigma + 1e-8)) ** 3)
|
|
kurt = np.mean(((image - mu) / (sigma + 1e-8)) ** 4)
|
|
features.extend([mu, sigma, skew, kurt])
|
|
|
|
# Add GLCM contrast and homogeneity
|
|
glcm = greycomatrix((image * 255).astype(np.uint8), [1], [0], 256, symmetric=True, normed=True)
|
|
contrast = greycoprops(glcm, 'contrast')[0, 0]
|
|
homogeneity = greycoprops(glcm, 'homogeneity')[0, 0]
|
|
features.extend([contrast, homogeneity])
|
|
|
|
# Load pretrained SVR model
|
|
model_path = os.path.join(os.path.dirname(__file__), "brisque_model.joblib")
|
|
if not os.path.exists(model_path):
|
|
raise FileNotFoundError("BRISQUE model not found. Please train or download 'brisque_model.joblib'")
|
|
|
|
model = joblib_load(model_path)
|
|
score = model.predict([features])[0]
|
|
return score |