Example #1
0
    def calc_patch_mask(self, src, dst, coord=None, overlap=None):
        """
        Calculates the blend mask between two patches according to the min cut
        in their top and left overlapping areas.
        It computes the min-cuts of the difference between the two patches both
        in their top and left overlapping areas. Then it uses them to build the
        blend mask of the patch.
                     ___________
                    |/\_/\_/\_/_| min-cut in the top overlap
            min-cut |\|         |
             in the |/|  PATCH  |
               left |\|         |
            overlap |_|_________|


        Args:
            src: "background" patch
            dst: "foreground" patch
            coord: coordinate in tile-size
            overlap: depth of the overlap regions

        Returns:
            blend mask: 0 where background, 1 where foreground
        """
        [i, j] = coord
        overlap = overlap or self.overlap

        # Initialize the mask to all ones
        mask = np.ones((src.shape[0], src.shape[1]))

        # if it is the first one, or if src==dst, or is dst is empty
        if (i == 0 and j == 0) or np.all(src == dst) or not np.any(dst):
            return mask

        # We have a left overlap
        if j > 0:

            # difference
            diff = power(rgb2gray(src[:, :overlap] - dst[:, :overlap]), 2)
            # min-cut
            cut = mincut(diff, 0)
            # find the mask
            mask[:, :overlap] = cut

        # We have a top overlap
        if i > 0:

            # difference
            diff = power(rgb2gray(src[:overlap, :] - dst[:overlap, :]), 2)
            # min-cut
            cut = mincut(diff, 1)
            # find the mask
            mask[:overlap, :] = multiply(mask[:overlap, :], cut)

        # Write to the destination using the mask
        return mask
Example #2
0
 def test_size(self):
     """
     Test the returned matrix has the same shape of the input one
     """
     a = np.array([[30, 39, 48, 1, 10, 19, 28], [38, 47, 7, 9, 18, 27, 29],
                   [46, 6, 8, 17, 26, 35, 37], [5, 14, 16, 25, 34, 36, 45],
                   [13, 15, 24, 33, 42, 44, 4]])
     result = mincut(a)
     np.testing.assert_array_equal(a.shape, result.shape)
Example #3
0
 def test_values(self):
     """
     Test the returned matrix contains only 0 and 1 values.
     """
     a = np.array([[30, 39, 48, 1, 10, 19, 28], [38, 47, 7, 9, 18, 27, 29],
                   [46, 6, 8, 17, 26, 35, 37], [5, 14, 16, 25, 34, 36, 45],
                   [13, 15, 24, 33, 42, 44, 4]])
     result = mincut(a)
     np.testing.assert_array_equal([0, 1], np.unique(result))
Example #4
0
 def test_small(self):
     """
     Test expected output with a small matrix.
     """
     a = np.array([[7, 8, 9, 0, 0, 6], [5, 9, 0, 4, 3, 3],
                   [8, 1, 3, 0, 6, 6]])
     result = mincut(a)
     expected = np.asarray([[0, 0, 0, 0, 0, 1], [0, 0, 0, 1, 1, 1],
                            [0, 0, 0, 0, 1, 1]])
     self.assertEqual(expected.shape, result.shape)
     np.testing.assert_array_equal(expected, result)
Example #5
0
 def test_transpose(self):
     """
     Test expected output with inverted matrix direction.
     """
     a = np.array([[30, 39, 48, 1, 10, 19, 28], [38, 47, 7, 9, 18, 27, 29],
                   [46, 6, 8, 17, 26, 35, 37], [5, 14, 16, 25, 34, 36, 45],
                   [13, 15, 24, 33, 42, 44, 4]])
     result = mincut(a, direction=1)
     expected = np.asarray([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1],
                            [0, 0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 1],
                            [1, 1, 1, 1, 1, 1, 1]])
     self.assertEqual(expected.shape, result.shape)
     np.testing.assert_array_equal(expected, result)