-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_operations.py
More file actions
83 lines (62 loc) · 2.44 KB
/
test_operations.py
File metadata and controls
83 lines (62 loc) · 2.44 KB
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
from argparse import Namespace
import numpy as np
from app.image.image import Image
from app.operation.bgr2rgb import BGR2RGB
from app.operation.flip import Flip
from app.operation.grayscale import Grayscale
from app.operation.rotate90 import Rotate90
def test_bgr2rgb():
input_image = Image(np.array([
[[10, 20, 30], [15, 25, 35], [16, 26, 36]],
[[20, 30, 40], [21, 31, 41], [22, 32, 42]],
[[30, 40, 50], [31, 41, 51], [32, 42, 52]]
]))
expected_image = Image(np.array([
[[30, 20, 10], [35, 25, 15], [36, 26, 16]],
[[40, 30, 20], [41, 31, 21], [42, 32, 22]],
[[50, 40, 30], [51, 41, 31], [52, 42, 32]]
]))
output_image = BGR2RGB()(args=None, input_image=input_image)
assert (output_image.data == expected_image.data).all()
def test_flip():
input_image = Image(np.array([
[[10, 20, 30], [15, 25, 35], [16, 26, 36]],
[[20, 30, 40], [21, 31, 41], [22, 32, 42]],
[[30, 40, 50], [31, 41, 51], [32, 42, 52]]
]))
expected_image = Image(np.array([
[[32, 42, 52], [31, 41, 51], [30, 40, 50]],
[[22, 32, 42], [21, 31, 41], [20, 30, 40]],
[[16, 26, 36], [15, 25, 35], [10, 20, 30]],
]))
output_image = Flip()(args=Namespace(horizontal=True, vertical=True), input_image=input_image)
assert (expected_image.data == output_image.data).all()
def test_grayscale():
# Try for already grayscale image
input_image = Image(np.array([
[[5], [123], [123]],
[[12], [12], [12]],
[[12], [12], [12]]
]))
expected_output = input_image
output_image = Grayscale()(args=None, input_image=input_image)
assert (expected_output.data == output_image.data).all()
# Try for coloured image
input_image = Image(np.array([
[[15, 10, 12], [120, 33, 20]],
[[30, 0, 2], [10, 45, 22]]
]))
output_image = Grayscale()(args=None, input_image=input_image)
assert output_image.data.shape[-1] == 3
for row in output_image.data:
for pixel in row:
assert np.all(pixel == pixel[0])
def test_rotate90():
input_image = Image(np.array([
[[15, 10, 12], [120, 33, 20]],
[[30, 0, 2], [10, 45, 22]]
]))
for rotation in range(-5, 5):
expected_output = np.rot90(input_image.data, rotation)
output_image = Rotate90()(args=Namespace(rotations=rotation), input_image=input_image)
assert (expected_output == output_image.data).all()