Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#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.")