如何从二进制掩码生成 trimap?

How do I generate a trimap from a binary mask?

我现在正在尝试实施这篇论文:

Automatic Skin and Hair Masking Using Convolutional Neural Networks

我已经让 FCN 和 CRF 部分正常工作,并且我找到了在我拥有 trimap 后生成 alpha 掩码的代码。

不过,我卡在了 (c) 和 (d) 之间。

如何在给定二进制掩码的情况下生成 trimap?论文说:

We apply morphological operators on the binary segmentation mask for hair and skin, obtaining a trimap that indicates foreground (hair/skin), background and unknown pixels. In order to deal with segmentation inaccuracies, and to best capture the appearance variance of both foreground and background, we first erode the binary mask with a small kernel, then extract the skeleton pixels as part of foreground constrain pixels. We also erode the binary mask with a larger kernel to get more foreground constrain pixels. The final foreground constrain pixels is the union of the two parts. If we only keep the second part then some thin hair regions will be gone after erosion with a large kernel. If a pixel is outside the dilated mask then we take it as background constrain pixel. All other pixels as marked as unknown, see figure 2 (d).

OpenCV 支持形态学运算。
请参阅 this tutorial explaining how to use erode and dilate 函数。