forked from yati-sagade/opencv-ndarray-conversion
/
test.py
executable file
·49 lines (39 loc) · 1.54 KB
/
test.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
import unittest
import numpy as np
import examples
class TestExamples(unittest.TestCase):
'''
Tests for the examples module.
'''
def test_vector_multiplication(self):
a = np.array([[1., 2., 3.]])
b = a.reshape(3, 1)
self.assertEqual(np.equal(examples.mul(a, b), a.dot(b)).all(), True)
def test_matrix_squaring(self):
a = np.array([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
self.assertEqual(np.equal(examples.mul(a, a), a.dot(a)).all(), True)
def test_matrix_multiplication(self):
a = np.array([[1., 2., 3., 4.],
[4., 3., 2., 1.]])
b = np.array([[ 1., 0.],
[ 2., 3.],
[ 4., 4.],
[-1., 2.]])
self.assertEqual(np.equal(examples.mul(a, b), a.dot(b)).all(), True)
def test_identity_multiplication(self):
eye = np.eye(3)
a = np.random.random((3, 3)) * 10
self.assertEqual(np.equal(examples.mul(eye, a),
examples.mul(a, eye)).all(), True)
self.assertEqual(np.equal(examples.mul(eye, a), eye.dot(a)).all(), True)
def test_binarization(self):
image = np.ones((8, 8), dtype=np.uint8) * 128 # Grayscale
image[4:6, 4:6] = 126
expected = np.ones((8, 8), dtype=np.uint8) * 255
expected[4:6, 4:6] = 0
binarized = examples.binarize(image, 127)
self.assertTrue(np.equal(expected, binarized).all())
if __name__ == '__main__':
unittest.main()