You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Segment-Anything分割掩码结果以单一颜色可视化?

如何将Segment-Anything分割掩码统一设为黑色绘制?

你可以通过指定sv.MaskAnnotator的color参数为黑色,实现所有掩码统一用黑色绘制。具体修改如下:

sv.MaskAnnotator默认会为每个掩码分配随机颜色,只需在初始化时传入黑色的BGR值(OpenCV采用BGR色彩空间,黑色对应(0, 0, 0))即可覆盖默认行为。

修改后的完整代码:

import cv2
import supervision as sv
import numpy as np

sam_result = np.load('sam_result_multy1.npy', allow_pickle=True)
print(sam_result[0].keys())

IMAGE_PATH = 'img_mult_sk.png'
image_bgr = cv2.imread(IMAGE_PATH)

# 初始化MaskAnnotator时指定黑色
mask_annotator = sv.MaskAnnotator(color=(0, 0, 0))
detections = sv.Detections.from_sam(sam_result=sam_result)
annotated_image = mask_annotator.annotate(scene=image_bgr.copy(), detections=detections)

sv.plot_images_grid(
    images=[image_bgr, annotated_image],
    grid_size=(1, 2),
    titles=['source image', 'segmented image']
)

如果需要同时调整掩码的透明度,可以额外设置opacity参数(比如opacity=0.5),让黑色掩码与原图更好地融合。

内容的提问来源于stack exchange,提问作者brownser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 16:19:55