Spaces:
Sleeping
Sleeping
Create clean_refine.py
Browse files- clean_refine.py +71 -0
clean_refine.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def apply_gaussian_blur(mask, kernel_size=5):
|
| 5 |
+
"""Apply Gaussian blur to smooth the mask edges."""
|
| 6 |
+
return cv2.GaussianBlur(mask, (kernel_size, kernel_size), 0)
|
| 7 |
+
|
| 8 |
+
def apply_threshold(mask, threshold=127):
|
| 9 |
+
"""Apply binary threshold to sharpen the mask."""
|
| 10 |
+
_, binary_mask = cv2.threshold(mask, threshold, 255, cv2.THRESH_BINARY)
|
| 11 |
+
return binary_mask
|
| 12 |
+
|
| 13 |
+
def refine_edges(mask, kernel_size=3):
|
| 14 |
+
"""Refine edges using morphological operations."""
|
| 15 |
+
kernel = np.ones((kernel_size, kernel_size), np.uint8)
|
| 16 |
+
eroded = cv2.erode(mask, kernel, iterations=1)
|
| 17 |
+
dilated = cv2.dilate(mask, kernel, iterations=1)
|
| 18 |
+
refined = dilated - eroded
|
| 19 |
+
return cv2.bitwise_or(eroded, refined)
|
| 20 |
+
|
| 21 |
+
def apply_contour_smoothing(mask):
|
| 22 |
+
"""Smooth contours of the mask."""
|
| 23 |
+
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 24 |
+
smooth_mask = np.zeros_like(mask)
|
| 25 |
+
for contour in contours:
|
| 26 |
+
epsilon = 0.02 * cv2.arcLength(contour, True)
|
| 27 |
+
approx = cv2.approxPolyDP(contour, epsilon, True)
|
| 28 |
+
cv2.drawContours(smooth_mask, [approx], 0, 255, -1)
|
| 29 |
+
return smooth_mask
|
| 30 |
+
|
| 31 |
+
def refine_mask(mask, blur_kernel=5, edge_kernel=3, threshold_value=127):
|
| 32 |
+
"""Apply a series of refinement operations to the mask."""
|
| 33 |
+
mask = apply_gaussian_blur(mask, blur_kernel)
|
| 34 |
+
mask = apply_threshold(mask, threshold_value)
|
| 35 |
+
mask = refine_edges(mask, edge_kernel)
|
| 36 |
+
mask = apply_contour_smoothing(mask)
|
| 37 |
+
return mask
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def apply_morphology(mask, kernel_size=3):
|
| 41 |
+
"""Apply morphological operations to clean up the mask."""
|
| 42 |
+
kernel = np.ones((kernel_size, kernel_size), np.uint8)
|
| 43 |
+
|
| 44 |
+
# Opening operation to remove small noise
|
| 45 |
+
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
|
| 46 |
+
|
| 47 |
+
# Closing operation to fill small holes
|
| 48 |
+
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
|
| 49 |
+
|
| 50 |
+
return mask
|
| 51 |
+
|
| 52 |
+
def remove_small_objects(mask, min_size=100):
|
| 53 |
+
"""Remove small objects from the mask based on area."""
|
| 54 |
+
# Ensure mask is binary and single channel
|
| 55 |
+
if len(mask.shape) > 2:
|
| 56 |
+
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
|
| 57 |
+
_, binary_mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
|
| 58 |
+
|
| 59 |
+
num_labels, labels, stats, _ = cv2.connectedComponentsWithStats(binary_mask, connectivity=8)
|
| 60 |
+
|
| 61 |
+
for i in range(1, num_labels): # Start from 1 to skip the background
|
| 62 |
+
if stats[i, cv2.CC_STAT_AREA] < min_size:
|
| 63 |
+
binary_mask[labels == i] = 0
|
| 64 |
+
|
| 65 |
+
return binary_mask
|
| 66 |
+
|
| 67 |
+
def clean_mask(mask, morph_kernel_size=3, min_object_size=100):
|
| 68 |
+
"""Apply both morphological operations and small object removal."""
|
| 69 |
+
mask = apply_morphology(mask, kernel_size=morph_kernel_size)
|
| 70 |
+
mask = remove_small_objects(mask, min_size=min_object_size)
|
| 71 |
+
return mask
|