-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest_A01.py
143 lines (126 loc) · 6.06 KB
/
Test_A01.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import unittest
from unittest.mock import patch
import shutil
from pathlib import Path
import sys
import os
import subprocess as sub
import cv2
import numpy as np
import pandas as pd
import General_Testing as GT
import A01
base_dir = "assign01"
image_dir = base_dir + "/" + "images"
ground_dir = base_dir + "/" + "ground"
out_dir = base_dir + "/" + "output"
def get_matching_hist(df, filename):
return df.loc[df['filename'] == filename].drop(["filename"], axis=1).to_numpy()[0]
class Test_A01(unittest.TestCase):
def test_create_unnormalized_hist(self):
# Load unnormalized histogram list
df = pd.read_csv(os.path.join(ground_dir,"data_unnorm_hist.csv"))
# Get filenames
all_filenames = df["filename"]
# For each image...
for filename in all_filenames:
# Load image
image = cv2.imread(os.path.join(image_dir, filename))
# Convert to grayscale
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Call hist function
hist = A01.create_unnormalized_hist(image)
# Compare to correct answer
ground_hist = get_matching_hist(df, filename)
GT.check_for_unequal("Failed on image", filename, hist, ground_hist)
def test_normalize_hist(self):
# Load up CSVs
input_df = pd.read_csv(os.path.join(ground_dir,"data_unnorm_hist.csv"))
ground_df = pd.read_csv(os.path.join(ground_dir,"data_norm_hist.csv"))
# Sort by filename
input_df = input_df.sort_values("filename", axis=0)
ground_df = ground_df.sort_values("filename", axis=0)
# Get filename list
all_filenames = input_df["filename"].to_numpy()
# Drop filename column
all_input_hist = input_df.drop(["filename"], axis=1)
all_ground_hist = ground_df.drop(["filename"], axis=1)
# Loop through rows
for index, row in all_input_hist.iterrows():
# Grab the input unnormalized histogram
input_hist = row.to_numpy()
# Get ground truth
ground_hist = all_ground_hist.iloc[[index]].to_numpy()[0]
# Calculate the normalized histogram
norm_hist = A01.normalize_hist(input_hist)
# Compare to see if correct
GT.check_for_unequal("Failed on image", all_filenames[index], norm_hist, ground_hist)
def test_create_cdf(self):
# Load up CSVs
input_df = pd.read_csv(os.path.join(ground_dir,"data_norm_hist.csv"))
ground_df = pd.read_csv(os.path.join(ground_dir,"data_cdf.csv"))
# Sort by filename
input_df = input_df.sort_values("filename", axis=0)
ground_df = ground_df.sort_values("filename", axis=0)
# Get filename list
all_filenames = input_df["filename"].to_numpy()
# Drop filename column
all_input_hist = input_df.drop(["filename"], axis=1)
all_ground = ground_df.drop(["filename"], axis=1)
# Loop through rows
for index, row in all_input_hist.iterrows():
# Grab the input normalized histogram
input_hist = row.to_numpy()
# Get ground truth
ground = all_ground.iloc[[index]].to_numpy()[0]
# Calculate the cdf
cdf = A01.create_cdf(input_hist)
# Compare to see if correct
GT.check_for_unequal("Failed on image", all_filenames[index], cdf, ground)
def test_get_hist_equalize_transform(self):
# For each image...
all_filenames = os.listdir(image_dir)
all_filenames.sort()
# Load up CSVs
df_eq = pd.read_csv(os.path.join(ground_dir,"data_transform_eq.csv"))
df_eqs = pd.read_csv(os.path.join(ground_dir,"data_transform_eqs.csv"))
# For each image
for filename in all_filenames:
# Load image
image = cv2.imread(os.path.join(image_dir, filename))
# Convert to grayscale
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Call histogram equalization function
output_eq = A01.get_hist_equalize_transform(image, False)
output_eqs = A01.get_hist_equalize_transform(image, True)
# Load up ground truth transformations
ground_eq = get_matching_hist(df_eq, filename)
ground_eqs = get_matching_hist(df_eqs, filename)
# Compare to see if correct
GT.check_for_unequal("Failed on image (no stretch)", filename, output_eq, ground_eq)
GT.check_for_unequal("Failed on image (stretch)", filename, output_eqs, ground_eqs)
def test_do_histogram_equalize(self):
# For each image...
all_filenames = os.listdir(image_dir)
all_filenames.sort()
for filename in all_filenames:
# Load image
image = cv2.imread(os.path.join(image_dir, filename))
# Convert to grayscale
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Call histogram equalization function
output_eq = A01.do_histogram_equalize(image, False)
output_eqs = A01.do_histogram_equalize(image, True)
# Load up ground truth image
ground_eq = cv2.imread(os.path.join(ground_dir, "EQ_" + filename))
ground_eq = cv2.cvtColor(ground_eq, cv2.COLOR_BGR2GRAY)
ground_eqs = cv2.imread(os.path.join(ground_dir, "EQS_" + filename))
ground_eqs = cv2.cvtColor(ground_eqs, cv2.COLOR_BGR2GRAY)
# Compare to see if correct
GT.check_for_unequal("Failed on image (no stretch)", filename, output_eq, ground_eq)
GT.check_for_unequal("Failed on image (stretch)", filename, output_eqs, ground_eqs)
def main():
runner = unittest.TextTestRunner()
runner.run(unittest.makeSuite(Test_A01))
if __name__ == '__main__':
main()