批量图像分割评估脚本:使用Python和OpenCV

在计算机视觉任务中,图像分割是一项重要的任务,而对分割结果进行评估则是验证模型性能的关键一环。本文将介绍如何使用Python和OpenCV编写一个简单的批量图像分割评估脚本,以评估分割模型的性能。

1. 问题背景

假设我们有一组GT(Ground Truth)图像和相应的KMeans算法生成的分割图像。我们想要批量评估这些分割图像与GT图像之间的相似度,以便量化模型的性能。先装包

python 复制代码
pip install numpy opencv-python tqdm hausdorff

2. 脚本概览

首先,定义了一组评估指标,包括Dice系数、IoU(Intersection over Union)、灵敏度、PPV(Positive Predictive Value)以及Hausdorff距离的95th percentile。

python 复制代码
def dice_coef(predict: np.ndarray, label: np.ndarray, epsilon: float = 1e-5) -> float:
    predict, label = transform_image_data(predict, label)
    intersection = (predict * label).sum()
    return (2. * intersection + epsilon) / (predict.sum() + label.sum() + epsilon)

def iou_score(predict: np.ndarray, label: np.ndarray, epsilon: float = 1e-5) -> float:
    predict, label = transform_image_data(predict, label)
    intersection = (predict & label).sum()
    union = (predict | label).sum()
    return (intersection + epsilon) / (union + epsilon)

def sensitivity(predict: np.ndarray, label: np.ndarray, epsilon: float = 1e-5) -> float:
    predict, label = transform_image_data(predict, label)
    intersection = (predict * label).sum()
    return (intersection + epsilon) / (label.sum() + epsilon)

def ppv(predict: np.ndarray, label: np.ndarray, epsilon: float = 1e-5) -> float:
    predict, label = transform_image_data(predict, label)
    intersection = (predict * label).sum()
    return (intersection + epsilon) / (predict.sum() + epsilon)

def hd95(predict: np.ndarray, label: np.ndarray, distance="euclidean"):
    predict, label = transform_image_data(predict, label)
    predict = predict.flatten()[..., None]
    label = label.flatten()[..., None]
    distance = hausdorff.hausdorff_distance(predict, label, distance=distance)
    return distance * 0.95

然后,我们编写了一个函数,该函数接受包含GT和分割图像的文件夹路径,并返回每个图像的评估指标。

python 复制代码
def batch_evaluation(data_folder, extension='.png'):
    gt_files = glob.glob(data_folder + '/gt*' + extension)
    mask_files = glob.glob(data_folder + '/mask*' + extension)

    dice_scores = []
    iou_scores = []
    sensitivity_scores = []
    ppv_scores = []
    hd95_distances = []

    for gt_file, mask_file in tqdm(zip(gt_files, mask_files), total=len(gt_files)):
        gt_image = cv2.imread(gt_file, 0)
        mask_image = cv2.imread(mask_file, 0)

        dice_scores.append(dice_coef(mask_image, gt_image))
        iou_scores.append(iou_score(mask_image, gt_image))
        sensitivity_scores.append(sensitivity(mask_image, gt_image))
        ppv_scores.append(ppv(mask_image, gt_image))
        hd95_distances.append(hd95(mask_image, gt_image))

    return {
        'dice_scores': dice_scores,
        'iou_scores': iou_scores,
        'sensitivity_scores': sensitivity_scores,
        'ppv_scores': ppv_scores,
        'hd95_distances': hd95_distances
    }

最后,我们使用这个函数对指定文件夹中的所有图像进行评估,并输出结果。

python 复制代码
# 执行批量评估
evaluation_results = batch_evaluation(data_folder)

# 输出结果
print("Dice Scores:", evaluation_results['dice_scores'])
print("IOU Scores:", evaluation_results['iou_scores'])
print("Sensitivity Scores:", evaluation_results['sensitivity_scores'])
print("PPV Scores:", evaluation_results['ppv_scores'])
print("HD95 Distances:", evaluation_results['hd95_distances'])

完整脚本如下

python 复制代码
import glob
import cv2
import numpy as np
from tqdm import tqdm

def transform_image_data(predict: np.ndarray, label: np.ndarray):
    predict = predict.astype(np.bool_).astype(np.int_)
    label = label.astype(np.bool_).astype(np.int_)
    return predict, label

def dice_coef(predict: np.ndarray, label: np.ndarray, epsilon: float = 1e-5) -> float:
    predict, label = transform_image_data(predict, label)
    intersection = (predict * label).sum()
    return (2. * intersection + epsilon) / (predict.sum() + label.sum() + epsilon)

def iou_score(predict: np.ndarray, label: np.ndarray, epsilon: float = 1e-5) -> float:
    predict, label = transform_image_data(predict, label)
    intersection = (predict & label).sum()
    union = (predict | label).sum()
    return (intersection + epsilon) / (union + epsilon)

def sensitivity(predict: np.ndarray, label: np.ndarray, epsilon: float = 1e-5) -> float:
    predict, label = transform_image_data(predict, label)
    intersection = (predict * label).sum()
    return (intersection + epsilon) / (label.sum() + epsilon)

def ppv(predict: np.ndarray, label: np.ndarray, epsilon: float = 1e-5) -> float:
    predict, label = transform_image_data(predict, label)
    intersection = (predict * label).sum()
    return (intersection + epsilon) / (predict.sum() + epsilon)

def hd95(predict: np.ndarray, label: np.ndarray, distance="euclidean"):
    predict, label = transform_image_data(predict, label)
    predict = predict.flatten()[..., None]
    label = label.flatten()[..., None]
    distance = hausdorff.hausdorff_distance(predict, label, distance=distance)
    return distance * 0.95

def batch_evaluation(data_folder, extension='.png'):
    gt_files = glob.glob(data_folder + '/gt*' + extension)
    mask_files = glob.glob(data_folder + '/mask*' + extension)

    dice_scores = []
    iou_scores = []
    sensitivity_scores = []
    ppv_scores = []
    hd95_distances = []

    for gt_file, mask_file in tqdm(zip(gt_files, mask_files), total=len(gt_files)):
        gt_image = cv2.imread(gt_file, 0)
        mask_image = cv2.imread(mask_file, 0)

        dice_scores.append(dice_coef(mask_image, gt_image))
        iou_scores.append(iou_score(mask_image, gt_image))
        sensitivity_scores.append(sensitivity(mask_image, gt_image))
        ppv_scores.append(ppv(mask_image, gt_image))
        hd95_distances.append(hd95(mask_image, gt_image))

    return {
        'dice_scores': dice_scores,
        'iou_scores': iou_scores,
        'sensitivity_scores': sensitivity_scores,
        'ppv_scores': ppv_scores,
        'hd95_distances': hd95_distances
    }

# 指定包含图像文件的文件夹路径
data_folder = '/path/to/your/data/folder'

# 执行批量评估
evaluation_results = batch_evaluation(data_folder)

# 输出结果
print("Dice Scores:", evaluation_results['dice_scores'])
print("IOU Scores:", evaluation_results['iou_scores'])
print("Sensitivity Scores:", evaluation_results['sensitivity_scores'])
print("PPV Scores:", evaluation_results['ppv_scores'])
print("HD95 Distances:", evaluation_results['hd95_distances'])
相关推荐
杨荧几秒前
【JAVA毕业设计】基于Vue和SpringBoot的服装商城系统学科竞赛管理系统
java·开发语言·vue.js·spring boot·spring cloud·java-ee·kafka
白子寰7 分钟前
【C++打怪之路Lv14】- “多态“篇
开发语言·c++
yannan201903137 分钟前
【算法】(Python)动态规划
python·算法·动态规划
蒙娜丽宁17 分钟前
《Python OpenCV从菜鸟到高手》——零基础进阶,开启图像处理与计算机视觉的大门!
python·opencv·计算机视觉
光芒再现dev19 分钟前
已解决,部署GPTSoVITS报错‘AsyncRequest‘ object has no attribute ‘_json_response_data‘
运维·python·gpt·语言模型·自然语言处理
王俊山IT19 分钟前
C++学习笔记----10、模块、头文件及各种主题(一)---- 模块(5)
开发语言·c++·笔记·学习
为将者,自当识天晓地。21 分钟前
c++多线程
java·开发语言
小政爱学习!23 分钟前
封装axios、环境变量、api解耦、解决跨域、全局组件注入
开发语言·前端·javascript
好喜欢吃红柚子33 分钟前
万字长文解读空间、通道注意力机制机制和超详细代码逐行分析(SE,CBAM,SGE,CA,ECA,TA)
人工智能·pytorch·python·计算机视觉·cnn
小馒头学python37 分钟前
机器学习是什么?AIGC又是什么?机器学习与AIGC未来科技的双引擎
人工智能·python·机器学习