Calculation Intersection Over Union (IoU) For Evaluating an Image Segmentation Model

Last Updated : 16 Jun, 2026

Intersection over Union (IoU) is an important evaluation metric used in image segmentation models to measure how accurately the predicted mask overlaps with the actual ground truth mask. It is widely used in computer vision tasks because it provides a clear numerical value for evaluating segmentation quality.

  • IoU measures overlap between predicted and actual masks
  • It is also called the Jaccard Index
  • Used mainly in image segmentation and object detection

Calculating IoU

To calculate IoU, we need to first understand two key terms: True Positive (TP) and False Positive (FP). A True Positive is when the model correctly predicts a pixel as being part of an object when it is actually part of the object. A False Positive is when the model predicts a pixel as part of an object when it is part of the background.

IoU = TP / (TP + FP + FN)

  • TP (True Positive): Correctly predicted object pixels
  • FP (False Positive): Wrongly predicted object pixels
  • FN (False Negative): Missed object pixels

IoU evaluates how much the predicted region matches the actual region by comparing correct predictions against total relevant pixels.

Why IoU is Important?

  • Measures segmentation accuracy at pixel level
  • Shows how well objects are separated from background
  • Common metric in datasets like COCO, PASCAL VOC
  • Helps compare different deep learning models

Step-by-Step IoU Calculation

  • Input Masks: Take ground truth and predicted binary masks of the same size.
  • Initialize Counters: Set TP, FP, and FN to zero.
  • Compare Pixels: Traverse both masks and compare each corresponding pixel.
  • Count Values: Update TP when both are 1, FP when prediction is 1 and ground truth is 0, FN when prediction is 0 and ground truth is 1.
  • Apply Formula: IoU = TP / (TP + FP + FN)
  • Final Output: Return IoU score between 0 and 1, where higher means better segmentation.

Here is an example Java code snippet that demonstrates how to calculate IoU for evaluating an image segmentation model:

C++
#include <iostream>
#include <vector>

double calculateIoU(std::vector<std::vector<int>> gtMask, std::vector<std::vector<int>> predMask) {
    // Calculate the true positives,
    // false positives, and false negatives
    int tp = 0;
    int fp = 0;
    int fn = 0;

    for (int i = 0; i < gtMask.size(); i++) {
        for (int j = 0; j < gtMask[0].size(); j++) {
            if (gtMask[i][j] == 1 && predMask[i][j] == 1) {
                tp++;
            }
            else if (gtMask[i][j] == 0 && predMask[i][j] == 1) {
                fp++;
            }
            else if (gtMask[i][j] == 1 && predMask[i][j] == 0) {
                fn++;
            }
        }
    }

    // Calculate IoU
    double iou = (double)tp / (tp + fp + fn);

    return iou;
}

int main() {
    // Create a ground truth mask and a predicted mask
    std::vector<std::vector<int>> gtMask = {{1, 1, 0}, {0, 1, 0}, {0, 0, 0}};
    std::vector<std::vector<int>> predMask = {{1, 1, 0}, {0, 1, 1}, {0, 0, 0}};

    // Calculate IoU
    double iou = calculateIoU(gtMask, predMask);

    // Print the result
    std::cout << "IoU: " << iou << std::endl;

    return 0;
}
Java
/*package whatever //do not write package name here */

import java.util.ArrayList;

public class IoUCalculator {

    public static void main(String... aryan)
    {
        // Create a ground truth mask and a predicted mask
        int[][] gtMask
            = { { 1, 1, 0 }, { 0, 1, 0 }, { 0, 0, 0 } };
        int[][] predMask
            = { { 1, 1, 0 }, { 0, 1, 1 }, { 0, 0, 0 } };

        // Calculate IoU
        double iou = calculateIoU(gtMask, predMask);

        // Print the result
        System.out.println("IoU: " + iou);
    }

    public static double calculateIoU(int[][] gtMask,
                                      int[][] predMask)
    {
        // Calculate the true positives,
        // false positives, and false negatives
        int tp = 0;
        int fp = 0;
        int fn = 0;

        for (int i = 0; i < gtMask.length; i++) {
            for (int j = 0; j < gtMask[0].length; j++) {
                if (gtMask[i][j] == 1
                    && predMask[i][j] == 1) {
                    tp++;
                }
                else if (gtMask[i][j] == 0
                         && predMask[i][j] == 1) {
                    fp++;
                }
                else if (gtMask[i][j] == 1
                         && predMask[i][j] == 0) {
                    fn++;
                }
            }
        }

        // Calculate IoU
        double iou = (double)tp / (tp + fp + fn);

        return iou;
    }
}
Python
class IoUCalculator:

    @staticmethod
    def main():
        # Create a ground truth mask and a predicted mask
        gtMask = [[1, 1, 0], [0, 1, 0], [0, 0, 0]]
        predMask = [[1, 1, 0], [0, 1, 1], [0, 0, 0]]

        # Calculate IoU
        iou = IoUCalculator.calculateIoU(gtMask, predMask)

        # Print the result
        print("IoU: ", iou)

    @staticmethod
    def calculateIoU(gtMask, predMask):
        # Calculate the true positives,
        # false positives, and false negatives
        tp = 0
        fp = 0
        fn = 0

        for i in range(len(gtMask)):
            for j in range(len(gtMask[0])):
                if gtMask[i][j] == 1 and predMask[i][j] == 1:
                    tp += 1
                elif gtMask[i][j] == 0 and predMask[i][j] == 1:
                    fp += 1
                elif gtMask[i][j] == 1 and predMask[i][j] == 0:
                    fn += 1

        # Calculate IoU
        iou = tp / (tp + fp + fn)

        return iou


if __name__ == '__main__':
    IoUCalculator.main()
C#
using System;
using System.Collections.Generic;

class Program {
  static double CalculateIoU(List<List<int>> gtMask, List<List<int>> predMask) {
  // Calculate the true positives,
  // false positives, and false negatives
    int tp = 0;
    int fp = 0;
    int fn = 0;
  
      for (int i = 0; i < gtMask.Count; i++) {
        for (int j = 0; j < gtMask[0].Count; j++) {
            if (gtMask[i][j] == 1 && predMask[i][j] == 1) {
                tp++;
            }
            else if (gtMask[i][j] == 0 && predMask[i][j] == 1) {
                fp++;
            }
            else if (gtMask[i][j] == 1 && predMask[i][j] == 0) {
                fn++;
            }
        }
    }

    // Calculate IoU
    double iou = (double)tp / (tp + fp + fn);

    return iou;
  }

  static void Main(string[] args) {
      // Create a ground truth mask and a predicted mask
      List<List<int>> gtMask = new List<List<int>> {new List<int>{1, 1, 0}, new List<int>{0, 1, 0}, new List<int>{0, 0, 0}};
      List<List<int>> predMask = new List<List<int>> {new List<int>{1, 1, 0}, new List<int>{0, 1, 1}, new List<int>{0, 0, 0}};

      // Calculate IoU
      double iou = CalculateIoU(gtMask, predMask);

      // Print the result
      Console.WriteLine("IoU: " + iou);
  }
}
JavaScript
function calculateIoU(gtMask, predMask) {
  // Calculate the true positives, false positives, and false negatives
  let tp = 0;
  let fp = 0;
  let fn = 0;

  for (let i = 0; i < gtMask.length; i++) {
    for (let j = 0; j < gtMask[0].length; j++) {
      if (gtMask[i][j] === 1 && predMask[i][j] === 1) {
        tp++;
      } else if (gtMask[i][j] === 0 && predMask[i][j] === 1) {
        fp++;
      } else if (gtMask[i][j] === 1 && predMask[i][j] === 0) {
        fn++;
      }
    }
  }

  // Calculate IoU
  const iou = tp / (tp + fp + fn);

  return iou;
}

// Example usage
const gtMask = [[1, 1, 0], [0, 1, 0], [0, 0, 0]];
const predMask = [[1, 1, 0], [0, 1, 1], [0, 0, 0]];
const iou = calculateIoU(gtMask, predMask);
console.log(`IoU: ${iou}`);

Output
IoU: 0.75

Real-Life Examples of IoU

  • Medical Imaging: IoU is used to evaluate tumor detection in MRI or CT scans. Higher IoU means better tumor segmentation accuracy.
  • Autonomous Vehicles: Used in self-driving cars to detect pedestrians, vehicles, and road signs accurately for safe navigation.
  • Satellite Image Analysis: Helps in identifying land, water bodies, and buildings in satellite images.
  • Face Recognition Systems: Used to evaluate how well facial regions are segmented from background images.

Advantages of IoU

  • Provides a clear measure of segmentation accuracy
  • Evaluates pixel-level overlap between prediction and ground truth
  • Works well for image segmentation and object detection tasks
  • Simple and easy to compute using basic formula
  • Produces a standardized score (0 to 1) for comparison
  • Helps in comparing different models fairly
  • Widely used in real-world applications like medical imaging and autonomous driving
Comment