Image segmentation is an important task in computer vision and is used to segment different regions in an image into semantically meaningful regions. The following are several commonly used image segmentation evaluation metrics and their code implementation examples (using Python and common computer vision libraries):
1. IoU (Intersection over Union)
Similar to IoU in target detection, it is used to measure the degree of overlap between the predicted segmentation area and the real segmentation area.
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. Say Coefficient
It is used to measure the degree of overlap between the predicted segmentation area and the real segmentation area.
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:
Calculate the ratio of the number of correctly predicted pixels to the total number of pixels.
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)
Calculate the average IoU value across different categories.
def calculate_miou(class_iou_list):
return np.mean(class_iou_list)
5. Boundary F1-score:
A measure of the prediction quality of the boundaries that separate regions.
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
These code examples provide basic evaluation index calculation methods, and actual applications may involve more details and optimization. It is easier to calculate these evaluation metrics using deep learning frameworks (such as TensorFlow, PyTorch) and computer vision libraries (such as OpenCV, Scikit-image), as they provide rich built-in functions and tools to handle image segmentation tasks.