Spaces:
Paused
Paused
File size: 4,603 Bytes
c7a38a3 | 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 | """Objects the wall mask wrongly includes (a frame's mount, a mirror), found after the AI has answered.
Segmentation sometimes labels a picture mount or a mirror reflecting the wall as
"wall", so the editor papers over it and the colour-based repair cannot tell it
apart. Two independent pieces of evidence must agree before such a region is
handed back to the photo:
- the original photo encloses it with its own edges, apart from the main wall, and
- the AI's answer shows the photo's structure there, not the wallpaper's print.
A sunbeam's edge can split the wall too, but the AI keeps the print under it, so it
stays. The only action is to show the original photo's pixels: nothing from the
answer is ever pasted.
"""
from __future__ import annotations
import cv2
import numpy as np
from services.enhance.composite import LUMA
HIGH_PASS_SIGMA = 6.0
WINDOW_SIGMA = 7.0
EDGE_LOW, EDGE_HIGH = 20, 50
EDGE_WIDTH = 5 # px, the dilated photo edges that separate regions
MIN_AREA = 0.0005 # of the image
MAX_AREA = 0.2 # of the layer
MAX_PRINT_CORRELATION = 0.3 # the answer shows the wallpaper's print this little ...
MAX_PRINT_SHARE = 0.4 # ... relative to the main wall
MIN_PHOTO_CORRELATION = 0.5 # and the photo's own structure this much
def _log_luma(rgb: np.ndarray) -> np.ndarray:
return np.log((rgb.astype(np.float32) / 255.0) @ LUMA + 0.02)
def _local_correlation(a: np.ndarray, b: np.ndarray) -> np.ndarray:
a = a - cv2.GaussianBlur(a, (0, 0), HIGH_PASS_SIGMA)
b = b - cv2.GaussianBlur(b, (0, 0), HIGH_PASS_SIGMA)
def mean(x: np.ndarray) -> np.ndarray:
return cv2.GaussianBlur(x, (0, 0), WINDOW_SIGMA)
ma, mb = mean(a), mean(b)
covariance = mean(a * b) - ma * mb
variance = np.maximum((mean(a * a) - ma * ma) * (mean(b * b) - mb * mb), 1e-8)
return covariance / np.sqrt(variance)
def wrongly_covered(original: np.ndarray, answer: np.ndarray, layer_rgb: np.ndarray, alpha: np.ndarray) -> tuple[np.ndarray, int]:
"""bool (H, W): pixels of this layer to give back to the photo; and how many regions."""
region = alpha > 0.5
found = np.zeros(region.shape, bool)
if region.sum() < 1000:
return found, 0
gray = cv2.GaussianBlur(cv2.cvtColor(original, cv2.COLOR_RGB2GRAY), (0, 0), 1.2)
edges = cv2.dilate((cv2.Canny(gray, EDGE_LOW, EDGE_HIGH) > 0).astype(np.uint8), np.ones((EDGE_WIDTH, EDGE_WIDTH), np.uint8)) > 0
count, labels, stats, _ = cv2.connectedComponentsWithStats((region & ~edges).astype(np.uint8), connectivity=4)
if count < 3:
return found, 0
answer_y = _log_luma(answer)
shows_print = _local_correlation(answer_y, _log_luma(layer_rgb))
shows_photo = _local_correlation(answer_y, _log_luma(original))
main = 1 + int(np.argmax(stats[1:, cv2.CC_STAT_AREA]))
main_print = float(np.median(shows_print[labels == main]))
regions = 0
for index in range(1, count):
area = stats[index, cv2.CC_STAT_AREA]
if index == main or area < MIN_AREA * region.size or area > MAX_AREA * region.sum():
continue
component = labels == index
print_score = float(np.median(shows_print[component]))
photo_score = float(np.median(shows_photo[component]))
if print_score < min(MAX_PRINT_CORRELATION, MAX_PRINT_SHARE * main_print) and photo_score > MIN_PHOTO_CORRELATION:
found |= component
regions += 1
if regions:
# The photo's edge pixels around them (the frame, the mirror's border) go back too,
# and so do the specks of wallpaper that would be left stranded between them.
reach = cv2.dilate(found.astype(np.uint8), np.ones((2 * EDGE_WIDTH + 1,) * 2, np.uint8)) > 0
found |= reach & edges & region
near = cv2.dilate(found.astype(np.uint8), np.ones((4 * EDGE_WIDTH + 1,) * 2, np.uint8)) > 0
count, labels, stats, _ = cv2.connectedComponentsWithStats((region & ~found).astype(np.uint8), connectivity=8)
areas = stats[:, cv2.CC_STAT_AREA]
near_counts = np.bincount(labels[near], minlength=count)
stranded = (areas < MIN_AREA * region.size) & (near_counts == areas)
stranded[0] = False
found |= stranded[labels]
return found, regions
def give_back(alpha: np.ndarray, found: np.ndarray) -> np.ndarray:
"""The layer's alpha without `found`, with a one-pixel soft edge (never adds coverage)."""
if not found.any():
return alpha
soft = cv2.GaussianBlur(found.astype(np.float32), (0, 0), 1.0)
return (alpha * (1.0 - np.maximum(soft, found.astype(np.float32)))).astype(np.float32)
|