Skip to content
DeepForgeby svx
All problems
MediumComputer Visioncv-144

Dice Coefficient for Binary Masks

Problem

Compute the Dice coefficient between two binary masks.

Return 2 * intersection / (sum_a + sum_b) rounded to 4 decimal places, where the intersection counts pixels that are 1 in both masks. Return 0.0 when both masks are all zeros.

Test cases

case 1
in: [[[1,1],[0,0]],[[1,0],[1,0]]]
expected: 0.5
case 2
in: [[[1,0],[0,1]],[[1,0],[0,1]]]
expected: 1
case 3
in: [[[1,1],[1,1]],[[0,0],[0,0]]]
expected: 0
case 4
in: [[[0,0],[0,0]],[[0,0],[0,0]]]
expected: 0
Starter codePython
def dice_coefficient(mask_a, mask_b):
    # Your code here
    pass

4 test cases · run your code against them in the editor