-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_processing.py
110 lines (75 loc) · 3.22 KB
/
test_processing.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
"""
Test the functions in read_and_process.py
"""
import os
import yaml
from pylithics.src.read_and_process import read_image, detect_lithic, \
find_lithic_contours, process_image
import matplotlib.pyplot as plt
def test_read_image():
image_array = read_image(os.path.join('tests', 'test_images'), 'test')
assert image_array.shape == (1841, 1665)
def test_detect_lithic():
image_array = read_image(os.path.join('tests', 'test_images'), 'test')
filename_config = os.path.join('tests', 'test_config.yml')
# Read YAML file
with open(filename_config, 'r') as config_file:
config_file = yaml.load(config_file)
binary_edge_sobel, _ = detect_lithic(image_array, config_file)
fig, axes = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True,
figsize=(8, 8))
axes = axes.ravel()
axes[0].imshow(image_array, cmap=plt.cm.gray)
axes[0].set_title('Original image')
axes[1].imshow(binary_edge_sobel, cmap=plt.cm.gray)
axes[1].set_title('Sobel Edge Detection')
for ax in axes:
ax.axis('off')
plt.tight_layout()
plt.savefig(os.path.join('tests', 'edge_detection_lithic.png'))
assert binary_edge_sobel.shape == (1841, 1665)
def test_find_lithic_contours():
image_array = read_image(os.path.join('tests', 'test_images'), 'test')
filename_config = os.path.join('tests', 'test_config.yml')
# Read YAML file
with open(filename_config, 'r') as config_file:
config_file = yaml.load(config_file)
from matplotlib.font_manager import FontProperties
fontP = FontProperties()
image_processed = process_image(image_array, config_file)
config_file['conversion_px'] = 0.1 # hardcoded for now
binary_edge_sobel, _ = detect_lithic(image_processed, config_file)
contours = find_lithic_contours(binary_edge_sobel, config_file)
fig, ax = plt.subplots(figsize=(10, 5))
ax = plt.subplot(111)
ax.imshow(image_array, cmap=plt.cm.gray)
for contour, hierarchy, index in contours[['contour', 'hierarchy', 'index']].itertuples(index=False):
try:
if hierarchy[-1] == -1:
linewidth = 3
linestyle = 'solid'
text = "Lithic"
else:
linewidth = 2
linestyle = 'dashed'
text = "Scar"
ax.plot(contour[:, 0], contour[:, 1], linewidth=linewidth, linestyle=linestyle, label=text)
except:
continue
fontP.set_size('xx-small')
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', fontsize='xx-small')
plt.figtext(0.02, 0.5, str(len(contours)) + ' contours')
ax.set_xticks([])
ax.set_yticks([])
plt.savefig(os.path.join('tests', 'contour_detection_lithic.png'))
plt.close(fig)
assert contours[['contour']].shape[0] > 10
def test_process_image():
image_array = read_image(os.path.join('tests', 'test_images'), 'test')
filename_config = os.path.join('tests', 'test_config.yml')
# Read YAML file
with open(filename_config, 'r') as config_file:
config_file = yaml.load(config_file)
image_processed = process_image(image_array, config_file)
assert image_processed.shape[0] != 0
assert image_processed.max() <= 1.0