Skip to content

Commit 97aa07c

Browse files
committed
rgba images : resized alpha channel fix (graphdeco-inria/gaussian-splatting#1193)
1 parent cc22e66 commit 97aa07c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

utils/general_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ def inverse_sigmoid(x):
1919
return torch.log(x/(1-x))
2020

2121
def PILtoTorch(pil_image, resolution):
22+
# Adapted from: https://github.com/graphdeco-inria/gaussian-splatting/pull/1193
23+
# Original author: @ndming
24+
# License: For non-commercial, research and evaluation use (see LICENSE.md)
25+
26+
# When resizing RGBA, PIL pre-multiplies the resulting RGB with the resized alpha channel. This gives
27+
# different training behaviors depending on whether the image is actually resized (via -r flag) or not.
28+
# Moreover, the resized alpha is no longer a perfect binary image due to interpolation, which produces
29+
# a significant amount of floaters along the edges. To fix this, we manually mask the RGB if the input
30+
# is an RGBA, then we forget the alpha channel entirely. The multiplication of the rendered image with
31+
# the alpha_mask during training thus becomes a no-op for RGBA.
32+
if pil_image.mode == 'RGBA':
33+
from PIL import Image
34+
image_np = np.array(pil_image)
35+
rgb_np = image_np[..., :3]
36+
alpha_np = image_np[..., 3:]
37+
masked_rgb_np = (rgb_np / 255.0) * (alpha_np / 255.0)
38+
masked_rgb_np = np.clip(masked_rgb_np, 0.0, 1.0)
39+
pil_image = Image.fromarray((masked_rgb_np * 255).astype(np.uint8))
40+
2241
resized_image_PIL = pil_image.resize(resolution)
2342
resized_image = torch.from_numpy(np.array(resized_image_PIL)) / 255.0
2443
if len(resized_image.shape) == 3:

0 commit comments

Comments
 (0)