File size: 7,230 Bytes
1bd13a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""
Post-Processing Module for Bone Tumor Segmentation Masks.

Provides:
- Dense CRF refinement using image intensity gradients
- Morphological refinement (closing + opening)
- Combined pipeline for maximum boundary accuracy

These post-processing steps are applied after SAM2 inference
to sharpen mask boundaries, especially in low-contrast X-ray regions.
"""

import numpy as np
import cv2
from typing import Optional, Tuple


def morphological_refine(mask: np.ndarray,
                          close_kernel: int = 7,
                          open_kernel: int = 5,
                          fill_holes: bool = True) -> np.ndarray:
    """
    Morphological refinement of a binary segmentation mask.
    
    Applies closing (fill small gaps) followed by opening (remove noise),
    then optionally fills interior holes.
    
    Args:
        mask: (H, W) binary mask (uint8, 0/1)
        close_kernel: Kernel size for morphological closing
        open_kernel: Kernel size for morphological opening
        fill_holes: Whether to fill interior holes
        
    Returns:
        Refined binary mask (H, W), uint8
    """
    mask_binary = (mask > 0).astype(np.uint8) * 255
    
    # Closing: fills small gaps and connects nearby regions
    k_close = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (close_kernel, close_kernel))
    closed = cv2.morphologyEx(mask_binary, cv2.MORPH_CLOSE, k_close)
    
    # Opening: removes small noise blobs
    k_open = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (open_kernel, open_kernel))
    opened = cv2.morphologyEx(closed, cv2.MORPH_OPEN, k_open)
    
    if fill_holes:
        # Flood fill from corners to find exterior, then invert
        h, w = opened.shape
        flood = opened.copy()
        flood_mask = np.zeros((h + 2, w + 2), dtype=np.uint8)
        cv2.floodFill(flood, flood_mask, (0, 0), 255)
        holes = cv2.bitwise_not(flood)
        opened = cv2.bitwise_or(opened, holes)
    
    return (opened > 127).astype(np.uint8)


def crf_refine(image: np.ndarray,
               mask_prob: np.ndarray,
               n_iterations: int = 5,
               sxy_gauss: int = 3,
               compat_gauss: int = 3,
               sxy_bilateral: int = 80,
               srgb_bilateral: int = 13,
               compat_bilateral: int = 10) -> np.ndarray:
    """
    Dense CRF refinement using image appearance.
    
    Uses pydensecrf to refine mask boundaries using both spatial
    smoothness and image intensity information, particularly effective
    on X-rays where bone-tissue transitions are gradual.
    
    Args:
        image: (H, W, 3) RGB image, uint8
        mask_prob: (H, W) mask probability [0, 1] or binary {0, 1}
        n_iterations: CRF inference iterations
        sxy_gauss: Spatial sigma for appearance-independent smoothness
        compat_gauss: Compatibility for Gaussian kernel
        sxy_bilateral: Spatial sigma for bilateral kernel
        srgb_bilateral: Color sigma for bilateral kernel
        compat_bilateral: Compatibility for bilateral kernel
        
    Returns:
        Refined binary mask (H, W), uint8
    """
    try:
        import pydensecrf.densecrf as dcrf
        from pydensecrf.utils import unary_from_softmax
    except ImportError:
        print("[PostProcess] pydensecrf not installed, falling back to morphological refinement")
        return morphological_refine((mask_prob > 0.5).astype(np.uint8))
    
    h, w = mask_prob.shape
    
    # Ensure mask_prob is float in [0, 1]
    if mask_prob.max() > 1:
        mask_prob = mask_prob.astype(np.float32) / 255.0
    
    # Create 2-class softmax input (background, foreground)
    prob_fg = np.clip(mask_prob, 0.001, 0.999).astype(np.float32)
    prob_bg = 1.0 - prob_fg
    probs = np.stack([prob_bg, prob_fg], axis=0)  # (2, H, W)
    
    # Create CRF
    d = dcrf.DenseCRF2D(w, h, 2)
    
    # Unary potentials from softmax probabilities
    unary = unary_from_softmax(probs)
    d.setUnaryEnergy(unary)
    
    # Pairwise Gaussian (spatial smoothness)
    d.addPairwiseGaussian(sxy=sxy_gauss, compat=compat_gauss)
    
    # Pairwise bilateral (appearance-aware smoothness)
    img_for_crf = np.ascontiguousarray(image[:, :, :3].astype(np.uint8))
    d.addPairwiseBilateral(
        sxy=sxy_bilateral,
        srgb=srgb_bilateral,
        rgbim=img_for_crf,
        compat=compat_bilateral
    )
    
    # Inference
    Q = d.inference(n_iterations)
    result = np.argmax(Q, axis=0).reshape(h, w)
    
    return result.astype(np.uint8)


def box_blend_refine(mask: np.ndarray, box: tuple, alpha: float = 0.0) -> np.ndarray:
    """
    Refine a binary mask by blending it with the bounding box rectangle.
    Useful for weakly annotated rectangular ground truth datasets.
    
    Args:
        mask: (H, W) binary mask (uint8)
        box: (x1, y1, x2, y2) bounding box in pixel coordinates
        alpha: Weight of the rectangular box prior [0.0, 1.0]
        
    Returns:
        Refined binary mask (H, W), uint8
    """
    if alpha <= 0.0:
        return mask
        
    h, w = mask.shape
    box_mask = np.zeros((h, w), dtype=np.float32)
    x1, y1, x2, y2 = box
    
    # Clamp bounding box coordinates to image dimensions
    x1, y1 = max(0, int(x1)), max(0, int(y1))
    x2, y2 = min(w - 1, int(x2)), min(h - 1, int(y2))
    
    box_mask[y1:y2+1, x1:x2+1] = 1.0
    
    blended = (1.0 - alpha) * mask.astype(np.float32) + alpha * box_mask
    return (blended > 0.5).astype(np.uint8)


def refine_mask(image: np.ndarray,
                mask: np.ndarray,
                use_crf: bool = True,
                use_morphology: bool = True,
                box: Optional[Tuple[int, int, int, int]] = None,
                box_blend_alpha: float = 0.0) -> np.ndarray:
    """
    Combined post-processing pipeline.
    
    Applies CRF refinement, morphological cleanup, and optional BBox Prior Fusion.
    
    Args:
        image: (H, W, 3) RGB image
        mask: (H, W) binary mask or probability map
        use_crf: Apply CRF refinement
        use_morphology: Apply morphological cleanup
        box: Bounding box for prior fusion
        box_blend_alpha: Weight of the rectangular box prior
        
    Returns:
        Refined binary mask (H, W), uint8
    """
    result = mask.copy()
    
    if use_crf:
        result = crf_refine(image, result.astype(np.float32))
    
    if use_morphology:
        result = morphological_refine(result)
        
    if box is not None and box_blend_alpha > 0.0:
        result = box_blend_refine(result, box, alpha=box_blend_alpha)
    
    return result


if __name__ == '__main__':
    # Quick test
    print("Testing post-processing module...")
    
    # Synthetic noisy mask
    mask = np.zeros((256, 256), dtype=np.uint8)
    rr, cc = np.ogrid[:256, :256]
    mask[((rr - 128)**2 + (cc - 128)**2) < 60**2] = 1
    
    # Add noise
    noise = np.random.random((256, 256)) > 0.95
    mask_noisy = np.clip(mask + noise.astype(np.uint8), 0, 1)
    
    # Morphological refinement
    refined = morphological_refine(mask_noisy)
    print(f"  Original area: {mask.sum()}")
    print(f"  Noisy area: {mask_noisy.sum()}")
    print(f"  Refined area: {refined.sum()}")
    print("  Done!")