Exemplo n.º 1
0
    def test_set_lut(self):
        # Arrange
        lb = ImageMorph.LutBuilder(op_name='corner')
        lut = lb.build_lut()
        mop = ImageMorph.MorphOp()

        # Act
        mop.set_lut(lut)

        # Assert
        self.assertEqual(mop.lut, lut)
Exemplo n.º 2
0
 def create_lut(self):
     for op in ('corner', 'dilation4', 'dilation8', 'erosion4', 'erosion8',
                'edge'):
         lb = ImageMorph.LutBuilder(op_name=op)
         lut = lb.build_lut()
         with open('Tests/images/%s.lut' % op, 'wb') as f:
             f.write(lut)
Exemplo n.º 3
0
    def test_corner(self):
        # Create a corner detector pattern
        mop = ImageMorph.MorphOp(
            patterns=['1:(... ... ...)->0', '4:(00. 01. ...)->1'])
        count, Aout = mop.apply(self.A)
        self.assertEqual(count, 5)
        self.assert_img_equal_img_string(
            Aout, """
                                         .......
                                         .......
                                         ..1.1..
                                         .......
                                         ..1.1..
                                         .......
                                         .......
                                         """)

        # Test the coordinate counting with the same operator
        coords = mop.match(self.A)
        self.assertEqual(len(coords), 4)
        self.assertEqual(tuple(coords), ((2, 2), (4, 2), (2, 4), (4, 4)))

        coords = mop.get_on_pixels(Aout)
        self.assertEqual(len(coords), 4)
        self.assertEqual(tuple(coords), ((2, 2), (4, 2), (2, 4), (4, 4)))
Exemplo n.º 4
0
    def test_load_invalid_mrl(self):
        # Arrange
        invalid_mrl = 'Tests/images/hopper.png'
        mop = ImageMorph.MorphOp()

        # Act / Assert
        with self.assertRaises(Exception) as e:
            mop.load_lut(invalid_mrl)
        self.assertEqual(str(e.exception), 'Wrong size operator file!')
Exemplo n.º 5
0
    def test_lut(self):
        for op in ('corner', 'dilation4', 'dilation8', 'erosion4', 'erosion8',
                   'edge'):
            lb = ImageMorph.LutBuilder(op_name=op)
            self.assertIsNone(lb.get_lut())

            lut = lb.build_lut()
            with open('Tests/images/%s.lut' % op, 'rb') as f:
                self.assertEqual(lut, bytearray(f.read()))
Exemplo n.º 6
0
 def test_no_operator_loaded(self):
     mop = ImageMorph.MorphOp()
     with self.assertRaises(Exception) as e:
         mop.apply(None)
     self.assertEqual(str(e.exception), 'No operator loaded')
     with self.assertRaises(Exception) as e:
         mop.match(None)
     self.assertEqual(str(e.exception), 'No operator loaded')
     with self.assertRaises(Exception) as e:
         mop.save_lut(None)
     self.assertEqual(str(e.exception), 'No operator loaded')
Exemplo n.º 7
0
    def test_roundtrip_mrl(self):
        # Arrange
        tempfile = self.tempfile('temp.mrl')
        mop = ImageMorph.MorphOp(op_name='corner')
        initial_lut = mop.lut

        # Act
        mop.save_lut(tempfile)
        mop.load_lut(tempfile)

        # Act / Assert
        self.assertEqual(mop.lut, initial_lut)
Exemplo n.º 8
0
    def test_pattern_syntax_error(self):
        # Arrange
        lb = ImageMorph.LutBuilder(op_name='corner')
        new_patterns = ['a pattern with a syntax error']
        lb.add_patterns(new_patterns)

        # Act / Assert
        with self.assertRaises(Exception) as e:
            lb.build_lut()
        self.assertEqual(
            str(e.exception),
            'Syntax error in pattern "a pattern with a syntax error"')
Exemplo n.º 9
0
 def test_erosion4(self):
     # erosion4
     mop = ImageMorph.MorphOp(op_name='dilation4')
     count, Aout = mop.apply(self.A)
     self.assertEqual(count, 12)
     self.assert_img_equal_img_string(
         Aout, """
                                      .......
                                      ..111..
                                      .11111.
                                      .11111.
                                      .11111.
                                      ..111..
                                      .......
                                      """)
Exemplo n.º 10
0
    def test_add_patterns(self):
        # Arrange
        lb = ImageMorph.LutBuilder(op_name='corner')
        self.assertEqual(lb.patterns,
                         ['1:(... ... ...)->0', '4:(00. 01. ...)->1'])
        new_patterns = ['M:(00. 01. ...)->1', 'N:(00. 01. ...)->1']

        # Act
        lb.add_patterns(new_patterns)

        # Assert
        self.assertEqual(lb.patterns, [
            '1:(... ... ...)->0', '4:(00. 01. ...)->1', 'M:(00. 01. ...)->1',
            'N:(00. 01. ...)->1'
        ])
Exemplo n.º 11
0
 def test_edge(self):
     # edge
     mop = ImageMorph.MorphOp(op_name='edge')
     count, Aout = mop.apply(self.A)
     self.assertEqual(count, 1)
     self.assert_img_equal_img_string(
         Aout, """
                                      .......
                                      .......
                                      ..111..
                                      ..1.1..
                                      ..111..
                                      .......
                                      .......
                                      """)
Exemplo n.º 12
0
 def test_erosion8(self):
     # erosion8
     mop = ImageMorph.MorphOp(op_name='erosion8')
     count, Aout = mop.apply(self.A)
     self.assertEqual(count, 8)
     self.assert_img_equal_img_string(
         Aout, """
                                      .......
                                      .......
                                      .......
                                      ...1...
                                      .......
                                      .......
                                      .......
                                      """)
Exemplo n.º 13
0
 def test_dialation8(self):
     # dialation8
     mop = ImageMorph.MorphOp(op_name='dilation8')
     count, Aout = mop.apply(self.A)
     self.assertEqual(count, 16)
     self.assert_img_equal_img_string(
         Aout, """
                                      .......
                                      .11111.
                                      .11111.
                                      .11111.
                                      .11111.
                                      .11111.
                                      .......
                                      """)
Exemplo n.º 14
0
 def test_mirroring(self):
     # Test 'M' for mirroring
     mop = ImageMorph.MorphOp(
         patterns=['1:(... ... ...)->0', 'M:(00. 01. ...)->1'])
     count, Aout = mop.apply(self.A)
     self.assertEqual(count, 7)
     self.assert_img_equal_img_string(
         Aout, """
                                      .......
                                      .......
                                      ..1.1..
                                      .......
                                      .......
                                      .......
                                      .......
                                      """)
Exemplo n.º 15
0
    def test_non_binary_images(self):
        im = hopper('RGB')
        mop = ImageMorph.MorphOp(op_name="erosion8")

        with self.assertRaises(Exception) as e:
            mop.apply(im)
        self.assertEqual(str(e.exception),
                         'Image must be binary, meaning it must use mode L')
        with self.assertRaises(Exception) as e:
            mop.match(im)
        self.assertEqual(str(e.exception),
                         'Image must be binary, meaning it must use mode L')
        with self.assertRaises(Exception) as e:
            mop.get_on_pixels(im)
        self.assertEqual(str(e.exception),
                         'Image must be binary, meaning it must use mode L')
Exemplo n.º 16
0
 def test_negate(self):
     # Test 'N' for negate
     mop = ImageMorph.MorphOp(
         patterns=['1:(... ... ...)->0', 'N:(00. 01. ...)->1'])
     count, Aout = mop.apply(self.A)
     self.assertEqual(count, 8)
     self.assert_img_equal_img_string(
         Aout, """
                                      .......
                                      .......
                                      ..1....
                                      .......
                                      .......
                                      .......
                                      .......
                                      """)
Exemplo n.º 17
0
    def test_wrong_mode(self):
        lut = ImageMorph.LutBuilder(op_name='corner').build_lut()
        imrgb = Image.new('RGB', (10, 10))
        iml = Image.new('L', (10, 10))

        with self.assertRaises(RuntimeError):
            _imagingmorph.apply(bytes(lut), imrgb.im.id, iml.im.id)

        with self.assertRaises(RuntimeError):
            _imagingmorph.apply(bytes(lut), iml.im.id, imrgb.im.id)

        with self.assertRaises(RuntimeError):
            _imagingmorph.match(bytes(lut), imrgb.im.id)

        # Should not raise
        _imagingmorph.match(bytes(lut), iml.im.id)