Added smooth edges function
Browse files
app.py
CHANGED
|
@@ -121,7 +121,7 @@ def crop_signature(original_image_path, mask, padding=32):
|
|
| 121 |
cropped_mask = binary_mask[y_padded:y_padded+h_padded, x_padded:x_padded+w_padded]
|
| 122 |
|
| 123 |
# Apply smoothing and denoising to the cropped mask
|
| 124 |
-
smooth_denoised_mask =
|
| 125 |
|
| 126 |
# Create an RGBA image with a black background and the denoised mask as the alpha channel
|
| 127 |
mask_image = Image.new('RGBA', (w_padded, h_padded), (0, 0, 0))
|
|
@@ -200,6 +200,29 @@ def smooth_and_denoise(mask):
|
|
| 200 |
return denoised_mask
|
| 201 |
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
def predict(net, inputs_val, shapes_val, hypar, device):
|
| 205 |
'''
|
|
|
|
| 121 |
cropped_mask = binary_mask[y_padded:y_padded+h_padded, x_padded:x_padded+w_padded]
|
| 122 |
|
| 123 |
# Apply smoothing and denoising to the cropped mask
|
| 124 |
+
smooth_denoised_mask = smooth_edges(cropped_mask)
|
| 125 |
|
| 126 |
# Create an RGBA image with a black background and the denoised mask as the alpha channel
|
| 127 |
mask_image = Image.new('RGBA', (w_padded, h_padded), (0, 0, 0))
|
|
|
|
| 200 |
return denoised_mask
|
| 201 |
|
| 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 |
+
"""
|
| 209 |
+
# Convert mask to uint8 type if it isn't already
|
| 210 |
+
mask = mask.astype(np.uint8)
|
| 211 |
+
|
| 212 |
+
# Define a kernel for morphological operations
|
| 213 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 214 |
+
|
| 215 |
+
# Use morphological close operation to close small holes in the mask
|
| 216 |
+
closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=2)
|
| 217 |
+
|
| 218 |
+
# Use morphological open operation to remove noise
|
| 219 |
+
opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel, iterations=2)
|
| 220 |
+
|
| 221 |
+
# Dilate the mask to make the signature slightly thicker
|
| 222 |
+
dilated = cv2.dilate(opening, kernel, iterations=1)
|
| 223 |
+
|
| 224 |
+
return dilated
|
| 225 |
+
|
| 226 |
|
| 227 |
def predict(net, inputs_val, shapes_val, hypar, device):
|
| 228 |
'''
|