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

astropy导入报错修复及裂缝分支彩色显示与交点检测需求

问题解决与实现方案

一、修复ImportError报错

fil-finder 1.7.2和astropy 6.x版本不兼容——astropy 6.x移除了update_default_config这个旧API,导致导入失败。解决方法二选一:

  • 降级astropy到兼容的5.x版本,执行命令:
    pip install astropy==5.3.4
    
  • 升级fil-finder到最新版本(若新版本已适配astropy 6.x),执行命令:
    pip install --upgrade fil-finder
    

二、完整实现代码(彩色分支+交点检测)

import numpy as np
import matplotlib.pyplot as plt
import cv2
from fil_finder import FilFinder2D
from astropy import units
from skimage.measure import label

# 加载并预处理骨架图像
skeleton = cv2.imread("./zhang.png", 0)
skeleton = (skeleton == 255).astype(np.uint8)  # 转为二值图像(1为骨架,0为背景)

# 初始化FilFinder并分析骨架
fil = FilFinder2D(skeleton, distance=250 * units.pc, mask=skeleton)
fil.preprocess_image(flatten_percent=85)
fil.create_mask(border_masking=True, verbose=False, use_existing_mask=True)
fil.medskel(verbose=False)
fil.analyze_skeletons(branch_thresh=40*units.pix, skel_thresh=10*units.pix, prune_criteria='length')

# 创建绘图窗口
plt.figure(figsize=(10, 10))

# 1. 给每个裂缝分支分配不同颜色
branch_labels = label(fil.skeleton, connectivity=2)  # 标记独立分支
color_map = plt.cm.get_cmap('tab20', np.max(branch_labels))  # 生成足够的颜色
for label_id in range(1, np.max(branch_labels)+1):
    y_coords, x_coords = np.where(branch_labels == label_id)
    plt.scatter(x_coords, y_coords, color=color_map(label_id), s=2)

# 2. 检测并标记裂缝交点
def get_intersections(skeleton_img):
    h, w = skeleton_img.shape
    intersections = []
    # 遍历图像(跳过边界)
    for i in range(1, h-1):
        for j in range(1, w-1):
            if skeleton_img[i, j] == 1:
                # 统计8邻域内的骨架像素数量
                neighbor_count = np.sum(skeleton_img[i-1:i+2, j-1:j+2])
                # 邻域内≥3个骨架像素即为交点(分支汇合处)
                if neighbor_count >= 3:
                    intersections.append((j, i))
    return np.array(intersections)

intersection_points = get_intersections(fil.skeleton)
# 用红色大圆点标记交点
plt.scatter(intersection_points[:, 0], intersection_points[:, 1], color='red', s=50, marker='o', label='裂缝交点')

plt.axis('off')
plt.legend()
plt.show()

代码说明

  • 分支彩色显示:用skimage.measure.label给每个独立分支分配唯一ID,再通过颜色映射给不同ID分配不同颜色,实现分支区分
  • 交点检测:遍历骨架像素,统计每个像素的8邻域骨架像素数,数量≥3的点就是分支交汇的交点,最后用红色圆点突出显示
  • 运行后会得到和参考图一致的彩色分支效果,同时标记出所有裂缝交点

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 04:57:24