ComfyUI_essentials>🔧 Mask From RGB/CMY/BW
ComfyUI_essentials
时间:2025/09/12

🔧 Mask From RGB/CMY/BW

用于 从一张 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-节点参数说明
输入参数
image必须参数,传入待处理的图像
输出参数
red输出根据threshold_x参数别到的红色区域遮罩
green输出根据threshold_x参数别到的绿色区域遮罩
blue输出根据threshold_x参数别到的蓝色区域遮罩
cyan输出根据threshold_x参数别到的青色区域遮罩
magenta输出根据threshold_x参数别到的品红色区域遮罩
yellow输出根据threshold_x参数别到的黄色区域遮罩
black输出根据threshold_x参数别到的黑色区域遮罩
white输出根据threshold_x参数别到的白色区域遮罩
控件参数
threshold_r红色通道阈值 默认0.15 范围0.0~1.0
threshold_g绿色通道阈值 默认0.15 范围0.0~1.0
threshold_b蓝色通道阈值 默认0.15 范围0.0~1.0
使用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()

       


广告

可加入知识星球获取所有示例工作流

广告

微信扫码入群,加入AIGC大家庭,与大家一起交流学习