Skip to content
Snippets Groups Projects
Commit a6d9a63a authored by XinningCui's avatar XinningCui
Browse files

Initial commit

parent 2a3f19c2
No related branches found
No related tags found
No related merge requests found
File added
import os
import cv2
import tensorflow as tf
import pandas as pd
import numpy as np
#Vanilla CVDM
folder_a = r'path to\images\VanillaCVDM_inference'
folder_b = r'path to\images\BioSR_high_resolution_images'
#CVDM+N
# folder_a=r'path to\images\naturalized_BioSR_high_resolution_images'
# folder_b=r'path to\images\CVDM+N_inference'
#NCVDM
# folder_a=r'path to\images\naturalized_BioSR_high_resolution_images'
# folder_b=r'path to\images\NCVDM_inference'
metrics = []
# Loop through the images in the first folder
for filename in os.listdir(folder_a):
img_path_a = os.path.join(folder_a, filename)
img_path_b = os.path.join(folder_b, filename)
if os.path.isfile(img_path_b):
# Read the grayscale TIFF images
img_a = cv2.imread(img_path_a, cv2.IMREAD_UNCHANGED)
img_b = cv2.imread(img_path_b, cv2.IMREAD_UNCHANGED)
if img_a is None:
print(f"Error: Could not load {img_path_a}")
continue
if img_b is None:
print(f"Error: Could not load {img_path_b}")
continue
img_a = img_a.astype('float32')
img_b = img_b.astype('float32')
# Min-Max normalization
img_a_min, img_a_max = img_a.min(), img_a.max()
img_b_min, img_b_max = img_b.min(), img_b.max()
#normalize image to [0, 255]
img_a = ((img_a - img_a_min) / (img_a_max - img_a_min)) * 255
img_b = ((img_b - img_b_min) / (img_b_max - img_b_min)) * 255
# Adds a channel dimension
img_a = tf.expand_dims(img_a, axis=-1)
img_b = tf.expand_dims(img_b, axis=-1)
# Calculate MS-SSIM, PSNR and MAE
msssim_value = tf.image.ssim_multiscale(img_a, img_b, max_val=255.0).numpy()
psnr_value = tf.image.psnr(img_a, img_b, max_val=255).numpy()
mae_value = np.mean(np.abs(img_a - img_b))
# Append the result to the list
metrics.append({
"filename": filename,
"MS-SSIM": msssim_value,
"MAE": mae_value,
"PSNR": psnr_value
})
# Create a DataFrame from the results
results_df = pd.DataFrame(metrics)
# Save results to a CSV file
results_df.to_csv(r"path to\CVDM+N.csv", index=False)
import pandas as pd
# Load the Excel file
file_path = r"path to\VanillaCVDM_all_metrics.xlsx" # Replace with your file path
df = pd.read_excel(file_path, header=None)
# Set the first row as column headers
df.columns = df.iloc[0]
df = df[1:] # Remove the first row from the data
# # Debugging: Check the processed data
# print("\nProcessed DataFrame:")
# print(df.head())
# Calculate the Pearson correlation coefficient matrix
correlation_matrix = df.corr(method='pearson').round(3)
# Display the correlation matrix
print("\nPearson Correlation Coefficient Matrix:")
print(correlation_matrix)
# Optionally, save the correlation matrix to a CSV file
#correlation_matrix.to_csv(r"path to\VanillaCVDM_correlation_matrix.csv", index=True)
#RMS contrast
import os
import cv2
import numpy as np
import pandas as pd
# Function to normalize image to [0, 255] using Min-Max normalization
def min_max_normalize(image):
return ((image - np.min(image)) / (np.max(image) - np.min(image)))*255
# Function to calculate RMS contrast
def calculate_rms_contrast(image):
# Calculate the mean intensity
mean_intensity = np.mean(image)
# Calculate RMS contrast
rms_contrast = np.sqrt(np.mean((image - mean_intensity) ** 2))
return rms_contrast
#Vanilla CVDM
image_folder = r'path to\images\VanillaCVDM_inference' # Replace with your folder path
output_csv = r"path to \RMS_contrast_CVDM.csv"
#CVDM+N
# image_folder = =r'path to\images\CVDM+N_inference' # Replace with your folder path
# output_csv = r"path to\RMS_contrast_CVDM+N.csv"
#NCVDM
# image_folder = r'path to\images\NCVDM_inference' # Replace with your folder path
# output_csv = r"path to \RMS_contrast_NCVDM.csv"
# List to store results
results = []
# Loop through all files in the folder
for filename in os.listdir(image_folder):
file_path = os.path.join(image_folder, filename)
# Check if the file is an image (optional: add specific extensions if needed)
if filename.lower().endswith(( ".tif", ".tiff")):
try:
# Read the image
image = cv2.imread(file_path,cv2.IMREAD_UNCHANGED)
image=image.astype('float32')
image=min_max_normalize(image)
# Ensure the image was loaded successfully
if image is not None:
# Calculate RMS contrast
rms_contrast = calculate_rms_contrast(image)
# Append the result to the list
results.append({"filename": filename, "RMS Contrast": rms_contrast})
else:
print(f"Could not read image: {filename}")
except Exception as e:
print(f"Error processing file {filename}: {e}")
# Save results to a CSV file
if results:
df = pd.DataFrame(results)
df.to_csv(output_csv, index=False)
print(f"RMS contrast results saved to {output_csv}")
else:
print("No valid images found in the folder.")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment