Main Content

segmentationConfusionMatrix

多级像素级图像分割的混淆矩阵

Description

example

confusionMatrix= SemengemationConfusionMatrix(LPred,Ltruth)computes a confusion matrix from the predicted pixel labelsLPredand ground truth pixel labelsLtruth.

Examples

collapse all

Load a pretrained network that performs binary segmentation of triangles against a background.

load('triangleSegmentationNetwork');

The三角形图data set has 100 test images with ground truth labels. Define the location of the data set.

dataSetDir = fullfile(toolboxdir('vision'),'visiondata',“三角形图”);

得到一个测试图像。

testImage = imread(fullfile(dataSetDir,“证词”,'image_001.jpg');

Get the corresponding ground truth label. The label image is stored as a numeric image, so convert the ground truth label to a categorical image.

gtLabel = imread(fullfile(dataSetDir,'testLabels','labeled_image_001.png');classNames = ["triangle","background"];labelids = [255 0];gtlabel =分类(gtlabel,labelids,classNames);

Display the ground truth labels over the test image.

imshow(labeloverlay(testImage,gtLabel))

Figure contains an axes object. The axes object contains an object of type image.

Perform semantic image segmentation.

predLabel = semanticseg(testImage,net);

Get the confusion matrix of the predicted labels and ground truth labels. The confusion matrix shows the segmentation correctly classified 21 pixels as triangle and 939 pixels as background. The confusion matrix also shows that the segmentation misclassified 64 background pixels as triangle. No triangle pixels are misclassified as background.

confusionMatrix = segmentationConfusionMatrix(predLabel,gtLabel)
confusionMatrix =2×221 0 64 939

要检查混淆矩阵的准确性,请在测试图像上显示预测标签。覆盖层与混淆矩阵中的值一致。

imshow(labeloverlay(testImage,predLabel))

Figure contains an axes object. The axes object contains an object of type image.

Input Arguments

collapse all

Predicted pixel labels after semantic segmentation, specified as a 2-D label image or 3-D label image.

Data Types:double|logical|categorical

Ground truth pixel labels, specified as a 2-D label image or 3-D label image of the same size as the predicted pixel labels,LPred.

Data Types:double|logical|categorical

Output Arguments

collapse all

分段图像中类的混淆矩阵,返回为C-by-C数字矩阵,其中C是语义分割中的类数。元素 (i,j)是已知属于类的像素的数量ibut predicted to belong to classj.

Tips

  • You can calculate semantic segmentation metrics such as the Jaccard score and classification accuracy from the confusion matrix by using theevaluateSemanticSegmentationfunction.

  • You can use this function in block-based image processing workflows such asbigimage. For example, you can perform semantic segmentation and calculate the confusion matrix for a block in the same call to theapplyfunction.

Version History

Introduced in R2020b