def __run_boundary_stawiaski_test(self, label, gradient, expected_result, msg=''): label = numpy.asarray(label) gradient = numpy.asarray(gradient) graph = GCGraphTest( numpy.unique(label).size, math.pow(numpy.unique(label).size, 2)) boundary_stawiaski(graph, label, (gradient)) graph.validate_nweights(self, expected_result, msg)
def test_boundary_stawiaski_3d(self): """Test the @link medpy.graphcut.test_boundary_stawiaski() function for 3D.""" # the gradient magnitude image gradient = [[[0., 0., 0., 0.1, 0.1, 0.5], [0., 0., 0., 0.1, 0.1, 0.1], [0., 0.3, 0.3, 0.2, 0.2, 0.2], [0., 0., 0.3, 0.3, 0.3, 0.2], [0., 0., 0.3, 0.2, 0.2, 0.4]], [[0., 0., 0., 0.1, 0.1, 0.6], [0., 0., 0., 0.1, 0.1, 0.1], [0., 0.3, 0.3, 0.2, 0.2, 0.2], [0., 0., 0.3, 0.3, 0.3, 0.2], [0., 0., 0.3, 0.2, 0.2, 0.4]]] # the label image (labels have to start from 0) label = [[[0, 0, 0, 1, 1, 5], [0, 0, 0, 1, 1, 1], [0, 3, 3, 2, 2, 2], [0, 0, 3, 3, 3, 2], [0, 0, 3, 2, 2, 4]], [[0, 0, 0, 1, 1, 6], [0, 0, 0, 1, 1, 1], [0, 3, 3, 2, 2, 2], [0, 0, 3, 3, 3, 2], [0, 0, 3, 2, 2, 4]]] # the expected result expected_result = { (0, 1): 3.305785124, (0, 3): 7.100591712, (1, 2): 4.166666666, (1, 5): 0.888888889, (1, 6): 0.78125, (2, 3): 8.284023668, (2, 4): 2.040816326, (5, 6): 0.390625 } # only 3D edge expected_result = self.__to_two_directed(expected_result) # run the function result = boundary_stawiaski(label, (gradient)) # check returned values self.__compare_dictionaries(result, expected_result)
def test_boundary_stawiaski_2d(self): """Test the @link medpy.graphcut.test_boundary_stawiaski() function for 2D.""" # the gradient magnitude image gradient = [ [0.0, 0.0, 0.0, 0.1, 0.1, 0.5], [0.0, 0.0, 0.0, 0.1, 0.1, 0.1], [0.0, 0.3, 0.3, 0.2, 0.2, 0.2], [0.0, 0.0, 0.3, 0.3, 0.3, 0.2], [0.0, 0.0, 0.3, 0.2, 0.2, 0.4], ] # the label image (labels have to start from 0) label = [[0, 0, 0, 1, 1, 5], [0, 0, 0, 1, 1, 1], [0, 3, 3, 2, 2, 2], [0, 0, 3, 3, 3, 2], [0, 0, 3, 2, 2, 4]] # the expected result expected_result = { (0, 1): 1.652892562, (0, 3): 3.550295856, (1, 2): 2.083333333, (1, 5): 0.888888889, (2, 3): 4.142011834, (2, 4): 1.020408163, } expected_result = self.__to_two_directed(expected_result) # run the function result = boundary_stawiaski(label, (gradient)) # check returned values self.__compare_dictionaries(result, expected_result)
def test_boundary_stawiaski_3d(self): """Test the @link medpy.graphcut.test_boundary_stawiaski() function for 3D.""" # the gradient magnitude image gradient = [ [ [0.0, 0.0, 0.0, 0.1, 0.1, 0.5], [0.0, 0.0, 0.0, 0.1, 0.1, 0.1], [0.0, 0.3, 0.3, 0.2, 0.2, 0.2], [0.0, 0.0, 0.3, 0.3, 0.3, 0.2], [0.0, 0.0, 0.3, 0.2, 0.2, 0.4], ], [ [0.0, 0.0, 0.0, 0.1, 0.1, 0.6], [0.0, 0.0, 0.0, 0.1, 0.1, 0.1], [0.0, 0.3, 0.3, 0.2, 0.2, 0.2], [0.0, 0.0, 0.3, 0.3, 0.3, 0.2], [0.0, 0.0, 0.3, 0.2, 0.2, 0.4], ], ] # the label image (labels have to start from 0) label = [ [[0, 0, 0, 1, 1, 5], [0, 0, 0, 1, 1, 1], [0, 3, 3, 2, 2, 2], [0, 0, 3, 3, 3, 2], [0, 0, 3, 2, 2, 4]], [[0, 0, 0, 1, 1, 6], [0, 0, 0, 1, 1, 1], [0, 3, 3, 2, 2, 2], [0, 0, 3, 3, 3, 2], [0, 0, 3, 2, 2, 4]], ] # the expected result expected_result = { (0, 1): 3.305785124, (0, 3): 7.100591712, (1, 2): 4.166666666, (1, 5): 0.888888889, (1, 6): 0.78125, (2, 3): 8.284023668, (2, 4): 2.040816326, (5, 6): 0.390625, } # only 3D edge expected_result = self.__to_two_directed(expected_result) # run the function result = boundary_stawiaski(label, (gradient)) # check returned values self.__compare_dictionaries(result, expected_result)
def test_boundary_stawiaski_2d(self): """Test the @link medpy.graphcut.test_boundary_stawiaski() function for 2D.""" # the gradient magnitude image gradient = [[0., 0., 0., 0.1, 0.1, 0.5], [0., 0., 0., 0.1, 0.1, 0.1], [0., 0.3, 0.3, 0.2, 0.2, 0.2], [0., 0., 0.3, 0.3, 0.3, 0.2], [0., 0., 0.3, 0.2, 0.2, 0.4]] # the label image (labels have to start from 0) label = [[0, 0, 0, 1, 1, 5], [0, 0, 0, 1, 1, 1], [0, 3, 3, 2, 2, 2], [0, 0, 3, 3, 3, 2], [0, 0, 3, 2, 2, 4]] # the expected result expected_result = { (0, 1): 1.652892562, (0, 3): 3.550295856, (1, 2): 2.083333333, (1, 5): 0.888888889, (2, 3): 4.142011834, (2, 4): 1.020408163 } expected_result = self.__to_two_directed(expected_result) # run the function result = boundary_stawiaski(label, (gradient)) # check returned values self.__compare_dictionaries(result, expected_result)
def test_boundary_stawiaski_borders(self): """Test the @link medpy.graphcut.test_boundary_stawiaski() border conditions.""" # TEST1: test for a label image with not continuous label ids not starting from 0 label = [[1, 4, 8], [1, 3, 10], [1, 3, 10]] expected_result = { (1, 3): (2.0, 2.0), (1, 4): (1.0, 1.0), (4, 8): (1.0, 1.0), (3, 4): (1.0, 1.0), (3, 10): (2.0, 2.0), (8, 10): (1.0, 1.0) } result = boundary_stawiaski(label, (scipy.zeros_like(label))) self.__compare_dictionaries(result, expected_result, 'Test1') # TEST2: test for a label image with negative labels label = [[-1, 4, 8], [-1, 3, 10], [1, -3, 10]] expected_result = { (-1, 1): (1.0, 1.0), (4, 8): (1.0, 1.0), (-1, 3): (1.0, 1.0), (3, 10): (1.0, 1.0), (-3, 10): (1.0, 1.0), (8, 10): (1.0, 1.0), (-3, 1): (1.0, 1.0), (-3, 3): (1.0, 1.0), (-1, 4): (1.0, 1.0), (3, 4): (1.0, 1.0) } result = boundary_stawiaski(label, (scipy.zeros_like(label))) self.__compare_dictionaries(result, expected_result, 'Test2') # TEST3: test for behavior on occurrence of very small (~0) and 1 weights gradient = [[0., 0., 0.], [0., 0., sys.float_info.max]] label = [[0, 1, 2], [0, 1, 3]] expected_result = { (0, 1): (2.0, 2.0), (1, 2): (1.0, 1.0), (1, 3): (sys.float_info.min, sys.float_info.min), (2, 3): (sys.float_info.min, sys.float_info.min) } result = boundary_stawiaski(label, (gradient)) self.__compare_dictionaries(result, expected_result, 'Test3') # TEST4: check behavior for integer gradient image label = [[1, 4, 8], [1, 3, 10], [1, 3, 10]] label = scipy.asarray(label) expected_result = { (1, 3): (2.0, 2.0), (1, 4): (1.0, 1.0), (4, 8): (1.0, 1.0), (3, 4): (1.0, 1.0), (3, 10): (2.0, 2.0), (8, 10): (1.0, 1.0) } result = boundary_stawiaski(label, (scipy.zeros(label.shape, scipy.int_))) self.__compare_dictionaries(result, expected_result, 'Test4') # TEST5: reaction to different array orders label = [[1, 4, 8], [1, 3, 10], [1, 3, 10]] label = scipy.asarray(label, order='C') # C-order, gradient same order expected_result = { (1, 3): (2.0, 2.0), (1, 4): (1.0, 1.0), (4, 8): (1.0, 1.0), (3, 4): (1.0, 1.0), (3, 10): (2.0, 2.0), (8, 10): (1.0, 1.0) } result = boundary_stawiaski(label, (scipy.zeros_like(label))) self.__compare_dictionaries(result, expected_result, 'Test5 (C,C)') label = scipy.asarray(label, order='F') # Fortran order, gradient same order expected_result = { (1, 3): (2.0, 2.0), (1, 4): (1.0, 1.0), (4, 8): (1.0, 1.0), (3, 4): (1.0, 1.0), (3, 10): (2.0, 2.0), (8, 10): (1.0, 1.0) } result = boundary_stawiaski(label, (scipy.zeros_like(label))) self.__compare_dictionaries(result, expected_result, 'Test5 (F, F)') label = scipy.asarray(label, order='C') # C-order, gradient different order expected_result = { (1, 3): (2.0, 2.0), (1, 4): (1.0, 1.0), (4, 8): (1.0, 1.0), (3, 4): (1.0, 1.0), (3, 10): (2.0, 2.0), (8, 10): (1.0, 1.0) } result = boundary_stawiaski(label, (scipy.zeros(label.shape, order='F'))) self.__compare_dictionaries(result, expected_result, 'Test5 (C, F)') label = scipy.asarray(label, order='F') # F-order, gradient different order expected_result = { (1, 3): (2.0, 2.0), (1, 4): (1.0, 1.0), (4, 8): (1.0, 1.0), (3, 4): (1.0, 1.0), (3, 10): (2.0, 2.0), (8, 10): (1.0, 1.0) } result = boundary_stawiaski(label, (scipy.zeros(label.shape, order='C'))) self.__compare_dictionaries(result, expected_result, 'Test5 (F, C)')
def test_boundary_stawiaski_borders(self): """Test the @link medpy.graphcut.test_boundary_stawiaski() border conditions.""" # TEST1: test for a label image with not continuous label ids not starting from 0 label = [[1, 4, 8], [1, 3, 10], [1, 3, 10]] expected_result = { (1, 3): (2.0, 2.0), (1, 4): (1.0, 1.0), (4, 8): (1.0, 1.0), (3, 4): (1.0, 1.0), (3, 10): (2.0, 2.0), (8, 10): (1.0, 1.0), } result = boundary_stawiaski(label, (scipy.zeros_like(label))) self.__compare_dictionaries(result, expected_result, "Test1") # TEST2: test for a label image with negative labels label = [[-1, 4, 8], [-1, 3, 10], [1, -3, 10]] expected_result = { (-1, 1): (1.0, 1.0), (4, 8): (1.0, 1.0), (-1, 3): (1.0, 1.0), (3, 10): (1.0, 1.0), (-3, 10): (1.0, 1.0), (8, 10): (1.0, 1.0), (-3, 1): (1.0, 1.0), (-3, 3): (1.0, 1.0), (-1, 4): (1.0, 1.0), (3, 4): (1.0, 1.0), } result = boundary_stawiaski(label, (scipy.zeros_like(label))) self.__compare_dictionaries(result, expected_result, "Test2") # TEST3: test for behavior on occurrence of very small (~0) and 1 weights gradient = [[0.0, 0.0, 0.0], [0.0, 0.0, sys.float_info.max]] label = [[0, 1, 2], [0, 1, 3]] expected_result = { (0, 1): (2.0, 2.0), (1, 2): (1.0, 1.0), (1, 3): (sys.float_info.min, sys.float_info.min), (2, 3): (sys.float_info.min, sys.float_info.min), } result = boundary_stawiaski(label, (gradient)) self.__compare_dictionaries(result, expected_result, "Test3") # TEST4: check behavior for integer gradient image label = [[1, 4, 8], [1, 3, 10], [1, 3, 10]] label = scipy.asarray(label) expected_result = { (1, 3): (2.0, 2.0), (1, 4): (1.0, 1.0), (4, 8): (1.0, 1.0), (3, 4): (1.0, 1.0), (3, 10): (2.0, 2.0), (8, 10): (1.0, 1.0), } result = boundary_stawiaski(label, (scipy.zeros(label.shape, scipy.int_))) self.__compare_dictionaries(result, expected_result, "Test4") # TEST5: reaction to different array orders label = [[1, 4, 8], [1, 3, 10], [1, 3, 10]] label = scipy.asarray(label, order="C") # C-order, gradient same order expected_result = { (1, 3): (2.0, 2.0), (1, 4): (1.0, 1.0), (4, 8): (1.0, 1.0), (3, 4): (1.0, 1.0), (3, 10): (2.0, 2.0), (8, 10): (1.0, 1.0), } result = boundary_stawiaski(label, (scipy.zeros_like(label))) self.__compare_dictionaries(result, expected_result, "Test5 (C,C)") label = scipy.asarray(label, order="F") # Fortran order, gradient same order expected_result = { (1, 3): (2.0, 2.0), (1, 4): (1.0, 1.0), (4, 8): (1.0, 1.0), (3, 4): (1.0, 1.0), (3, 10): (2.0, 2.0), (8, 10): (1.0, 1.0), } result = boundary_stawiaski(label, (scipy.zeros_like(label))) self.__compare_dictionaries(result, expected_result, "Test5 (F, F)") label = scipy.asarray(label, order="C") # C-order, gradient different order expected_result = { (1, 3): (2.0, 2.0), (1, 4): (1.0, 1.0), (4, 8): (1.0, 1.0), (3, 4): (1.0, 1.0), (3, 10): (2.0, 2.0), (8, 10): (1.0, 1.0), } result = boundary_stawiaski(label, (scipy.zeros(label.shape, order="F"))) self.__compare_dictionaries(result, expected_result, "Test5 (C, F)") label = scipy.asarray(label, order="F") # F-order, gradient different order expected_result = { (1, 3): (2.0, 2.0), (1, 4): (1.0, 1.0), (4, 8): (1.0, 1.0), (3, 4): (1.0, 1.0), (3, 10): (2.0, 2.0), (8, 10): (1.0, 1.0), } result = boundary_stawiaski(label, (scipy.zeros(label.shape, order="C"))) self.__compare_dictionaries(result, expected_result, "Test5 (F, C)")