60 """Create feathering masks for all edges of the patch.
64 dimensions : `tuple` of `int`
65 Shape of the patch (height, width) for which to create feathering masks.
69 This method creates four feathering masks (top, bottom, left, right) using
70 linear ramps from 0 to 1 (or 1 to 0) over the `patch_grow` distance. The
71 featherings are stored in `self.featherings` as a list of arrays.
75 extent = int(np.floor(extent / self.
bin_factor))
76 ramp = np.linspace(0, 1, extent)
78 top = np.ones(dimensions)
79 top[:extent, :] = np.repeat(np.expand_dims(ramp, 1), top.shape[1], axis=1)
81 bottom = np.ones(dimensions)
82 bottom[-1 * extent :, :] = np.repeat(
83 np.expand_dims(1 - ramp, 1), bottom.shape[1], axis=1
86 left = np.ones(dimensions)
87 left[:, :extent] = np.repeat(np.expand_dims(ramp, 0), left.shape[0], axis=0)
89 right = np.ones(dimensions)
90 right[:, -1 * extent :] = np.repeat(
91 np.expand_dims(1 - ramp, 0), right.shape[0], axis=0
101 self, image: NDArray, patch: NDArray, newBox: Box2I, box: Box2I, reverse: bool =
True
103 """Add a patch to an image with feathering at the edges.
108 Target image to which the patch will be added. Modified in-place.
110 Patch array to be added to the image.
112 New bounding box position of the patch.
114 Original bounding box position of the patch.
115 reverse : `bool`, optional
116 If True, reverse the patch along the first axis before adding.
121 This method applies feathering to smoothly blend the patch into the image
122 at the edges where the patch overlaps with existing image content. The
123 feathering is applied based on which edges of the patch differ between
124 `box` and `newBox`. The patch is multiplied by a mixer array that gradually
125 transitions from 0 to 1 across the feathering region.
127 base_shape = patch.shape
if patch.ndim == 2
else patch.shape[:2]
128 mixer = np.ones(base_shape)
131 if box.getBeginY() != newBox.getBeginY():
133 if box.getEndY() != newBox.getEndY():
135 if box.getBeginX() != newBox.getBeginX():
137 if box.getEndX() != newBox.getEndX():
141 mixer = np.repeat(np.expand_dims(mixer, 2), 3, axis=2)
143 patch = mixer * patch
145 image[*box.slices] += patch[::-1, :, :]
if reverse
else patch