def test_area_opening_on_test_image(self): name, ext = os.path.splitext(self.TEST_IMAGE['path']) try: os.mkdir(self.TEST_OUT) except OSError: # Hope it just already exists pass test_out = join(self.TEST_OUT, '%s%s%s' % (basename(name), '_AREA_OPENING', ext)) test_intermediate = join(self.TEST_OUT, '%s%s%s' % (basename(name), '_area_opening', ext)) try: self.i.save(test_intermediate) structElemBuilder = SquaredStructuralElementBuilder(15) structElem = structElemBuilder.get_struct_elem() areaOpening = AreaOpening(StructuralElement.predefined('diagonal5'), 529, 109) i = areaOpening(self.i) i.save(test_out) finally: try: pass #os.unlink(test_out) #os.unlink(test_intermidiate) except EnvironmentError: pass
def test_dilation(self): """ Test the dilation operator """ from morphlib.operator import Dilation, StructuralElement dilate = Dilation(StructuralElement.predefined('rhombus')) original = ImageMock() # Set a high value to the second pixel to the right original[0][1] = 255 result = dilate(original) self.assertEquals(result.size, original.size) # Make sure that the pixel left to the highest-valued pixel was switched to the bumped pixel's value # (since dilation takes the maximum) self.assertEquals(result[0][0], original[0][1])
def test_geodesic_dilation(self): """ Test geodesic dilation operator """ from morphlib.operator import GeodesicDilation, StructuralElement original = ImageMock() mask = ImageMock() mask[0][0] = 1 mask[0][2] = 255 original[0][1] = 255 dilate = GeodesicDilation(StructuralElement.predefined('rhombus'), mask=mask) res = dilate(original) self.assertEquals(res[0][0], 1) self.assertEquals(res[0][2], original[0][1])
def test_reconstruction_by_dilation(self): """ Test reconstruction by dilation operator """ from morphlib.operator import ReconstructionByDilation, StructuralElement original = ImageMock() mask = ImageMock() # Create a noop mask for i in xrange(mask.height): for j in xrange(mask.width): mask[i][j]=255 original[0][0] = 1 reconstruct = ReconstructionByDilation( StructuralElement.predefined('rhombus'), mask=mask) res = reconstruct(original) # A very basic assert that we've "reconstructed" the first pixel self.assertEquals(res[0][0], 250)
def test_erosion(self): """ Test the erosion operator """ from morphlib.operator import Erosion, StructuralElement erode = Erosion(StructuralElement.predefined('octagon')) original = ImageMock() # Set the highest value to the pixel in the top left corner original[0][0] = 255 # Set some low value to the second pixel on the second row original[1][1] = 1 result = erode(original) self.assertEquals(result.size, original.size) # Make sure that the value of the second pixel on the second row is switched to 0 # (erosion takes the minimum) self.assertEquals(result[0][0], original[0][1]) self.assertEquals(result[1][1], original[0][1])
def test_closing_on_test_image(self): name, ext = os.path.splitext(self.TEST_IMAGE['path']) try: os.mkdir(self.TEST_OUT) except OSError: # Hope it just already exists pass test_out = join(self.TEST_OUT, '%s%s%s' % (basename(name), '_CLOSING_test', ext)) test_intermediate = join(self.TEST_OUT, '%s%s%s' % (basename(name), '_grayscale', ext)) try: self.i.save(test_intermediate) closing = Closing(StructuralElement.predefined('rhombus')) i = closing(self.i) i.save(test_out) finally: try: pass #os.unlink(test_out) #os.unlink(test_intermidiate) except EnvironmentError: pass
def test_erosion_on_test_image(self): name, ext = os.path.splitext(self.TEST_IMAGE['path']) try: os.mkdir(self.TEST_OUT) except OSError: # Hope it already exists pass test_out = join(self.TEST_OUT, '%s%s%s' % (basename(name), '_erosion_test', ext)) test_intermediate = join(self.TEST_OUT, '%s%s%s' % (basename(name), '_grayscale2', ext)) try: self.i.save(test_intermediate) erode = Erosion(StructuralElement.predefined('diagonal5')) i = erode(self.i) i.save(test_out) finally: try: pass #os.unlink(test_out) #os.unlink(test_intermidiate) except EnvironmentError: pass
def reconstruct_by_dilation(self, i, mask): dilate = ReconstructionByDilation(StructuralElement.predefined('rhombus'), mask=mask) return self.image_to_tk(dilate(i))
def closing(self, i): closing = Closing(StructuralElement.predefined('rhombus')) return self.image_to_tk(closing(i))
def opening(self, i): opening = Opening(StructuralElement.predefined('rhombus')) return self.image_to_tk(opening(i))
def dilated(self, i): dilate = Dilation(StructuralElement.predefined('rhombus')) return self.image_to_tk(dilate(i))