【图像分割】理论篇(1)评估指标代码实现

图像分割是计算机视觉中的重要任务,用于将图像中的不同区域分割成具有语义意义的区域。以下是几种常用的图像分割评价指标以及它们的代码实现示例(使用Python和常见的计算机视觉库):

1. IoU (Intersection over Union)

与目标检测中的IoU类似,用于衡量预测分割区域与真实分割区域之间的重叠程度。

示意图:

计算公式:

计算示例:

def calculate_iou(mask_true, mask_pred):
    intersection = np.logical_and(mask_true, mask_pred)
    union = np.logical_or(mask_true, mask_pred)
    iou = np.sum(intersection) / np.sum(union)
    return iou


#e.g.

import cv2
import numpy as np

mask_true=cv2.imread("round_meter_421.png",0)
mask_pred=cv2.imread("round_meter_423.png",0)

mask_true=cv2.resize(mask_true,(512,512),interpolation = cv2.INTER_LINEAR)
mask_pred=cv2.resize(mask_pred,(512,512),interpolation = cv2.INTER_LINEAR)
def calculate_iou(mask_true, mask_pred):
    intersection = np.logical_and(mask_true, mask_pred)
    union = np.logical_or(mask_true, mask_pred)
    iou = np.sum(intersection) / np.sum(union)
    return iou

print(calculate_iou(mask_true,mask_pred))
#结果0.6660

2. Dice Coefficient

用于衡量预测分割区域与真实分割区域的重叠程度。

但实际上该公式是由两个公式得到,即真阳性率(TPR)和阳性预测值(PPV)的调和平均数(harmonic mean)得到:

发现其实和IOU有一点相似,我们可以在改写一下:

def calculate_dice_coefficient(mask_true, mask_pred):
    intersection = np.logical_and(mask_true, mask_pred)
    dice_coeff = (2.0 * np.sum(intersection)) / (np.sum(mask_true) + np.sum(mask_pred))
    return dice_coeff

#e.g.

import cv2
import numpy as np

mask_true=cv2.imread("round_meter_421.png",0)
mask_pred=cv2.imread("round_meter_423.png",0)

mask_true=cv2.resize(mask_true,(512,512),interpolation = cv2.INTER_LINEAR)
mask_true = np.where(mask_true != 0, 1, mask_true)
mask_pred=cv2.resize(mask_pred,(512,512),interpolation = cv2.INTER_LINEAR)
mask_pred = np.where(mask_pred != 0, 1, mask_pred)
def calculate_dice_coefficient(mask_true, mask_pred):
    intersection = np.logical_and(mask_true, mask_pred)
    dice_coeff = (2.0 * np.sum(intersection)) / (np.sum(mask_true) + np.sum(mask_pred))
    return dice_coeff


print(calculate_dice_coefficient(mask_true,mask_pred))
#结果是 0.7995

3. Pixel Accuracy:

计算正确预测的像素数量占总像素数量的比例。

def calculate_pixel_accuracy(mask_true, mask_pred):
    correct_pixels = np.sum(mask_true == mask_pred)
    total_pixels = mask_true.size
    pixel_accuracy = correct_pixels / total_pixels
    return pixel_accuracy


#e.g.

import cv2
import numpy as np

mask_true=cv2.imread("round_meter_421.png",0)
mask_pred=cv2.imread("round_meter_423.png",0)

mask_true=cv2.resize(mask_true,(512,512),interpolation = cv2.INTER_LINEAR)
mask_true = np.where(mask_true != 0, 1, mask_true)
mask_pred=cv2.resize(mask_pred,(512,512),interpolation = cv2.INTER_LINEAR)
mask_pred = np.where(mask_pred != 0, 1, mask_pred)
def calculate_pixel_accuracy(mask_true, mask_pred):
    correct_pixels = np.sum(mask_true == mask_pred)
    total_pixels = mask_true.size
    pixel_accuracy = correct_pixels / total_pixels
    return pixel_accuracy


print(calculate_pixel_accuracy(mask_true,mask_pred))
#结果是 0.9914

4. Mean Intersection over Union (mIoU)

计算在不同类别上的平均IoU值。

def calculate_miou(class_iou_list):
    return np.mean(class_iou_list)

5. Boundary F1-score:

用于衡量分割区域的边界的预测质量。

def calculate_boundary_f1(mask_true, mask_pred):
    # Calculate true positive, false positive, and false negative boundary pixels
    true_positive = np.sum(np.logical_and(mask_true, mask_pred))
    false_positive = np.sum(np.logical_and(np.logical_not(mask_true), mask_pred))
    false_negative = np.sum(np.logical_and(mask_true, np.logical_not(mask_pred)))

    precision = true_positive / (true_positive + false_positive)
    recall = true_positive / (true_positive + false_negative)
    f1_score = 2 * (precision * recall) / (precision + recall)
    return f1_score


#e.g.

import cv2
import numpy as np

mask_true=cv2.imread("round_meter_421.png",0)
mask_pred=cv2.imread("round_meter_423.png",0)

mask_true=cv2.resize(mask_true,(512,512),interpolation = cv2.INTER_LINEAR)
mask_true = np.where(mask_true != 0, 1, mask_true)
mask_pred=cv2.resize(mask_pred,(512,512),interpolation = cv2.INTER_LINEAR)
mask_pred = np.where(mask_pred != 0, 1, mask_pred)
def calculate_boundary_f1(mask_true, mask_pred):
    # Calculate true positive, false positive, and false negative boundary pixels
    true_positive = np.sum(np.logical_and(mask_true, mask_pred))
    false_positive = np.sum(np.logical_and(np.logical_not(mask_true), mask_pred))
    false_negative = np.sum(np.logical_and(mask_true, np.logical_not(mask_pred)))

    precision = true_positive / (true_positive + false_positive)
    recall = true_positive / (true_positive + false_negative)
    f1_score = 2 * (precision * recall) / (precision + recall)
    return f1_score


print(calculate_boundary_f1(mask_true,mask_pred))
#结果是 0.7995

这些代码示例提供了基本的评价指标计算方法,实际应用中可能会涉及更多的细节和优化。使用深度学习框架(如TensorFlow、PyTorch)和计算机视觉库(如OpenCV、Scikit-image)可以更方便地计算这些评价指标,因为它们提供了丰富的内置函数和工具来处理图像分割任务。

  • 9
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
图像分割评估指标是用来衡量图像分割算法性能的重要指标。以下是一个示例代码,用于计算图像分割的常见评估指标,包括精确度(Accuracy)、召回率(Recall)、特异度(Specificity)、F1分数(F1 Score)和IoU(Intersection over Union)。 ```python import numpy as np def evaluate_segmentation(true_mask, pred_mask): true_mask = true_mask.astype(bool) pred_mask = pred_mask.astype(bool) # 计算真阳性、假阳性和假阴性 true_positive = np.logical_and(true_mask, pred_mask).sum() false_positive = np.logical_and(np.logical_not(true_mask), pred_mask).sum() false_negative = np.logical_and(true_mask, np.logical_not(pred_mask)).sum() # 计算精确度、召回率和特异度 accuracy = (true_positive + true_negative) / (true_positive + true_negative + false_positive + false_negative) recall = true_positive / (true_positive + false_negative) specificity = true_negative / (true_negative + false_positive) # 计算F1分数 precision = true_positive / (true_positive + false_positive) f1_score = 2 * (precision * recall) / (precision + recall) # 计算IoU intersection = np.logical_and(true_mask, pred_mask).sum() union = np.logical_or(true_mask, pred_mask).sum() iou = intersection / union return accuracy, recall, specificity, f1_score, iou # 示例用法 true_mask = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) pred_mask = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]]) accuracy, recall, specificity, f1_score, iou = evaluate_segmentation(true_mask, pred_mask) print("Accuracy:", accuracy) print("Recall:", recall) print("Specificity:", specificity) print("F1 Score:", f1_score) print("IoU:", iou) ``` 这段代码定义了一个`evaluate_segmentation`函数,该函数接受两个二值化的掩膜(true_mask和pred_mask),并计算出精确度、召回率、特异度、F1分数和IoU。在示例中,我们使用了一个3x3的真实掩膜和预测掩膜进行了计算,并打印了结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TechMasterPlus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值