Smooth edges
Browse files
app.py
CHANGED
|
@@ -202,7 +202,7 @@ def smooth_and_denoise(mask):
|
|
| 202 |
|
| 203 |
def smooth_edges(mask):
|
| 204 |
"""
|
| 205 |
-
Smooth edges of a binary mask using morphological operations.
|
| 206 |
:param mask: The binary mask of the signature.
|
| 207 |
:return: Mask with smoothed edges.
|
| 208 |
"""
|
|
@@ -221,7 +221,19 @@ def smooth_edges(mask):
|
|
| 221 |
# Dilate the mask to make the signature slightly thicker
|
| 222 |
dilated = cv2.dilate(opening, kernel, iterations=1)
|
| 223 |
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
|
| 227 |
def predict(net, inputs_val, shapes_val, hypar, device):
|
|
|
|
| 202 |
|
| 203 |
def smooth_edges(mask):
|
| 204 |
"""
|
| 205 |
+
Smooth edges of a binary mask using morphological operations and anti-aliasing resizing.
|
| 206 |
:param mask: The binary mask of the signature.
|
| 207 |
:return: Mask with smoothed edges.
|
| 208 |
"""
|
|
|
|
| 221 |
# Dilate the mask to make the signature slightly thicker
|
| 222 |
dilated = cv2.dilate(opening, kernel, iterations=1)
|
| 223 |
|
| 224 |
+
# Convert dilated mask to a PIL image for anti-aliasing resizing
|
| 225 |
+
pil_mask = Image.fromarray(dilated)
|
| 226 |
+
|
| 227 |
+
# Resize the image to a smaller size, then back to the original size
|
| 228 |
+
small_size = (pil_mask.width // 2, pil_mask.height // 2)
|
| 229 |
+
pil_mask_small = pil_mask.resize(small_size, Image.ANTIALIAS)
|
| 230 |
+
pil_mask_smooth = pil_mask_small.resize(pil_mask.size, Image.ANTIALIAS)
|
| 231 |
+
|
| 232 |
+
# Convert back to a numpy array
|
| 233 |
+
smoothed_mask = np.array(pil_mask_smooth)
|
| 234 |
+
final_mask = cv2.bilateralFilter(mask, d=9, sigmaColor=75, sigmaSpace=75)
|
| 235 |
+
|
| 236 |
+
return final_mask
|
| 237 |
|
| 238 |
|
| 239 |
def predict(net, inputs_val, shapes_val, hypar, device):
|