Ejemplo n.º 1
0
    def test_non_binary_images(self):
        im = hopper('RGB')
        mop = ImageMorph.MorphOp(op_name="erosion8")

        self.assertRaises(Exception, mop.apply, im)
        self.assertRaises(Exception, mop.match, im)
        self.assertRaises(Exception, mop.get_on_pixels, im)
Ejemplo n.º 2
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)))
Ejemplo n.º 3
0
def test_corner():
    # Create a corner detector pattern
    mop = ImageMorph.MorphOp(
        patterns=["1:(... ... ...)->0", "4:(00. 01. ...)->1"])
    count, Aout = mop.apply(A)
    assert count == 5
    assert_img_equal_img_string(
        Aout,
        """
                                     .......
                                     .......
                                     ..1.1..
                                     .......
                                     ..1.1..
                                     .......
                                     .......
                                     """,
    )

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

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

        # Act / Assert
        self.assertRaises(Exception, mop.load_lut, invalid_mrl)
    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!')
Ejemplo n.º 6
0
def test_load_invalid_mrl():
    # Arrange
    invalid_mrl = "Tests/images/hopper.png"
    mop = ImageMorph.MorphOp()

    # Act / Assert
    with pytest.raises(Exception) as e:
        mop.load_lut(invalid_mrl)
    assert str(e.value) == "Wrong size operator file!"
Ejemplo n.º 7
0
def count_regions(key):
    image=draw_disk(key)
    on_pixels=ImageMorph.MorphOp().get_on_pixels(image.convert('L'))
    image=image.convert('RGB')
    region_count=0
    for x,y in on_pixels:
        if image.getpixel((x,y))!=(0,0,0):
            ImageDraw.floodfill(image,(x,y),(0,0,0))
            region_count+=1
    return region_count
 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')
Ejemplo n.º 9
0
def test_no_operator_loaded():
    mop = ImageMorph.MorphOp()
    with pytest.raises(Exception) as e:
        mop.apply(None)
    assert str(e.value) == "No operator loaded"
    with pytest.raises(Exception) as e:
        mop.match(None)
    assert str(e.value) == "No operator loaded"
    with pytest.raises(Exception) as e:
        mop.save_lut(None)
    assert str(e.value) == "No operator loaded"
Ejemplo n.º 10
0
def test_set_lut():
    # Arrange
    lb = ImageMorph.LutBuilder(op_name="corner")
    lut = lb.build_lut()
    mop = ImageMorph.MorphOp()

    # Act
    mop.set_lut(lut)

    # Assert
    assert mop.lut == lut
Ejemplo n.º 11
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)
Ejemplo n.º 12
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)
Ejemplo n.º 13
0
def test_roundtrip_mrl(tmp_path):
    # Arrange
    tempfile = str(tmp_path / "temp.mrl")
    mop = ImageMorph.MorphOp(op_name="corner")
    initial_lut = mop.lut

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

    # Act / Assert
    assert mop.lut == initial_lut
Ejemplo n.º 14
0
def test_non_binary_images():
    im = hopper("RGB")
    mop = ImageMorph.MorphOp(op_name="erosion8")

    with pytest.raises(Exception) as e:
        mop.apply(im)
    assert str(e.value) == "Image must be binary, meaning it must use mode L"
    with pytest.raises(Exception) as e:
        mop.match(im)
    assert str(e.value) == "Image must be binary, meaning it must use mode L"
    with pytest.raises(Exception) as e:
        mop.get_on_pixels(im)
    assert str(e.value) == "Image must be binary, meaning it must use mode L"
Ejemplo n.º 15
0
def test_incorrect_mode():
    im = hopper("RGB")
    mop = ImageMorph.MorphOp(op_name="erosion8")

    with pytest.raises(ValueError) as e:
        mop.apply(im)
    assert str(e.value) == "Image mode must be L"
    with pytest.raises(ValueError) as e:
        mop.match(im)
    assert str(e.value) == "Image mode must be L"
    with pytest.raises(ValueError) as e:
        mop.get_on_pixels(im)
    assert str(e.value) == "Image mode must be L"
Ejemplo n.º 16
0
def processImage(path):
    im = Image.open(path)
    print("Path:", path)
    print("Format:", im.format)
    print("Dimensions:", im.size)
    print("Mode:", im.mode)

    # Gerando níveis de dimensões diferentes
    dimensions = []
    # Utilizei potencias de dois por arbitrariedade mesmo
    for j in [2**i for i in range(5)]:
        dimensions.append((im.width / j, im.height / j))
        print(dimensions[-1])

    im_all = []
    for (width, height) in dimensions:
        aux = im.resize((int(width), int(height)))
        im_all.append(aux.resize((im.width, im.height)))

    map_char = []
    for im in im_all:
        for it in im_all:
            if im != it:
                map_char.append(ImageChops.difference(im, it))
    result = map_char[0]
    for img in map_char[1:]:
        result = ImageChops.add(result, img, scale=2.0)
    # get an image

    # make a blank image for the text, initialized to transparent text color
    txt = Image.new('RGBA', im.size, (255, 255, 255, 0))

    # get a font
    fnt = ImageFont.truetype('./Roboto-Black.ttf', 30)
    # get a drawing context
    d = ImageDraw.Draw(txt)
    grayscale = result.convert('L')
    im = Image.open(path).convert('RGBA')
    out = im
    for i in range(1, 35):
        _, maxima = grayscale.getextrema()
        mask_image = grayscale.point(lambda i: 255 if i == maxima else 0)
        mask = ImageMorph.MorphOp(lut=ImageMorph.LutBuilder(
            patterns=["1:(000 010 000)->1"]).build_lut())
        match = mask.match(mask_image)
        d.text(match[0], str(i), font=fnt, fill=(255, 255, 255, 200))
        out = Image.alpha_composite(im, txt)
        supress = Image.new('L', (20, 20), "black")
        (w, h) = match[0]
        grayscale.paste(supress, (w - 10, h - 10))
    out.show()
Ejemplo n.º 17
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.
                                      .......
                                      """)
Ejemplo n.º 18
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..
                                      .......
                                      """)
Ejemplo n.º 19
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..
                                      .......
                                      .......
                                      """)
Ejemplo n.º 20
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...
                                      .......
                                      .......
                                      .......
                                      """)
    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')
Ejemplo n.º 22
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....
                                      .......
                                      .......
                                      .......
                                      .......
                                      """)
Ejemplo n.º 23
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..
                                      .......
                                      .......
                                      .......
                                      .......
                                      """)
Ejemplo n.º 24
0
def test_erosion8():
    # erosion8
    mop = ImageMorph.MorphOp(op_name="erosion8")
    count, Aout = mop.apply(A)
    assert count == 8
    assert_img_equal_img_string(
        Aout,
        """
                                     .......
                                     .......
                                     .......
                                     ...1...
                                     .......
                                     .......
                                     .......
                                     """,
    )
Ejemplo n.º 25
0
def test_dialation8():
    # dialation8
    mop = ImageMorph.MorphOp(op_name="dilation8")
    count, Aout = mop.apply(A)
    assert count == 16
    assert_img_equal_img_string(
        Aout,
        """
                                     .......
                                     .11111.
                                     .11111.
                                     .11111.
                                     .11111.
                                     .11111.
                                     .......
                                     """,
    )
Ejemplo n.º 26
0
def test_erosion4():
    # erosion4
    mop = ImageMorph.MorphOp(op_name="dilation4")
    count, Aout = mop.apply(A)
    assert count == 12
    assert_img_equal_img_string(
        Aout,
        """
                                     .......
                                     ..111..
                                     .11111.
                                     .11111.
                                     .11111.
                                     ..111..
                                     .......
                                     """,
    )
Ejemplo n.º 27
0
def test_edge():
    # edge
    mop = ImageMorph.MorphOp(op_name="edge")
    count, Aout = mop.apply(A)
    assert count == 1
    assert_img_equal_img_string(
        Aout,
        """
                                     .......
                                     .......
                                     ..111..
                                     ..1.1..
                                     ..111..
                                     .......
                                     .......
                                     """,
    )
Ejemplo n.º 28
0
def test_negate():
    # Test 'N' for negate
    mop = ImageMorph.MorphOp(
        patterns=["1:(... ... ...)->0", "N:(00. 01. ...)->1"])
    count, Aout = mop.apply(A)
    assert count == 8
    assert_img_equal_img_string(
        Aout,
        """
                                     .......
                                     .......
                                     ..1....
                                     .......
                                     .......
                                     .......
                                     .......
                                     """,
    )
Ejemplo n.º 29
0
def test_mirroring():
    # Test 'M' for mirroring
    mop = ImageMorph.MorphOp(
        patterns=["1:(... ... ...)->0", "M:(00. 01. ...)->1"])
    count, Aout = mop.apply(A)
    assert count == 7
    assert_img_equal_img_string(
        Aout,
        """
                                     .......
                                     .......
                                     ..1.1..
                                     .......
                                     .......
                                     .......
                                     .......
                                     """,
    )
Ejemplo n.º 30
0
    width = test_image.size[0]
    height = test_image.size[1]

    # načtení fontu
    font = ImageFont.truetype('FreeMono.ttf', 100)

    # vykreslení jednoduchého textu
    draw.text((70, height / 2 - 50), "Pillow", font=font, fill=255)

    # vzorek pro LUT
    patterns = ["1:(1)->0", "1:(0)->1"]

    # vytvoření objektu pro morfologické operace
    lutBuilder = ImageMorph.LutBuilder(patterns=patterns)
    lut = lutBuilder.build_lut()

    # aplikace morfologické operace
    morphOp = ImageMorph.MorphOp(lut=lut)
    pixels, updated_image = morphOp.apply(test_image)

    # zobrazení původního i upraveného obrázku
    test_image.show()
    updated_image.show()

    # uložení původního i upraveného obrázku
    test_image.save("42_bitmap.png")
    updated_image.save("42_updated.png")

except Exception as e:
    print("Vyjimka: " + e.__str__())