forked from belltailjp/selective_search_py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_color_space.py
99 lines (73 loc) · 3.24 KB
/
test_color_space.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest
import numpy
from color_space import *
class TestColorSpace:
def _assert_range(self, img):
assert img.dtype == numpy.uint8
assert img.shape == (10, 10, 3)
assert 0 <= numpy.min(img)
assert 1 < numpy.max(img) <= 255
def setup_method(self, method):
self.I = numpy.ndarray((10, 10, 3), dtype=numpy.uint8)
self.I[:, :, 0] = 50
self.I[:, :, 1] = 100
self.I[:, :, 2] = 150
self.Irand = numpy.random.randint(0, 256, (10, 10, 3)).astype(numpy.uint8)
def test_to_grey_range(self):
self._assert_range(to_grey(self.Irand))
def test_to_grey_value(self):
img = to_grey(self.I)
grey_value = int(0.2125 * 50 + 0.7154 * 100 + 0.0721 * 150)
assert ((img == grey_value).all())
def test_to_Lab_range(self):
self._assert_range(to_Lab(self.Irand))
def test_to_Lab_value(self):
img = to_Lab(self.I)
def test_to_rgI_range(self):
self._assert_range(to_rgI(self.Irand))
def test_to_rgI_value(self):
img = to_rgI(self.I)
grey_value = int(0.2125 * 50 + 0.7154 * 100 + 0.0721 * 150)
assert ((img[:, :, 0] == 50).all())
assert ((img[:, :, 1] == 100).all())
assert ((img[:, :, 2] == grey_value).all())
def test_to_HSV_range(self):
self._assert_range(to_HSV(self.Irand))
def test_to_HSV_value(self):
img = to_HSV(self.I)
h, s, v = 148, 170, 150
assert ((img[:, :, 0] == h).all())
assert ((img[:, :, 1] == s).all())
assert ((img[:, :, 2] == v).all())
def test_to_nRGB_range(self):
self._assert_range(to_nRGB(self.Irand))
def test_to_nRGB_value(self):
img = to_nRGB(self.I)
denom = numpy.sqrt(50 ** 2 + 100 ** 2 + 150 ** 2) / 255.0
r, g, b = 50 / denom, 100 / denom, 150 / denom
assert ((img[:, :, 0] == int(r)).all())
assert ((img[:, :, 1] == int(g)).all())
assert ((img[:, :, 2] == int(b)).all())
def test_to_Hue_range(self):
self._assert_range(to_Hue(self.Irand))
def test_to_Hue_value(self):
img = to_Hue(self.I)
expected_h = 148
assert ((img[:, :, 0] == expected_h).all())
assert ((img[:, :, 1] == expected_h).all())
assert ((img[:, :, 2] == expected_h).all())
def test_convert_color_nonexisting_color(self):
with pytest.raises(KeyError):
convert_color(self.Irand, 'nonexisting-colorspace')
def test_convert_color_give_singlechannel_image(self):
I = numpy.random.randint(0, 255, (10, 10)).astype(numpy.uint8)
assert numpy.array_equal(convert_color(I, 'rgb')[:, :, 0], I)
def test_convert_color_value(self):
assert numpy.array_equal(convert_color(self.Irand, 'rgb'), self.Irand)
assert numpy.array_equal(convert_color(self.Irand, 'lab'), to_Lab(self.Irand))
assert numpy.array_equal(convert_color(self.Irand, 'rgi'), to_rgI(self.Irand))
assert numpy.array_equal(convert_color(self.Irand, 'hsv'), to_HSV(self.Irand))
assert numpy.array_equal(convert_color(self.Irand, 'nrgb'), to_nRGB(self.Irand))
assert numpy.array_equal(convert_color(self.Irand, 'hue'), to_Hue(self.Irand))