用于 从一张 RGB 图像中提取八种基本颜色的遮罩 (Mask),分别是:
R:红 (Red)
G:绿 (Green)
B:蓝 (Blue)
C:青 (Cyan)
M:品红 (Magenta)
Y:黄 (Yellow)
K:黑 (Black)
W:白 (White)
它的核心是 基于阈值判断像素属于哪个颜色区域,并返回 8 张二值化 Mask,每个 Mask 对应一个颜色区域。
使用Mask From RGB/CMY/BW节点对不同颜色自动识别遮罩,一般threshold_x参数设置为0.5即可适应大部分场景,具体细节需要微调测试
源码展示:
red = ((image[..., 0] >= 1-threshold_r) & (image[..., 1] < threshold_g) & (image[..., 2] < threshold_b)).float()
green = ((image[..., 0] < threshold_r) & (image[..., 1] >= 1-threshold_g) & (image[..., 2] < threshold_b)).float()
blue = ((image[..., 0] < threshold_r) & (image[..., 1] < threshold_g) & (image[..., 2] >= 1-threshold_b)).float()
cyan = ((image[..., 0] < threshold_r) & (image[..., 1] >= 1-threshold_g) & (image[..., 2] >= 1-threshold_b)).float()
magenta = ((image[..., 0] >= 1-threshold_r) & (image[..., 1] < threshold_g) & (image[..., 2] > 1-threshold_b)).float()
yellow = ((image[..., 0] >= 1-threshold_r) & (image[..., 1] >= 1-threshold_g) & (image[..., 2] < threshold_b)).float()
black = ((image[..., 0] <= threshold_r) & (image[..., 1] <= threshold_g) & (image[..., 2] <= threshold_b)).float()
white = ((image[..., 0] >= 1-threshold_r) & (image[..., 1] >= 1-threshold_g) & (image[..., 2] >= 1-threshold_b)).float()