File size: 2,911 Bytes
56502de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""CPU-conscious, conservative alpha enhancement for Remova images and video.

Does not hallucinate missing objects; preserves high-confidence foreground and
avoids processing every frame with a second neural network.
"""
import cv2
import numpy as np
from PIL import Image


def enhance_rgba(result: Image.Image, source: Image.Image, model: str = 'silueta',
                 video: bool = False) -> Image.Image:
    """Edge-aware refinement restricted to uncertain alpha pixels."""
    rgba = np.asarray(result.convert('RGBA')).copy()
    alpha = rgba[:, :, 3].copy()
    if alpha.size < 64 or alpha.max() < 8 or alpha.min() > 247:
        return result
    guide = np.asarray(source.convert('RGB'))
    if guide.shape[:2] != alpha.shape:
        guide = cv2.resize(guide, (alpha.shape[1], alpha.shape[0]), interpolation=cv2.INTER_AREA)
    # A one-channel guided filter avoids colour-channel covariance artifacts.
    gray = cv2.cvtColor(guide, cv2.COLOR_RGB2GRAY).astype(np.float32) / 255.0
    p = alpha.astype(np.float32) / 255.0
    radius = 3 if video else 4
    kernel = (radius * 2 + 1, radius * 2 + 1)
    mean_i = cv2.boxFilter(gray, -1, kernel, normalize=True)
    mean_p = cv2.boxFilter(p, -1, kernel, normalize=True)
    cov = cv2.boxFilter(gray * p, -1, kernel, normalize=True) - mean_i * mean_p
    var = cv2.boxFilter(gray * gray, -1, kernel, normalize=True) - mean_i * mean_i
    a = cov / (var + 0.002)
    b = mean_p - a * mean_i
    refined = cv2.boxFilter(a, -1, kernel) * gray + cv2.boxFilter(b, -1, kernel)
    refined = np.clip(refined, 0, 1)
    # Only adjust the uncertain band. Do not erode hair or opaque body regions.
    uncertain = (alpha > 12) & (alpha < 243)
    strength = 0.42 if model == 'silueta' else 0.32
    if video:
        strength *= 0.8
    output = p.copy()
    output[uncertain] = (1 - strength) * p[uncertain] + strength * refined[uncertain]
    # Tiny isolated hard-mask speckles can be removed only when sufficiently
    # far from any substantial component. Keep multi-object subjects intact.
    if not video:
        core = (alpha >= 200).astype(np.uint8)
        count, labels, stats, _ = cv2.connectedComponentsWithStats(core, 8)
        if count > 2:
            large = np.zeros_like(core)
            threshold = max(16, int(alpha.size * 0.00008))
            for i in range(1, count):
                if stats[i, cv2.CC_STAT_AREA] >= threshold:
                    large[labels == i] = 1
            if large.any():
                near = cv2.dilate(large, np.ones((7, 7), np.uint8))
                for i in range(1, count):
                    if stats[i, cv2.CC_STAT_AREA] < threshold:
                        component = labels == i
                        if not np.any(near & component):
                            output[component] = 0
    rgba[:, :, 3] = np.uint8(np.clip(output * 255 + 0.5, 0, 255))
    return Image.fromarray(rgba, 'RGBA')