批量图像分割评估脚本:使用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'])
相关推荐
西猫雷婶5 分钟前
python学opencv|读取图像(二十一)使用cv2.circle()绘制圆形进阶
开发语言·python·opencv
kiiila5 分钟前
【Qt】对象树(生命周期管理)和字符集(cout打印乱码问题)
开发语言·qt
小_太_阳31 分钟前
Scala_【2】变量和数据类型
开发语言·后端·scala·intellij-idea
直裾34 分钟前
scala借阅图书保存记录(三)
开发语言·后端·scala
老刘莱国瑞39 分钟前
STM32 与 AS608 指纹模块的调试与应用
python·物联网·阿里云
湫ccc41 分钟前
《Opencv》基础操作详解(3)
人工智能·opencv·计算机视觉
唐 城1 小时前
curl 放弃对 Hyper Rust HTTP 后端的支持
开发语言·http·rust
一只敲代码的猪1 小时前
Llama 3 模型系列解析(一)
大数据·python·llama
Hello_WOAIAI2 小时前
批量将 Word 文件转换为 HTML:Python 实现指南
python·html·word
winfredzhang2 小时前
使用Python开发PPT图片提取与九宫格合并工具
python·powerpoint·提取·九宫格·照片