Image Matting
Module: Computer Vision | Difficulty: Advanced
Matting Equation
where is foreground, is background, .
Loss Function
Trimap-Based Matting
GrabCut
Iterative graph cut for foreground extraction.
import torch
import torch.nn as nn
class MattingNet(nn.Module):
def __init__(self):
super().__init__()
self.encoder = nn.Sequential(
nn.Conv2d(4, 64, 3, padding=1), nn.ReLU(True),
nn.Conv2d(64, 128, 3, 2, 1), nn.ReLU(True),
nn.Conv2d(128, 256, 3, 2, 1), nn.ReLU(True),
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(256, 128, 4, 2, 1), nn.ReLU(True),
nn.ConvTranspose2d(128, 64, 4, 2, 1), nn.ReLU(True),
nn.Conv2d(64, 1, 3, padding=1), nn.Sigmoid(),
)
def forward(self, x, trimap):
inp = torch.cat([x, trimap], dim=1)
feat = self.encoder(inp)
return self.decoder(feat)
Key Takeaways
- Matting estimates per-pixel alpha values
- Trimap guides the matting algorithm to the transition region
- Deep matting methods can work without explicit trimaps