145 lines
6.3 KiB
Python
145 lines
6.3 KiB
Python
"""
|
|
This script processes reference and distorted images, computes various image quality metrics
|
|
from the `metrics` modules, and writes the results to a CSV file as they are computed.
|
|
|
|
It loops through reference images, finds corresponding distorted images, and computes
|
|
metrics for each pair using the `compute_all` function from the appropriate modules.
|
|
|
|
Metrics include various quality assessment methods such as:
|
|
- Color-based metrics (C-SSIM, FSIMc, VIFc)
|
|
- Structural-based metrics (SSIM, MS-SSIM, G-SSIM, IW-SSIM)
|
|
- Gradient-based metrics (GMSD, G-SSIM, ESIM)
|
|
- Frequency-based metrics (FIQ, SRSIM, ERGAS, SAM)
|
|
- Wavelet-based metrics (W-SSIM, CW-SSIM, SPIQ)
|
|
- Perceptual-based metrics (VSNR, PIQE, UQI, PQM)
|
|
- Pixel-based metrics (MSE, PSNR, SNR, MAE)
|
|
- Information-theoretic metrics (VIF, VIFp)
|
|
- Model-based metrics (LPIPS, DISTS)
|
|
|
|
Functions:
|
|
- process_imgs: Loops through the images, computes metrics, and writes to a CSV file.
|
|
- main: Defines directories for images and the output file, then calls `process_imgs`.
|
|
|
|
Modules used:
|
|
- metrics.color: Contains color-based metrics computation.
|
|
- metrics.structural: Contains SSIM-based metrics computation.
|
|
- metrics.gradient: Contains gradient-based metrics computation.
|
|
- metrics.frequency: Contains frequency domain metrics computation.
|
|
- metrics.wavelet: Contains wavelet-based metrics computation.
|
|
- metrics.perceptual: Contains perceptual-based metrics computation.
|
|
- metrics.pixel: Contains pixel-based metrics computation.
|
|
- metrics.information: Contains information-theoretic metrics computation.
|
|
"""
|
|
|
|
import os
|
|
import csv
|
|
|
|
from metrics.objective.full_reference import color
|
|
from metrics.objective.full_reference import frequency
|
|
from metrics.objective.full_reference import gradient
|
|
from metrics.objective.full_reference import information
|
|
from metrics.objective.full_reference import models
|
|
from metrics.objective.full_reference import perceptual
|
|
from metrics.objective.full_reference import pixel
|
|
from metrics.objective.full_reference import region
|
|
from metrics.objective.full_reference import structural
|
|
from metrics.objective.full_reference import wavelet
|
|
|
|
def compute(ref_dir, dist_dir, out_file):
|
|
"""
|
|
Processes reference and distorted images, computes various image quality metrics,
|
|
and appends the results to a CSV file as they are computed.
|
|
|
|
Parameters:
|
|
ref_dir (str): Path to the directory containing reference images.
|
|
dist_dir (str): Path to the directory containing distorted images.
|
|
out_file (str): Path to the output CSV file where the results will be saved.
|
|
"""
|
|
print("Initializing processing of images...")
|
|
print(f"Reference directory: {ref_dir}")
|
|
print(f"Distorted directory: {dist_dir}")
|
|
print(f"Output file: {out_file}")
|
|
|
|
ref_imgs = sorted([f for f in os.listdir(ref_dir) if os.path.isfile(os.path.join(ref_dir, f))])
|
|
print(f"Found {len(ref_imgs)} reference images.")
|
|
|
|
header_written = False # Flag to check if the header has been written
|
|
|
|
with open(out_file, mode='a', newline='', encoding='utf-8') as file:
|
|
writer = csv.writer(file)
|
|
|
|
for ref_idx, ref_img in enumerate(ref_imgs, start=1):
|
|
ref_img_path = os.path.join(ref_dir, ref_img)
|
|
img_name = ref_img.rsplit('.', 1)[0]
|
|
|
|
print(f"\nProcessing reference image {ref_idx}/{len(ref_imgs)}: {ref_img}")
|
|
|
|
# Find all distorted images for the current reference image
|
|
dist_imgs = sorted([f for f in os.listdir(dist_dir) if f.startswith(img_name)])
|
|
print(f" Found {len(dist_imgs)} distorted images for {ref_img}.")
|
|
|
|
for dist_idx, dist_img in enumerate(dist_imgs, start=1):
|
|
dist_img_path = os.path.join(dist_dir, dist_img)
|
|
print(f" - Processing distorted image {dist_idx}/{len(dist_imgs)}: {dist_img}")
|
|
|
|
# Compute metrics
|
|
color_results = color.compute_all(ref_img_path, dist_img_path)
|
|
frequency_results = frequency.compute_all(ref_img_path, dist_img_path)
|
|
gradient_results = gradient.compute_all(ref_img_path, dist_img_path)
|
|
information_results = information.compute_all(ref_img_path, dist_img_path)
|
|
models_results = models.compute_all(ref_img_path, dist_img_path)
|
|
perceptual_results = perceptual.compute_all(ref_img_path, dist_img_path)
|
|
pixel_results = pixel.compute_all(ref_img_path, dist_img_path)
|
|
region_results = region.compute_all(ref_img_path, dist_img_path)
|
|
structural_results = structural.compute_all(ref_img_path, dist_img_path)
|
|
wavelet_results = wavelet.compute_all(ref_img_path, dist_img_path)
|
|
|
|
# Combine all metric results into a single dictionary
|
|
combined_results = {
|
|
**color_results,
|
|
**frequency_results,
|
|
**gradient_results,
|
|
**information_results,
|
|
**models_results,
|
|
**perceptual_results,
|
|
**pixel_results,
|
|
**region_results,
|
|
**structural_results,
|
|
**wavelet_results,
|
|
}
|
|
|
|
# Prepare header and result row
|
|
metric_names = ['file_name'] + list(combined_results.keys())
|
|
img_name = dist_img.rsplit('.', 1)[0]
|
|
result_row = [img_name] + [f"{val[0]:.8g}" for val in combined_results.values()]
|
|
|
|
# Write header if not already written
|
|
if not header_written:
|
|
writer.writerow(metric_names)
|
|
header_written = True
|
|
|
|
# Write the result row
|
|
writer.writerow(result_row)
|
|
|
|
print(f"Successfully processed and saved metrics for {img_name}")
|
|
|
|
print("\nProcessing complete! Results saved to:", out_file)
|
|
|
|
|
|
def main():
|
|
'''main function'''
|
|
#reference_dir = 'images/reference/'
|
|
#distorted_dir = 'images/distorted/'
|
|
#output_file = 'results/sheets/iqa_objective.csv'
|
|
|
|
# If you want to start fresh, uncomment the following line to clear the file
|
|
# open(output_file, 'w', encoding='utf-8').close()
|
|
|
|
print("Starting the full-reference image quality assessment pipeline...")
|
|
compute(reference_dir, distorted_dir, output_file)
|
|
print("Pipeline finished.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|