update
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
'''
|
||||
Filename: __init__.py
|
||||
Path: /home/netoa/Desktop/ISR/metrics/db
|
||||
Created Date: Wednesday December 18th 2024
|
||||
Author: Andre Neto
|
||||
-----
|
||||
Last Modified: Wed Dec 18 2024
|
||||
Modified By: Andre Neto
|
||||
-----
|
||||
Copyright (c) 2024 Institute of Systems and Robotics
|
||||
'''
|
||||
@@ -1,39 +0,0 @@
|
||||
"""
|
||||
|
||||
"""
|
||||
|
||||
from time import time as now
|
||||
import torch
|
||||
|
||||
|
||||
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
||||
|
||||
def compute_all(ref_path, dist_path):
|
||||
"""
|
||||
Computes the distribution between a reference and distorted image folders.
|
||||
"""
|
||||
|
||||
|
||||
output = {}
|
||||
|
||||
metrics = []
|
||||
|
||||
for name, func in metrics:
|
||||
start_time = now()
|
||||
output[name] = (func(), now() - start_time)
|
||||
|
||||
return output
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
# Paths to reference and distorted images
|
||||
REF_IMG_PATH = "test_reference/001.jpg"
|
||||
DIST_IMG_PATH = "test_distorted/001_CodeFace_1.4.jpg"
|
||||
|
||||
# Compute all metrics
|
||||
results = compute_all(REF_IMG_PATH, DIST_IMG_PATH)
|
||||
|
||||
# Print results for each metric and the time taken to compute it
|
||||
for metric, (value, duration) in results.items():
|
||||
print(f"{metric}: {value:.8g} (computed in {duration:.6f} seconds)")
|
||||
@@ -1,7 +1,7 @@
|
||||
"""
|
||||
This module contains functions for computing perceptual and HVS-based (Human Visual System)
|
||||
IQ metrics: VSNR (Visual Signal-to-Noise Ratio), PIQE (Perceptual Image Quality Evaluator),
|
||||
UQI (Universal Quality Index), and PQM (Perceptual Similarity Metric).
|
||||
UQI (Universal Quality Index), and PSM (Perceptual Similarity Metric).
|
||||
|
||||
These metrics evaluate the similarity between reference and distorted images
|
||||
by considering perceptual factors and human visual characteristics.
|
||||
@@ -12,7 +12,7 @@ Functions:
|
||||
- vsnr: Computes the Visual Signal-to-Noise Ratio (VSNR).
|
||||
- piqe: Computes the Perceptual Image Quality Evaluator (PIQE).
|
||||
- uqi: Computes the Universal Quality Index (UQI).
|
||||
- pqm: Computes the Perceptual Similarity Metric (PQM).
|
||||
- psm: Computes the Perceptual Similarity Metric (PSM).
|
||||
"""
|
||||
|
||||
from time import time as now
|
||||
@@ -34,7 +34,7 @@ def compute_all(ref_path, dist_path):
|
||||
("VSNR", lambda: vsnr(ref_img, dist_img)),
|
||||
("PIQE", lambda: piqe(ref_img, dist_img)),
|
||||
("UQI", lambda: uqi(ref_img, dist_img)),
|
||||
("PQM", lambda: pqm(ref_img, dist_img)),
|
||||
("PSM", lambda: psm(ref_img, dist_img)),
|
||||
]
|
||||
|
||||
for name, func in metrics:
|
||||
@@ -106,10 +106,10 @@ def uqi(ref_img, dist_img):
|
||||
return uqi_value
|
||||
|
||||
|
||||
# PQM (Perceptual Similarity Metric)
|
||||
def pqm(ref_img, dist_img):
|
||||
# PSM (Perceptual Similarity Metric)
|
||||
def psm(ref_img, dist_img):
|
||||
"""
|
||||
Computes the Perceptual Similarity Metric (PQM) by comparing the edge similarity
|
||||
Computes the Perceptual Similarity Metric (PSM) by comparing the edge similarity
|
||||
between the reference and distorted images.
|
||||
"""
|
||||
# Edge similarity based perceptual metric
|
||||
@@ -119,9 +119,9 @@ def pqm(ref_img, dist_img):
|
||||
# Compute similarity between edge maps
|
||||
edge_similarity = np.mean(1 - np.abs(ref_edges - dist_edges))
|
||||
|
||||
# PQM value based on edge similarity
|
||||
pqm_value = edge_similarity * 100 # Scale to 0-100
|
||||
return pqm_value
|
||||
# PSM value based on edge similarity
|
||||
psm_value = edge_similarity * 100 # Scale to 0-100
|
||||
return psm_value
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -129,7 +129,7 @@ if __name__ == "__main__":
|
||||
# VSNR (Visual Signal-to-Noise Ratio)
|
||||
# PIQE (Perceptual Image Quality Evaluator)
|
||||
# UQI (Universal Quality Index)
|
||||
# PQM (Perceptual Similarity Metric)
|
||||
# PSM (Perceptual Similarity Metric)
|
||||
|
||||
# Paths to reference and distorted images
|
||||
REF_IMG_PATH = "test_reference/001.jpg"
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
'''
|
||||
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
|
||||
Reference in New Issue
Block a user