Initial clean commit
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
"""
|
||||
This module contains functions for computing information-theoretic image quality metrics,
|
||||
such as VIF (Visual Information Fidelity) and VIFp (Visual Information Fidelity in Pixel Domain).
|
||||
|
||||
These metrics evaluate the similarity between reference and distorted images by analyzing
|
||||
the amount of information preserved in the distorted image.
|
||||
|
||||
Functions:
|
||||
- compute_all: Computes all information-theoretic image quality metrics.
|
||||
- load_image: Loads an image and converts it to floating-point format.
|
||||
- vif: Computes the Visual Information Fidelity (VIF).
|
||||
- vifp: Computes the Visual Information Fidelity in the Pixel Domain (VIFp).
|
||||
"""
|
||||
|
||||
from time import time as now
|
||||
|
||||
import numpy as np
|
||||
from skimage import io, img_as_float
|
||||
|
||||
|
||||
def compute_all(ref_path, dist_path):
|
||||
"""
|
||||
Computes multiple information-theoretic image quality metrics.
|
||||
"""
|
||||
ref_img = load_image(ref_path)
|
||||
dist_img = load_image(dist_path)
|
||||
|
||||
output = {}
|
||||
|
||||
metrics = [
|
||||
("VIF", lambda: vif(ref_img, dist_img)),
|
||||
("VIFp", lambda: vifp(ref_img, dist_img)),
|
||||
]
|
||||
|
||||
for name, func in metrics:
|
||||
start_time = now()
|
||||
output[name] = (func(), now() - start_time)
|
||||
|
||||
return output
|
||||
|
||||
|
||||
# Helper function to load images
|
||||
def load_image(path):
|
||||
"""
|
||||
Loads an image from the given path and converts it to floating-point format.
|
||||
"""
|
||||
img = io.imread(path)
|
||||
img = img_as_float(img)
|
||||
return img
|
||||
|
||||
|
||||
# VIF (Visual Information Fidelity)
|
||||
def vif(ref_img, dist_img):
|
||||
"""
|
||||
Computes the Visual Information Fidelity (VIF) metric by comparing
|
||||
the entropy of the reference and distorted images.
|
||||
"""
|
||||
# Compute the entropy of both images
|
||||
ref_entropy = -np.sum(ref_img * np.log2(ref_img + 1e-10))
|
||||
dist_entropy = -np.sum(dist_img * np.log2(dist_img + 1e-10))
|
||||
|
||||
# VIF score based on the ratio of information fidelity
|
||||
vif_value = ref_entropy / dist_entropy if dist_entropy != 0 else float('inf')
|
||||
return vif_value
|
||||
|
||||
|
||||
# VIFp (Visual Information Fidelity in the Pixel Domain)
|
||||
def vifp(ref_img, dist_img):
|
||||
"""
|
||||
Computes the Visual Information Fidelity in the Pixel Domain (VIFp) metric by analyzing
|
||||
the mean squared error and pixel variance between the reference and distorted images.
|
||||
"""
|
||||
# Compute the mean squared error (MSE)
|
||||
diff = ref_img - dist_img
|
||||
mse_value = np.mean(diff ** 2)
|
||||
|
||||
# Visual information fidelity based on MSE and pixel variance
|
||||
ref_variance = np.var(ref_img)
|
||||
vifp_value = 1 - mse_value / ref_variance if ref_variance != 0 else float('inf')
|
||||
return vifp_value
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Tested:
|
||||
# VIF (Visual Information Fidelity)
|
||||
# VIFp (Visual Information Fidelity in the Pixel Domain)
|
||||
|
||||
# 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 information-theoretic metrics
|
||||
results = compute_all(REF_IMG_PATH, DIST_IMG_PATH)
|
||||
|
||||
# Print results for each metric along with the time taken
|
||||
for metric, (value, duration) in results.items():
|
||||
print(f"{metric}: {value:.8g} (computed in {duration:.6f} seconds)")
|
||||
Reference in New Issue
Block a user