Mughees Ul Haq

Work 08 / 18

Replacing grey skies in exterior photos

Exterior photos taken on a grey day get a new sky. The hard part is the mask, because windows and reflections look like sky to a segmentation model. I built on an open-source sky-segmentation project and halved the model size.

For
A property-media platform
Public photos of buildings under grey skies, before and after sky replacement.OriginalNew sky

The sky mask covers 43% of the photo.

Public photos of buildings under grey skies, before and after sky replacement.OriginalNew sky

The sky mask covers 32% of the photo.

Sky replacement on public photos, without the refinement stage, which I did not have locally. Drag to compare. The mask covers 43% of the first photo and 32% of the second.

Photos: Arbury Hall (34337212276) by Elliott Brown from Birmingham, United Kingdom, CC BY-SA 2.0; Arbury Hall (34378442995) by Elliott Brown from Birmingham, United Kingdom, CC BY-SA 2.0. What I changed.

Model size
324.4 → 163.7 MB
FP16 export, no visible loss
Mask stages
3
Coarse, guided filter, refinement
Coarse mask input
320 px
Refined at full size
Accuracy benchmark
none
No labelled test set exists

How the mask is made

A segmentation model at 320 × 320 gives a coarse sky mask. A fast guided filter sharpens it, and a refinement stage recovers fine detail such as branches and roof edges. The masks are fused by confidence, using edge detection, exponential weighting and a bilateral filter.

The refinement is trusted only where the coarse mask agrees with it. That one rule is what stops windows and reflections from becoming sky.

  1. Coarse mask320 × 320 segmentation
  2. Guided filterFast, edge-aware
  3. RefinementFine detail, only where the coarse mask agrees
  4. Fused alphaConfidence-weighted
Three masks, one alpha channel.

Placing the new sky

Vanishing-point detection and the lowest sky pixel set the horizon, so the new sky sits at the right height. The fused mask becomes the alpha channel. If no replacement sky is chosen, the stage denoises the existing sky instead.

Limits

There is no labelled test set for this model, so I have no accuracy figure to quote. Vanishing-point detection fails on flat walls with no strong lines, and the coarse mask is limited by its 320-pixel input.

Tools

PyTorch, ONNX Runtime (FP16), OpenCV.

Builds on the open-source Sky-Segmentation-and-Post-processing project by xiongzhu666.

The full write-up

There is a longer write-up of this project. It has client details in it, so email me if you’d like a redacted copy.