File size: 3,427 Bytes
c225a63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Filename: ComfyUI_INSTARAW/modules/detection_bypass/utils/lbp_normalization.py
import numpy as np
from skimage.feature import local_binary_pattern
from PIL import Image
import os

def lbp_normalize(img_arr: np.ndarray,

                  ref_img_arr: np.ndarray = None,

                  profile_path: str = None, # NEW: Added profile_path

                  radius: int = 3,

                  n_points: int = 24,

                  method: str = 'uniform',

                  strength: float = 0.9,

                  seed: int = None,

                  eps: float = 1e-8):
    
    if seed is not None: rng = np.random.default_rng(seed)
    else: rng = np.random.default_rng()

    img = np.asarray(img_arr)
    h, w, channels = img.shape
    
    img_gray = np.mean(img.astype(np.float32), axis=2)
    
    lbp_img = local_binary_pattern(img_gray, n_points, radius, method=method)
    n_bins = int(lbp_img.max() + 1)
    lbp_hist, _ = np.histogram(lbp_img.ravel(), bins=n_bins, range=(0, n_bins), density=True)

    ref_lbp_hist = None
    
    # --- NEW LOGIC: PRIORITIZE PROFILE ---
    if profile_path and profile_path.strip():
        npz_path = f"{profile_path}.npz"
        if os.path.exists(npz_path):
            stats = np.load(npz_path)
            if 'lbp_hist' in stats:
                # Load the average LBP histogram from the profile
                avg_hist = stats['lbp_hist'].mean(axis=0)
                # Match the bin count of the source image's LBP
                if len(avg_hist) > n_bins:
                    ref_lbp_hist = avg_hist[:n_bins]
                else:
                    ref_lbp_hist = np.pad(avg_hist, (0, n_bins - len(avg_hist)), 'constant')
                ref_lbp_hist /= (ref_lbp_hist.sum() + eps) # Re-normalize

    # Fallback to live reference image
    if ref_lbp_hist is None and ref_img_arr is not None:
        ref = np.asarray(ref_img_arr)
        if ref.shape[0] != h or ref.shape[1] != w:
            ref = np.array(Image.fromarray(ref).resize((w, h), resample=Image.Resampling.BICUBIC))
        
        ref_gray = np.mean(ref.astype(np.float32), axis=2)
        ref_lbp = local_binary_pattern(ref_gray, n_points, radius, method=method)
        ref_lbp_hist, _ = np.histogram(ref_lbp.ravel(), bins=n_bins, range=(0, n_bins), density=True)
    # --- END NEW LOGIC ---

    if ref_lbp_hist is None:
        return img_arr

    out = np.empty_like(img, dtype=np.float32)
    
    cdf_img = np.cumsum(lbp_hist)
    cdf_ref = np.cumsum(ref_lbp_hist)
    
    mapping = np.zeros(n_bins, dtype=np.float32)
    for i in range(n_bins):
        # Find the value in reference cdf that is closest to the source cdf value
        j = np.searchsorted(cdf_ref, cdf_img[i], side='left')
        mapping[i] = j
    
    lbp_int = np.rint(lbp_img).astype(np.int32)
    mapping_per_pixel = mapping[lbp_int]
    scale_per_pixel = mapping_per_pixel / (lbp_int.astype(np.float32) + eps)
    
    noise_all = rng.normal(loc=0.0, scale=0.02 * strength, size=(h, w, channels)).astype(np.float32) * 255.0

    for c in range(channels):
        channel = img[:, :, c].astype(np.float32)
        adjusted = channel * scale_per_pixel
        blended = (1.0 - strength) * channel + strength * adjusted
        blended += noise_all[:, :, c]
        out[:, :, c] = blended

    return np.clip(out, 0, 255).astype(np.uint8)