jaccard
Jaccard similarity coefficient for image segmentation
Description
computes the intersection of binary images similarity
= jaccard(BW1
,BW2
)BW1
and
BW2
divided by the union of BW1
and
BW2
, also known as the Jaccard index. The images can be
binary images, label images, or categorical images.
computes the Jaccard index for each label in label images similarity
= jaccard(L1
,L2
)L1
and L2
.
computes the Jaccard index for each category in categorical images
similarity
= jaccard(C1
,C2
)C1
and C2
.
Examples
Compute Jaccard Similarity Coefficient for Binary Segmentation
Read an image containing an object to segment. Convert the image to grayscale, and display the result.
A = imread('hands1.jpg'); I = im2gray(A); figure imshow(I) title('Original Image')
Use the active contours (snakes) method to segment the hand.
mask = false(size(I)); mask(25:end-25,25:end-25) = true; BW = activecontour(I, mask, 300);
Read in the ground truth against which to compare the segmentation.
BW_groundTruth = imread('hands1-mask.png');
Compute the Jaccard index of this segmentation.
similarity = jaccard(BW, BW_groundTruth);
Display the masks on top of each other. Colors indicate differences in the masks.
figure
imshowpair(BW, BW_groundTruth)
title(['Jaccard Index = ' num2str(similarity)])
Compute Jaccard Similarity Coefficient for Multi-Region Segmentation
This example shows how to segment an image into multiple regions. The example then computes the Jaccard similarity coefficient for each region.
Read in an image with several regions to segment.
RGB = imread('yellowlily.jpg');
Create scribbles for three regions that distinguish their typical color characteristics. The first region classifies the yellow flower. The second region classifies the green stem and leaves. The last region classifies the brown dirt in two separate patches of the image. Regions are specified by a 4-element vector, whose elements indicate the x- and y-coordinate of the upper left corner of the ROI, the width of the ROI, and the height of the ROI.
region1 = [350 700 425 120]; % [x y w h] format
BW1 = false(size(RGB,1),size(RGB,2));
BW1(region1(2):region1(2)+region1(4),region1(1):region1(1)+region1(3)) = true;
region2 = [800 1124 120 230];
BW2 = false(size(RGB,1),size(RGB,2));
BW2(region2(2):region2(2)+region2(4),region2(1):region2(1)+region2(3)) = true;
region3 = [20 1320 480 200; 1010 290 180 240];
BW3 = false(size(RGB,1),size(RGB,2));
BW3(region3(1,2):region3(1,2)+region3(1,4),region3(1,1):region3(1,1)+region3(1,3)) = true;
BW3(region3(2,2):region3(2,2)+region3(2,4),region3(2,1):region3(2,1)+region3(2,3)) = true;
Display the seed regions on top of the image.
figure imshow(RGB) hold on visboundaries(BW1,'Color','r'); visboundaries(BW2,'Color','g'); visboundaries(BW3,'Color','b'); title('Seed Regions')
Segment the image into three regions using geodesic distance-based color segmentation.
L = imseggeodesic(RGB,BW1,BW2,BW3,'AdaptiveChannelWeighting',true);
Load a ground truth segmentation of the image.
L_groundTruth = double(imread('yellowlily-segmented.png'));
Visually compare the segmentation results with the ground truth.
figure imshowpair(label2rgb(L),label2rgb(L_groundTruth),'montage') title('Comparison of Segmentation Results (Left) and Ground Truth (Right)')
Compute the Jaccard similarity index (IoU) for each segmented region.
similarity = jaccard(L, L_groundTruth)
similarity = 3×1
0.8861
0.5683
0.8414
The Jaccard similarity index is noticeably smaller for the second region. This result is consistent with the visual comparison of the segmentation results, which erroneously classifies the dirt in the lower right corner of the image as leaves.
Input Arguments
BW1
— First binary image
logical array
First binary image, specified as a logical array of any dimension.
Data Types: logical
BW2
— Second binary image
logical array
Second binary image, specified as a logical array of the same size as
BW1
.
Data Types: logical
L1
— First label image
array of nonnegative integers
First label image, specified as an array of nonnegative integers, of any dimension.
Data Types: double
L2
— Second label image
array of nonnegative integers
Second label image, specified as an array of nonnegative integers, of the
same size as L1
.
Data Types: double
C1
— First categorical image
categorical
array
First categorical image, specified as a categorical
array of any
dimension.
Data Types: category
C2
— Second categorical image
categorical
array
Second categorical image, specified as a categorical
array of the
same size as C1
.
Data Types: category
Output Arguments
similarity
— Jaccard similarity coefficient
numeric scalar | numeric vector
Jaccard similarity coefficient, returned as a numeric scalar or numeric
vector with values in the range [0, 1]. A similarity
of
1 means that the segmentations in the two images are a perfect match. If the
input arrays are:
binary images,
similarity
is a scalar.label images,
similarity
is a vector, where the first coefficient is the Jaccard index for label 1, the second coefficient is the Jaccard index for label 2, and so on.categorical images,
similarity
is a vector, where the first coefficient is the Jaccard index for the first category, the second coefficient is the Jaccard index for the second category, and so on.
Data Types: double
More About
Jaccard Similarity Coefficient
The Jaccard similarity coefficient of two sets A and B (also known as intersection over union or IoU) is expressed as:
jaccard
(A,B)
= |
intersection
(A,B)
| / | union
(A,B)
|
where |A| represents the cardinal of set A. The Jaccard index can also be expressed in terms of true positives (TP), false positives (FP) and false negatives (FN) as:
jaccard
(A,B)
= TP / (TP + FP +
FN)
The Jaccard index is related to the Dice index according to:
jaccard
(A,B)
= dice
(A,B) / (2 -
dice
(A,B)
)
Version History
Introduced in R2017b
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)