Commit 96c46195 authored by Kelvin Rodriguez's avatar Kelvin Rodriguez Committed by GitHub
Browse files

Merge pull request #181 from evindunn/dev

naive_template test/fix
parents 51b2d115 99d6d7d0
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -65,7 +65,6 @@ def pattern_match(template, image, upsampling=16, func=cv2.TM_CCOEFF_NORMED, err
    y += (u_template.shape[0] / 2)
    x += (u_template.shape[1] / 2)

    x = (ideal_x - x) / upsampling
    x = (x - ideal_x) / upsampling
    y = (ideal_y - y) / upsampling
    return x, y, max_corr
+85 −0
Original line number Diff line number Diff line
import unittest
from .. import naive_template
from numpy import array
from numpy import uint8


class TestNaiveTemplate(unittest.TestCase):

    def setUp(self):
        # Center is (5, 6)
        self._test_image = array(((0, 0, 0, 0, 0, 0, 0, 1, 0),
                                  (0, 0, 0, 0, 0, 0, 0, 1, 0),
                                  (1, 1, 1, 0, 0, 0, 0, 1, 0),
                                  (0, 1, 0, 0, 0, 0, 0, 0, 0),
                                  (0, 1, 0, 0, 0, 0, 0, 0, 0),
                                  (0, 0, 0, 0, 0, 0, 0, 0, 0),
                                  (0, 0, 0, 0, 0, 0, 0, 0, 0),
                                  (0, 0, 0, 0, 0, 0, 0, 0, 0),
                                  (0, 0, 0, 0, 0, 0, 1, 1, 1),
                                  (0, 1, 1, 1, 0, 0, 1, 0, 1),
                                  (0, 1, 0, 1, 0, 0, 1, 0, 1),
                                  (0, 1, 1, 1, 0, 0, 1, 0, 1),
                                  (0, 0, 0, 0, 0, 0, 1, 1, 1)), dtype=uint8)

        # Should yield (-3, 3) offset from image center
        self._t_shape = array(((1, 1, 1),
                               (0, 1, 0),
                               (0, 1, 0)), dtype=uint8)

        # Should be (3, -4)
        self._rect_shape = array(((1, 1, 1),
                                  (1, 0, 1),
                                  (1, 0, 1),
                                  (1, 0, 1),
                                  (1, 1, 1)), dtype=uint8)

        # Should be (-2, -4)
        self._square_shape = array(((1, 1, 1),
                                    (1, 0, 1),
                                    (1, 1, 1)), dtype=uint8)

        # Should be (3, 5)
        self._vertical_line = array(((0, 1, 0),
                                     (0, 1, 0),
                                     (0, 1, 0)), dtype=uint8)

    def test_t_shape(self):
        result_x, result_y, result_strength = naive_template.pattern_match(self._t_shape,
                                                                           self._test_image, upsampling=1)
        # Test offsets
        self.assertEqual(result_x, -3)
        self.assertEqual(result_y, 3)
        # Test Correlation Strength: At least 0.8
        self.assertGreaterEqual(result_strength, 0.8, "Returned Correlation Strength of %d" % result_strength)

    def test_rect_shape(self):
        result_x, result_y, result_strength = naive_template.pattern_match(self._rect_shape,
                                                                           self._test_image, upsampling=1)
        # Test offsets
        self.assertEqual(result_x, 3)
        self.assertEqual(result_y, -4)
        # Test Correlation Strength: At least 0.8
        self.assertGreaterEqual(result_strength, 0.8, "Returned Correlation Strength of %d" % result_strength)

    def test_square_shape(self):
        result_x, result_y, result_strength = naive_template.pattern_match(self._square_shape,
                                                                           self._test_image, upsampling=1)
        # Test offsets
        self.assertEqual(result_x, -2)
        self.assertEqual(result_y, -4)
        # Test Correlation Strength: At least 0.8
        self.assertGreaterEqual(result_strength, 0.8, "Returned Correlation Strength of %d" % result_strength)

    def test_line_shape(self):
        result_x, result_y, result_strength = naive_template.pattern_match(self._vertical_line,
                                                                           self._test_image, upsampling=1)
        # Test offsets
        self.assertEqual(result_x, 3)
        self.assertEqual(result_y, 5)
        # Test Correlation Strength: At least 0.8
        self.assertGreaterEqual(result_strength, 0.8, "Returned Correlation Strength of %d" % result_strength)

    def tearDown(self):
        pass