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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#!/usr/bin/env python
from itertools import product
from functools import reduce
import numpy as np
import cv2 as cv
from tests_common import NewOpenCVTests
def norm_inf(x, y=None):
def norm(vec):
return np.linalg.norm(vec.flatten(), np.inf)
x = x.astype(np.float64)
return norm(x) if y is None else norm(x - y.astype(np.float64))
def norm_l1(x, y=None):
def norm(vec):
return np.linalg.norm(vec.flatten(), 1)
x = x.astype(np.float64)
return norm(x) if y is None else norm(x - y.astype(np.float64))
def norm_l2(x, y=None):
def norm(vec):
return np.linalg.norm(vec.flatten())
x = x.astype(np.float64)
return norm(x) if y is None else norm(x - y.astype(np.float64))
def norm_l2sqr(x, y=None):
def norm(vec):
return np.square(vec).sum()
x = x.astype(np.float64)
return norm(x) if y is None else norm(x - y.astype(np.float64))
def norm_hamming(x, y=None):
def norm(vec):
return sum(bin(i).count('1') for i in vec.flatten())
return norm(x) if y is None else norm(np.bitwise_xor(x, y))
def norm_hamming2(x, y=None):
def norm(vec):
def element_norm(element):
binary_str = bin(element).split('b')[-1]
if len(binary_str) % 2 == 1:
binary_str = '0' + binary_str
gen = filter(lambda p: p != '00',
(binary_str[i:i+2]
for i in range(0, len(binary_str), 2)))
return sum(1 for _ in gen)
return sum(element_norm(element) for element in vec.flatten())
return norm(x) if y is None else norm(np.bitwise_xor(x, y))
norm_type_under_test = {
cv.NORM_INF: norm_inf,
cv.NORM_L1: norm_l1,
cv.NORM_L2: norm_l2,
cv.NORM_L2SQR: norm_l2sqr,
cv.NORM_HAMMING: norm_hamming,
cv.NORM_HAMMING2: norm_hamming2
}
norm_name = {
cv.NORM_INF: 'inf',
cv.NORM_L1: 'L1',
cv.NORM_L2: 'L2',
cv.NORM_L2SQR: 'L2SQR',
cv.NORM_HAMMING: 'Hamming',
cv.NORM_HAMMING2: 'Hamming2'
}
def get_element_types(norm_type):
if norm_type in (cv.NORM_HAMMING, cv.NORM_HAMMING2):
return (np.uint8,)
else:
return (np.uint8, np.int8, np.uint16, np.int16, np.int32, np.float32,
np.float64, np.float16)
def generate_vector(shape, dtype):
if np.issubdtype(dtype, np.integer):
return np.random.randint(0, 100, shape).astype(dtype)
else:
return np.random.normal(10., 12.5, shape).astype(dtype)
shapes = (1, 2, 3, 5, 7, 16, (1, 1), (2, 2), (3, 5), (1, 7))
class norm_test(NewOpenCVTests):
def test_norm_for_one_array(self):
np.random.seed(123)
for norm_type, norm in norm_type_under_test.items():
element_types = get_element_types(norm_type)
for shape, element_type in product(shapes, element_types):
array = generate_vector(shape, element_type)
expected = norm(array)
actual = cv.norm(array, norm_type)
self.assertAlmostEqual(
expected, actual, places=2,
msg='Array {0} of {1} and norm {2}'.format(
array, element_type.__name__, norm_name[norm_type]
)
)
def test_norm_for_two_arrays(self):
np.random.seed(456)
for norm_type, norm in norm_type_under_test.items():
element_types = get_element_types(norm_type)
for shape, element_type in product(shapes, element_types):
first = generate_vector(shape, element_type)
second = generate_vector(shape, element_type)
expected = norm(first, second)
actual = cv.norm(first, second, norm_type)
self.assertAlmostEqual(
expected, actual, places=2,
msg='Arrays {0} {1} of type {2} and norm {3}'.format(
first, second, element_type.__name__,
norm_name[norm_type]
)
)
def test_norm_fails_for_wrong_type(self):
for norm_type in (cv.NORM_HAMMING, cv.NORM_HAMMING2):
with self.assertRaises(Exception,
msg='Type is not checked {0}'.format(
norm_name[norm_type]
)):
cv.norm(np.array([1, 2], dtype=np.int32), norm_type)
def test_norm_fails_for_array_and_scalar(self):
for norm_type in norm_type_under_test:
with self.assertRaises(Exception,
msg='Exception is not thrown for {0}'.format(
norm_name[norm_type]
)):
cv.norm(np.array([1, 2], dtype=np.uint8), 123, norm_type)
def test_norm_fails_for_scalar_and_array(self):
for norm_type in norm_type_under_test:
with self.assertRaises(Exception,
msg='Exception is not thrown for {0}'.format(
norm_name[norm_type]
)):
cv.norm(4, np.array([1, 2], dtype=np.uint8), norm_type)
def test_norm_fails_for_array_and_norm_type_as_scalar(self):
for norm_type in norm_type_under_test:
with self.assertRaises(Exception,
msg='Exception is not thrown for {0}'.format(
norm_name[norm_type]
)):
cv.norm(np.array([3, 4, 5], dtype=np.uint8),
norm_type, normType=norm_type)
if __name__ == '__main__':
NewOpenCVTests.bootstrap()
|