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

scikit-image中RAG合并后出现黑色区域的问题咨询

RAG合并高阈值时出现黑色区域的根源解决问题

我参考scikit-image的RAG合并示例编写代码,当阈值设置过高时,RAG合并后的图像出现黑色区域。按照文档逻辑,相似区域应被合并,但实际部分RAG区域仿佛被完全移除。我的需求是先用SLIC分割出大量区域以保留结构细节,再将其合并为更大区域。

完整脚本

import click
import numpy as np
import skimage as ski
from PIL import Image


def _weight_mean_color(graph, src, dst, n):
    diff = graph.nodes[dst]["mean color"] - graph.nodes[n]["mean color"]
    diff = np.linalg.norm(diff)
    return {"weight": diff}


def merge_mean_color(graph, src, dst):
    graph.nodes[dst]["total color"] += graph.nodes[src]["total color"]
    graph.nodes[dst]["pixel count"] += graph.nodes[src]["pixel count"]
    graph.nodes[dst]["mean color"] = (
        graph.nodes[dst]["total color"] / graph.nodes[dst]["pixel count"]
    )


@click.command()
@click.version_option()
@click.option(
    "--path",
    type=str,
    required=True,
    help="path to an image",
)
def main(path: str) -> None:
    print(f"loading image {path}")
    pil_img = Image.open(path)
    # pil_img = pil_img.resize((pil_img.size[0] // 4, pil_img.size[1] // 4)) # downsize during testing to reduce waiting times
    input_image = np.asarray(pil_img)

    print("making slic")
    slic = ski.segmentation.slic(
        image=input_image, n_segments=5000, compactness=0.1, sigma=0.95, slic_zero=True
    )
    slic_rgb = ski.util.img_as_ubyte(
        ski.color.label2rgb(label=slic, image=input_image, kind="avg")
    )
    result = Image.fromarray(slic_rgb)
    result.save("results/slic_rgb.png")

    print("making rag")
    rag = ski.graph.rag_mean_color(input_image, slic)
    rag_thresh_cut = ski.graph.merge_hierarchical(
        labels=slic,
        rag=rag,
        thresh=32, # no issues here with a lower thresh, e.g.8
        rag_copy=False,
        in_place_merge=True,
        merge_func=merge_mean_color,
        weight_func=_weight_mean_color,
    )
    rag_rgb = ski.color.label2rgb(rag_thresh_cut, slic_rgb, kind="avg")
    result = Image.fromarray(rag_rgb)
    result.save("results/rag_rgb.png")

示例图像

  • SLIC分割图像局部:
    SLIC分割图像局部
  • RAG合并后图像局部(注意顶部中心的黑色区域):
    RAG合并后图像局部

补充说明

我已找到部分问题原因:上述代码中rag_thresh_cut包含标签0,被skimage.color.label2rgb()识别为默认背景色(黑色),若将背景色设为红色,该区域会变为红色。临时解决方法是给rag_thresh_cut加1,但想了解如何从根源避免该问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 14:05:32