当前位置: 首页 > news >正文

【图像分割】理论篇(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 npmask_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 iouprint(calculate_iou(mask_true,mask_pred))
#结果0.6660

2. Dice Coefficient

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

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 npmask_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_coeffprint(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.sizepixel_accuracy = correct_pixels / total_pixelsreturn pixel_accuracy#e.g.import cv2
import numpy as npmask_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.sizepixel_accuracy = correct_pixels / total_pixelsreturn pixel_accuracyprint(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 pixelstrue_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 npmask_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 pixelstrue_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_scoreprint(calculate_boundary_f1(mask_true,mask_pred))
#结果是 0.7995

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

http://www.lryc.cn/news/133052.html

相关文章:

  • Git checkout 某个版本到指定文件夹下
  • Java多态详解(2)
  • Camtasia导入srt字幕乱码
  • YOLOv5、YOLOv8改进:SOCA注意力机制
  • 机器人的运动范围
  • 学习笔记|基于Delay实现的LED闪烁|模块化编程|SOS求救灯光|STC32G单片机视频开发教程(冲哥)|第六集(下):实现LED闪烁
  • 微服务-Ribbon(负载均衡)
  • 解决C#报“MSB3088 未能读取状态文件*.csprojAssemblyReference.cache“问题
  • GeoScene Pro在地图制图当中的应用
  • 国标混凝土结构设计规范的混凝土本构关系——基于python代码生成
  • 系统架构设计-架构师之路(八)
  • 【SA8295P 源码分析】25 - QNX Ethernet MAC 驱动 之 emac_isr_thread_handler 中断处理函数源码分析
  • 函数栈帧的创建与销毁
  • 工业安全生产平台在面粉行业的应用分享
  • Gitlab服务部署及应用
  • 【nodejs】用Node.js实现简单的壁纸网站爬虫
  • xlsx xlsx-style file-saver 导出json数据到excel文件并设置标题字体加粗
  • Win11游戏高性能模式怎么开
  • 深度学习最强奠基作ResNet《Deep Residual Learning for Image Recognition》论文解读(上篇)
  • 第22次CCF计算机软件能力认证
  • Go语言基础之基本数据类型
  • Linux Tracing Technologies
  • iOS自定义下拉刷新控件
  • Springboot写单元测试
  • 一篇文章教你使用Docker本地化部署Chatgpt(非api,速度非常快!!!)及裸连GPT的方式(告别镜像GPT)
  • 前馈神经网络dropout实例
  • Android DataStore:安全存储和轻松管理数据
  • opencv进阶12-EigenFaces 人脸识别
  • The internal rate of return (IRR)
  • 半导体自动化专用静电消除器主要由哪些部分组成