Intersection over Union (IoU) for object detection

Figure 1: An example of detecting a stop sign in an image. The predicted bounding box is drawn in red while the ground-truth bounding box is drawn in green. Our goal is to compute the Intersection of Union between these bounding box.

Today’s blog post is inspired from an email I received from Jason, a student at the University of Rochester.

Jason is interested in building a custom object detector using the HOG + Linear SVM framework for his final year project. He understands the steps required to build the object detector well enough — but he isn’t sure how to evaluate the accuracy of his detector once it’s trained.

His professor mentioned that he should use the Intersection over Union (IoU) method for evaluation, but Jason’s not sure how to implement it.

I helped Jason out over email by:

  1. Describing what Intersection over Union is.
  2. Explaining why we use Intersection over Union to evaluate object detectors.
  3. Providing him with some example Python code from my own personal library to perform Intersection over Union on bounding boxes.

My email really helped Jason finish getting his final year project together and I’m sure he’s going to pass with flying colors.

With that in mind, I’ve decided to turn my response to Jason into an actual blog post in hopes that it will help you as well.

To learn how to evaluate your own custom object detectors using the Intersection over Union evaluation metric, just keep reading.

Looking for the source code to this post?
Jump right to the downloads section.

Intersection over Union for object detection

In the remainder of this blog post I’ll explain what the Intersection over Union evaluation metric is and why we use it.

I’ll also provide a Python implementation of Intersection over Union that you can use when evaluating your own custom object detectors.

Finally, we’ll look at some actual results of applying the Intersection over Union evaluation metric to a set of ground-truth and predicted bounding boxes.

What is Intersection over Union?

Intersection over Union is an evaluation metric used to measure the accuracy of an object detector on a particular dataset. We often see this evaluation metric used in object detection challenges such as the popular PASCAL VOC challenge.

You’ll typically find Intersection over Union used to evaluate the performance of HOG + Linear SVM object detectors and Convolutional Neural Network detectors (R-CNN, Faster R-CNN, YOLO, etc.); however, keep in mind that the actual algorithm used to generate the predictions doesn’t matter.

Intersection over Union is simply an evaluation metric. Any algorithm that provides predicted bounding boxes as output can be evaluated using IoU.

More formally, in order to apply Intersection over Union to evaluate an (arbitrary) object detector we need:

  1. The ground-truth bounding boxes (i.e., the hand labeled bounding boxes from the testing set that specify where in the image our object is).
  2. The predicted bounding boxes from our model.

As long as we have these two sets of bounding boxes we can apply Intersection over Union.

Below I have included a visual example of a ground-truth bounding box versus a predicted bounding box:

Figure 1: An example of detecting a stop sign in an image. The predicted bounding box is drawn in red while the ground-truth bounding box is drawn in green. Our goal is to compute the Intersection of Union between these bounding box.

Figure 1: An example of detecting a stop sign in an image. The predicted bounding box is drawn in red while the ground-truth bounding box is drawn in green. Our goal is to compute the Intersection over Union between these bounding box.

In the figure above we can see that our object detector has detected the presence of a stop sign in an image.

The predicted bounding box is drawn in red while the ground-truth (i.e., hand labeled) bounding box is drawn in green.

Computing Intersection over Union can therefore be determined via:

Figure 2: Computing the Intersection of Union is as simple as dividing the area of overlap between the bounding boxes by the area of union (thank you to the excellent Pittsburg HW4 assignment for the inspiration for this figure).

Figure 2: Computing the Intersection over Union is as simple as dividing the area of overlap between the bounding boxes by the area of union (thank you to the excellent Pittsburg HW4 assignment for the inspiration for this figure).

Examining this equation you can see that Intersection over Union is simply a ratio.

In the numerator we compute the area of overlap between the predicted bounding box and the ground-truth bounding box.

The denominator is the area of union, or more simply, the area encompassed by both the predicted bounding box and the ground-truth bounding box.

Dividing the area of overlap by the area of union yields our final score — the Intersection over Union.

Where are you getting the ground-truth examples from?

Before we get too far, you might be wondering where the ground-truth examples come from. I’ve mentioned before that these images are “hand labeled”, but what exactly does that mean?

You see, when training your own object detector (such as the HOG + Linear SVM method), you need a dataset. This dataset should be broken into (at least) two groups:

  1. training set used for training your object detector.
  2. testing set for evaluating your object detector.

You may also have a validation set used to tune the hyperparameters of your model.

Both the training and testing set will consist of:

  1. The actual images themselves.
  2. The bounding boxes associated with the object(s) in the image. The bounding boxes are simply the (x, y)-coordinates of the object in the image.

The bounding boxes for the training and testing sets are hand labeled and hence why we call them the “ground-truth”.

Your goal is to take the training images + bounding boxes, construct an object detector, and then evaluate its performance on the testing set.

An Intersection over Union score > 0.5 is normally considered a “good” prediction. 

Why do we use Intersection over Union?

If you have performed any previous machine learning in your career, specifically classification, you’ll likely be used to predicting class labels where your model outputs a single label that is either correct or incorrect.

This type of binary classification makes computing accuracy straightforward; however, for object detection it’s not so simple.

In all reality, it’s extremely unlikely that the (x, y)-coordinates of our predicted bounding box are going to exactly match the (x, y)-coordinates of the ground-truth bounding box.

Due to varying parameters of our model (image pyramid scale, sliding window size, feature extraction method, etc.), a complete and total match between predicted and ground-truth bounding boxes is simply unrealistic.

Because of this, we need to define an evaluation metric that rewards predicted bounding boxes for heavily overlapping with the ground-truth:

Figure 3: An example of computing Intersection over Unions for various bounding boxes.

Figure 3: An example of computing Intersection over Unions for various bounding boxes.

In the above figure I have included examples of good and bad Intersection over Union scores.

As you can see, predicted bounding boxes that heavily overlap with the ground-truth bounding boxes have higher scores than those with less overlap. This makes Intersection over Union an excellent metric for evaluating custom object detectors.

We aren’t concerned with an exact match of (x, y)-coordinates, but we do want to ensure that our predicted bounding boxes match as closely as possible — Intersection over Union is able to take this into account.

Implementing Intersection over Union in Python

Now that we understand what Intersection over Union is and why we use it to evaluate object detection models, let’s go ahead and implement it in Python.

Before we get started writing any code though, I want to provide the five example images we will be working with:

Figure 4: In this example, we'll be detecting the presence of cars in images.

Figure 4: In this example, we’ll be detecting the presence of cars in images.

These images are part of the CALTECH-101 dataset used for both image classification and object detection.

Inside the PyImageSearch Gurus course I demonstrate how to train a custom object detector to detect the presence of cars in images like the ones above using the HOG + Linear SVM framework.

I have provided a visualization of the ground-truth bounding boxes (green) along with the predicted bounding boxes (red) from the custom object detector below:

Figure 5: Our goal is to evaluate the performs of our object detector by using Intersection of Union. Specifically, we want to measure the accuracy of the predicted bounding box (red) against the ground-truth (green).

Figure 5: Our goal is to evaluate the performance of our object detector by using Intersection over Union. Specifically, we want to measure the accuracy of the predicted bounding box (red) against the ground-truth (green).

Given these bounding boxes, our task is to define the Intersection over Union metric that can be used to evaluate how “good (or bad) our predictions are.

With that said, open up a new file, name it , and let’s get coding:

We start off by importing our required Python packages. We then define a Detection  object that will store three attributes:

  • image_path : The path to our input image that resides on disk.
  • gt : The ground-truth bounding box.
  • pred : The predicted bounding box from our model.

As we’ll see later in this example, I’ve already obtained the predicted bounding boxes from our five respective images and hardcoded them into this script to keep the example short and concise.

For a complete review of the HOG + Linear SVM object detection framework, please refer to this blog post. And if you’re interested in learning more about training your own custom object detectors from scratch, be sure to check out the PyImageSearch Gurus course.

Let’s go ahead and define the bb_intersection_over_union  function, which as the name suggests, is responsible for computing the Intersection over Union between two bounding boxes:

This method requires two parameters: boxA  and boxB , which are presumed to be our ground-truth and predicted bounding boxes (the actual order in which these parameters are supplied to bb_intersection_over_union  doesn’t matter).

Lines 11-14 determine the (x, y)-coordinates of the intersection rectangle which we then use to compute the area of the intersection (Line 17).

The interArea  variable now represents the numerator in the Intersection over Union calculation.

To compute the denominator we first need to derive the area of both the predicted bounding box and the ground-truth bounding box (Lines 21 and 22).

The Intersection over Union can then be computed on Line 27 by dividing the intersection area by the union area of the two bounding boxes, taking care to subtract out the intersection area from the denominator (otherwise the intersection area would be doubly counted).

Finally, the Intersection over Union score is returned to the calling function on Line 30.

Now that our Intersection over Union method is finished, we need to define the ground-truth and predicted bounding box coordinates for our five example images:

As I mentioned above, in order to keep this example short(er) and concise, I have manually obtained the predicted bounding box coordinates from my HOG + Linear SVM detector. These predicted bounding boxes (And corresponding ground-truth bounding boxes) are then hardcoded into this script.

For more information on how I trained this exact object detector, please refer to the PyImageSearch Gurus course.

We are now ready to evaluate our predictions:

On Line 41 we start looping over each of our examples  (which are Detection  objects).

For each of them, we load the respective image  from disk on Line 43 and then draw the ground-truth bounding box in green (Lines 47 and 48) followed by the predicted bounding box in red (Lines 49 and 50).

The actual Intersection over Union metric is computed on Line 53 by passing in the ground-truth and predicted bounding box.

We then write the Intersection over Union value on the image  itself followed by our console as well.

Finally, the output image is displayed to our screen on Lines 59 and 60.

Comparing predicted detections to the ground-truth with Intersection over Union

To see the Intersection over Union metric in action, make sure you have downloaded the source code + example images to this blog post by using the “Downloads” section found at the bottom of this tutorial.

After unzipping the archive, execute the following command:

Our first example image has an Intersection over Union score of 0.7980, indicating that there is significant overlap between the two bounding boxes:

Figure 6: Computing the Intersection of Union using Python.

Figure 6: Computing the Intersection over Union using Python.

The same is true for the following image which has an Intersection over Union score of 0.7899:

Figure 7: A slightly better Intersection over Union score.

Figure 7: A slightly better Intersection over Union score.

Notice how the ground-truth bounding box (green) is wider than the predicted bounding box (red). This is because our object detector is defined using the HOG + Linear SVM framework which requires us to specify a fixed size sliding window (not to mention, an image pyramid scale and the HOG parameters themselves).

Ground-truth bounding boxes will naturally have a slightly different aspect ratio than the predicted bounding boxes, but that’s okay provided that the Intersection over Union score is > 0.5 — as we can see, this still a great prediction.

The next example demonstrates a slightly “less good” prediction where our predicted bounding box is much less “tight” than the ground-truth bounding box:

Figure 8: Deriving the Intersection of Union evaluation metric for object detection.

Figure 8: Deriving the Intersection over Union evaluation metric for object detection.

The reason for this is because our HOG + Linear SVM detector likely couldn’t “find” the car in the lower layers of the image pyramid and instead fired near the top of the pyramid where the image is much smaller.

The following example is an extremely good detection with an Intersection over Union score of 0.9472:

Figure 9: Measuring object detection performance using Intersection over Union.

Figure 9: Measuring object detection performance using Intersection over Union.

Notice how the predicted bounding box nearly perfectly overlaps with the ground-truth bounding box.

Here is one final example of computing Intersection over Union:

Figure 10: Intersection over Union for evaluating object detection algorithms.

Figure 10: Intersection over Union for evaluating object detection algorithms.

Want to train your own custom object detectors?

If you enjoyed this tutorial and want to learn more about training your own custom object detectors, you’ll definitely want to take a look at the PyImageSearch Gurus course — the most complete, comprehensive computer vision course online today.

Inside the course, you’ll find over 168 lessons covering 2,161+ pages of content on Object Detection, Image Classification, Convolutional Neural Networks, and much more.

To learn more about the PyImageSearch Gurus course (and grab your FREE sample lessons + course syllabus), just click the button below:

Click here to learn more about PyImageSearch Gurus!


In this blog post I discussed the Intersection over Union metric used to evaluate object detectors. This metric can be used to assess any object detector provided that (1) the model produces predicted (x, y)-coordinates [i.e., the bounding boxes] for the object(s) in the image and (2) you have the ground-truth bounding boxes for your dataset.

Typically, you’ll see this metric used for evaluating HOG + Linear SVM and CNN-based object detectors.

To learn more about training your own custom object detectors, please refer to this blog post on the HOG + Linear SVM framework along with the PyImageSearch Gurus course where I demonstrate how to implement custom object detectors from scratch.

Finally, before you go, be sure to enter your email address in the form below to be notified when future PyImageSearch blog posts are published — you won’t want to miss them!


If you would like to download the code and images used in this post, please enter your email address in the form below. Not only will you get a .zip of the code, I’ll also send you a FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL! Sound good? If so, enter your email address and I’ll send you the code immediately!

, , , ,

86 Responses to Intersection over Union (IoU) for object detection

  1. wajih November 7, 2016 at 10:33 am #

    I was translating a code, was wondering of IoU, and now really I OWE YOU ONE 🙂 Thanks for the explanation. Helped me finish the translation in a breeze 🙂

    • Adrian Rosebrock November 7, 2016 at 2:42 pm #

      Awesome, I’m happy I could help Wajih! 🙂

    • Anne March 28, 2018 at 3:27 am #

      Ha, I love the play of words… I o U 1 XD

  2. Walid Ahmed November 7, 2016 at 11:23 am #

    Thanks a lot.

    This was really in time for me
    however, I am still clueless how to build a classifier for object detection.
    I already built my classifier for object classification using CNN.
    Any advice?

    • Adrian Rosebrock November 7, 2016 at 2:42 pm #

      Hey Walid — I would suggest starting by reading about the HOG + Linear SVM detector, a classic method used for object detection. I also demonstrate how to implement this system inside the PyImageSearch Gurus course. As for using a CNN for object detection, I will be covering that in my next book.

  3. kiran November 7, 2016 at 9:04 pm #

    Awesome. You made it pretty simple to understand. I request you to post a similar blog on evaluation metrics for object segmentation tasks as well. Thank you.

    • Adrian Rosebrock November 10, 2016 at 8:50 am #

      Most of my work focuses on object detection rather than pixel-wise segmentations of an image but I’ll certainly consider it for the future.

  4. jsky November 7, 2016 at 10:43 pm #

    I implemented this in my car detection framework for my FYP too.
    Its called the Jaccard Index, and is a standard measure for evaluating such record retrieval.

  5. abby November 10, 2016 at 8:54 am #

    thank you for the tutorial

    • Adrian Rosebrock November 10, 2016 at 9:40 am #

      No problem, happy I can help!

  6. Aamer November 12, 2016 at 2:33 am #

    what if our hog+svm model predicts multiple bounding boxes.

    In that case, will we iterate over all such predicted bounding boxes and see for the one which gets the max value for the Intersection/Union ratio ?

    • Adrian Rosebrock November 14, 2016 at 12:12 pm #

      If you detector predicts multiple bounding boxes for the same object then you should be applying non-maxima suppression. If you are predicting bounding boxes for multiple objects then you should be computing IoU for each of them.

      • elsa November 27, 2019 at 1:45 am #

        How do I computing IoU for each of them ? thanks.

        • Adrian Rosebrock December 5, 2019 at 10:43 am #

          You loop over the detected bounding boxes and compute IoU for each. You may decide to associate bounding boxes with ground-truth bounding boxes by computing the Euclidean distance between their respective centroids. Objects that minimize the distance should be associated together.

  7. Miej November 28, 2016 at 10:47 am #

    This code fails. Specifically, it gets broken when comparing two non-overlapping bounding boxes by providing a non-negative value for interArea when the boxes can be separated into diagonally opposing quadrants by a vertical and a horizontal line. This could be easily remedied with a simple catch for such cases.

  8. Miej November 28, 2016 at 11:14 am #

    that said, it’s still quite handy. thanks!

  9. auro November 28, 2016 at 2:34 pm #

    Do we need to consider the case where the two boxes to not intersect at all?
    Look up the MATLAB code at

    • Adrian Rosebrock November 28, 2016 at 2:40 pm #

      Good point, thank you for pointing this out Auro.

  10. Rimphy Darmanegara December 8, 2016 at 11:55 pm #

    So, in case of negative result just return zero?

    Thank you for the code.

    • Adrian Rosebrock December 10, 2016 at 7:14 am #

      Correct. In case of negative (non-overlapping) objects the return value would be zero.

  11. Anivini December 21, 2016 at 4:34 am #

    How to give bounding box parameters from line 33 to 38?

  12. Jere December 22, 2016 at 1:34 pm #

    This is helpful knowledge. You have made me understand about the method IoU used in fast rcnn. Thanks you are one of the best people in explaining concepts easily

    • Adrian Rosebrock December 23, 2016 at 10:52 am #

      Thank you for the kind words Jere 🙂

      • sabrine December 29, 2016 at 8:54 am #

        Good evening, I worked with HOG detector and I used Matlab software, I want to calculate the rate of overlap between the detection of the ground truth and my detection file, please help me.

        • Adrian Rosebrock December 31, 2016 at 1:26 pm #

          If you have the predictions from your MATLAB detector you can either (1) write them to file and use my Python code to read them and compare them to the ground-truth or (2) implement this function in MATLAB.

          • sabrine January 1, 2017 at 6:04 am #

            OK thanks
            I will try to implement on matlab
            Because I do not know how to use python

  13. Anivini January 2, 2017 at 4:14 am #

    I am confused about[:2] and[2:] in lines 47 to 50. What is actually specifed by :2 and 2: . I surfed but couldn’t get an answer.

  14. goingmyway January 15, 2017 at 4:56 am #

    Awesome post. A clear explanation of IoU.

    • Adrian Rosebrock January 15, 2017 at 12:03 pm #

      Thank you, I’m happy to hear you enjoyed it! 🙂

  15. secret February 22, 2017 at 12:00 am #

    Hey I would like to know how to compute the repeatability factor ,corresponding count and recall and precision to evaluate feature detector in python

    I would like to know is there a function in python similar to the one in C++ if not then how do I proceed

  16. Paarijaat Aditya March 7, 2017 at 10:06 am #

    Very helpful post! Thanks! A small correction, line 17 should be:

    interArea = max(0, xB – xA + 1) * max(0, yB – yA + 1)

    This is to avoid those corner cases where the rectangles are not overlapping but the intersection area still computes to be greater than 0. This happens when both the brackets in the original line 17 are negative. For e.g. boxA is on the upper right of the picture and boxB is somewhere on the lower left of the picture, without any overlap and with significant separation between them.

    • Adrian Rosebrock March 8, 2017 at 1:07 pm #

      Thanks for sharing Paarijaat!

  17. Evan April 5, 2017 at 3:41 pm #

    I am still confused as to why you add 1 in line 17: (xB – xA + 1) * (yB – yA + 1).

    Doesn’t xB-xA give the width of the intersecting area and same with yB-yA? What does adding one do?


    • Adrian Rosebrock April 8, 2017 at 1:02 pm #

      Adding one simply prevents the numerator from being zero.

    • Danny January 17, 2019 at 8:35 am #

      The actual reason of adding 1 is because xB, xA both represent pixel coordinates. Suppose you have 6 pixels, the coordinates are from 0 to 5. When you try to calculate the span of these 6 pixels, it should be (5-0+1) = 6, there you have the extra 1.

  18. Johannes April 7, 2017 at 9:52 am #

    Thanks, helped me out understanding the YOLO9000 paper.

    • Adrian Rosebrock April 8, 2017 at 12:47 pm #

      Fantastic, I’m glad to hear it Johannes!

  19. Mohammad April 9, 2017 at 2:35 pm #

    Hey Adrian,
    I tested your code but I think it is a little buggy:
    Assume that we have two below bounding boxes: (the structure of bounding boxes are (x1,y1,x2,y2), it is just a tuple in python. (x1,y1) denotes to the top-left coordinate and (x2,y2) denotes to the bottom-right coordinate.)

    boxA = (142,208,158,346)
    boxB = (243,203,348,279)

    Based on these BBoxes, the IoU should be zero. Because there is no intersection between them. But your code give a negative value! By the way, I should say that my origin is up-left corner of the image.
    So how can I solve this problem?

    • Adrian Rosebrock April 12, 2017 at 1:22 pm #

      This issue is simply to resolve:

      I will update the blog post in the future to reflect this change.

      • Byungsoo September 7, 2017 at 4:52 pm #

        @adrian That doesn’t look enough to resolve the issue. interArea = (xB - xA + 1) * (yB - yA + 1) could be positive when two terms are both negative. It should return 0 if either (xB - xA + 1) or (yB - yA + 1) is equal or less than 0.

      • Filippo November 20, 2017 at 6:14 am #

        Please, fix this issue, IOU should be 0 when interArea is the product of two negative terms. Not that you owe anyone anything, but this is the first result on google when searching intersection over union, it would be great not to have to scroll down to the comments to find out the code is buggy. Paarijaat Aditya solution works pretty fine and handles both corner cases of non overlapping boxes.

        Thanks for the nice tutorial!

  20. Islam Saad April 27, 2017 at 4:53 am #

    Many thanks Adrian for the great topic You.
    I wanna ask if I have dataset groundtruth as contour shows all object outline ( not rectangle box shape). Then, is there another method dealing with free shape bounding contours? In order to compute Lou.

    • Adrian Rosebrock April 28, 2017 at 9:32 am #

      Intersection over Union assumes bounding boxes. You can either convert your contour points to a bounding box and compute IoU or you can simply count the number of pixels in your mask that overlap with the detection.

  21. Javier June 20, 2017 at 9:12 pm #

    I based on your simple implementation to port it to Tensorflow to create the IoU matrix of two sets of bounding boxes:


    • Adrian Rosebrock June 22, 2017 at 9:35 am #

      Great job Javier!

      • MMD October 21, 2017 at 5:23 am #

        Hi Adrian Please am interested in your project but am not a developer.
        I will like you to develop a project for me the intended purpose is mainly to Classify Vehicle base on Length, Height and Axle count.
        Please any one interested in this commercial project is welcome.

        • Adrian Rosebrock October 22, 2017 at 8:34 am #

          I personally have too much on my plate to take on any new projects right now but I would suggest you post on PyImageJobs where there are thousands of OpenCV developers who can help you build your project.

  22. K M Ibrahim Khalilullah June 29, 2017 at 2:04 am #

    Thanks for this tutorial

  23. K M Ibrahim Khalilullah July 23, 2017 at 12:06 am #

    How can I calculate precision-recall from IoU?

  24. Walid Ahmed August 16, 2017 at 2:37 pm #

    But I found something strange
    box1=[265.0, 103.0, 372.0, 268.0]
    box2=[12, 34, 32, 61]
    has a high value of IOU based on the code although they are exclusive
    how Can this issue be resolved?


  25. Oyesh September 2, 2017 at 5:11 pm #

    Very insightful. Loved your explanation.

    • Adrian Rosebrock September 5, 2017 at 9:31 am #

      Thanks Oyesh! 🙂

  26. hema January 13, 2018 at 6:53 am #

    Hi Adrian Rosebrock,
    I am trying a HOG descriptor with SVM classifier to detect objects.And after creating annotation xml file if i use it for training it gives me the following error: “An impossible set of object boxes was given for training.All the boxes need to have a similar aspect ratio and also not be smaller than 400 pixels in area…” How do i fix it??
    Thanks in advance

    • Adrian Rosebrock January 15, 2018 at 9:24 am #

      I assume you are using dlib to train your own custom object detector? The HOG + Linear SVM implementation in dlib requires your bounding boxes to have a similar aspect ratio (i.e., similar width and height of the ROI). It sounds like your objects do not have this or you have an invalid bounding box in your annotations file.

  27. Mohamed Judi January 20, 2018 at 9:14 am #

    Hi Adrian, thank you so much for this excellent blog. It explains IoU very well. It is a gift to find someone, not only knows his stuff but also knows how to explain it to people in simple terms.

    I’m working on the Kaggle’s 2018 Data Science Bowl competition with very little knowledge of deep learning, let alone Biomedical Image Segmentation and U-Nets 🙂 However, I’m taking this challenge to learn. Winning is secondary at this stage of my career.

    In your blog, you are explaining how to calculate IoU for rectangular shapes. What about irregular shapes like the masks of cells or nucleus? They are not perfectly rectangular and therefore the formula to calculate the area is not quite useful.

    I would be very grateful if you could help me calculate the IoU for each threshold, as well as the IoU mean over multiple thresholds.

    I know this might be too much to ask but I’m willing to discuss options offline if your time permits.

    Thank you!

    • Adrian Rosebrock January 22, 2018 at 6:34 pm #

      There are two components to need to consider here (as is true with object detection): precision and recall. You first need to detect the correct object. In the case of object detection and semantic segmentation, this is your recall. For example, if you detect a “cat” but the actual label is a dog, then your recall score goes down.

      To measure the precision (accuracy) of the detection/segmentation we can use IoU. Exactly how IoU is used for segmentation depends on the challenge so you should consult the Kaggle documentation and/or evaluation scripts but typically it’s the ratio of ground-truth mask and the predicted mask.

      • Mohamed Judi February 6, 2018 at 8:17 pm #

        Thank you, Adrian!

  28. Qaisar Tanvir January 23, 2018 at 3:21 am #

    How can we do this with polygons? where bounding box may be a little rotated (RBOX). i dont have the angle of rotation. So my output bounding box cannot be drawn with top-left and bottom-right. it has four points. Any help or suggestion will be highly appreciated guys

    • Adrian Rosebrock January 23, 2018 at 2:00 pm #

      Why not convert your rotated bounding box to a “normal” bounding box and then compute IoU on the non-rotated version?

  29. Glen January 28, 2018 at 10:22 am #


    I think there will be a problem when two boxes have no intersection area at all. The computation for interArea could be potentially problematic.

    I suggest change from ‘interArea = (xB – xA + 1) * (yB – yA + 1)’
    interArea = max(0,(xB – xA + 1)) * max(0,(yB – yA + 1))

    • Adrian Rosebrock January 30, 2018 at 10:27 am #

      Indeed, you are right Glen. I will get the code updated.

  30. Johannes May 18, 2018 at 9:56 am #

    Hi, I am not 100% sure, but I think that your code tends to overestimate the IoU.

    Given two boxes [[0,0], [10, 10]] and [[1,1], [11, 11]]. The area of intersection should be 81. However, with your function it computes 100. The same problem occurs with the box size which are obviously 100 but are computed as 121. Your code gives an IoU of 0.7, while the analytical solution should be 0.68…

    Maybe you could clarify, why you are always adding +1 to the coordinates. This somehow seems to deviate from the standard. Used in many other implementations.

    • Adrian Rosebrock May 22, 2018 at 6:43 am #

      The “+1” here is used to prevent any division by zero errors. That said, I will reinvestigate this implementation in the near future.

  31. ptyshevs July 10, 2018 at 1:38 am #

    Hi, I’ve decided to check computations of IoU by hand and it seems that “+1” in your code is responsible for the incorrect result.

    Let bboxA = [0, 0, 2, 2], bboxB = [1, 1, 3, 3], then Union(bboxA, bboxB) = 7, Intersection(bboxA, bboxB) = 1, yielding IoU = 1/7 = 0.1428…
    Your version will give 0.2857…

    I suggest the following snippet:

    interArea = abs((xB – xA) * (yB – yA))
    if interArea == 0:
    return 0
    # compute the area of both the prediction and ground-truth
    # rectangles
    boxAArea = abs((boxA[2] – boxA[0]) * (boxA[3] – boxA[1]))
    boxBArea = abs((boxB[2] – boxB[0]) * (boxB[3] – boxB[1]))

  32. Johannes August 1, 2018 at 1:53 am #


    I made a github gist with the correct implementation of the bb_intersection_over_union feel free to check it out:

    The correction of ptyshevs is almost correct. However, it does not handle the cases in which boxes have no overlap.


    • prb November 27, 2018 at 3:33 am #

      @Johannes will this code work for one rectangle inside other?

  33. JP November 9, 2018 at 7:04 pm #

    What happens if the ground truth bounding box is much larger than the actual object? Sometimes when using the automation option in the ground truth labeler app in Matlab the bounding box will grow and shrink depending on what the object is doing.

    If you have a low-level object detector then you should have a predicted bounding box that tightly encloses the object’s contour but because the ground truth box is much bigger, your IoU score will be very low. Is there any way to address this?

    • Adrian Rosebrock November 10, 2018 at 9:53 am #

      The ground-truth bounding box is just a set of coordinates, it has absolutely no knowledge regarding the size of the actual object itself. IoU would have to operate on the ground-truth bounding boxes and assume they are correct. If they are not then you should re-label/adjust your data.

  34. prb November 27, 2018 at 3:50 am #

    @ Adrian Thanks for the great tutorial. What if there are multiple bounding boxes of different objects. I wanted to find the overlap between different bbs for other detected objects.

    • Adrian Rosebrock November 30, 2018 at 9:33 am #

      Loop over your combinations of the bounding boxes and apply IoU to each of them.

  35. Kartik Podugu November 29, 2018 at 8:26 am #

    Nice elaborate explanation.

    • Adrian Rosebrock November 30, 2018 at 8:55 am #

      Thanks Kartik!

  36. jeffinaustin May 11, 2019 at 7:18 pm #

    It’s a great explanation, but other than reviving some old math (Jaccard) why would IoU be better in any way than just the ratio of the intersection over the correct ground truth area? Has anyone showed there IS a reason to reach back for some old math, or is a method that would more tightly award correctness (with simpler code) be just as good?

    • Adrian Rosebrock May 15, 2019 at 3:04 pm #

      Jaccard and the Dice coefficient are sometimes used for measuring the quality of bounding boxes, but more typically they are used for measuring the accuracy of instance segmentation and semantic segmentation.

  37. Aditya Singh June 9, 2019 at 1:52 am #

    Hi Adrian,
    What should I do, if on my test data, in some frames , for some objects the bounding boxes aren’t predicted, but they are present in the ground truth labels. The interArea would be zero, but the loss should be high in this case.

  38. Mina July 10, 2019 at 12:13 pm #

    Hi Adrian,

    Thank you for the great post!
    I have a question. My main problem is segmentation, but I’d like to detect the object first, and then segment it. I have better results this way rather than end- to- end segmentation.

    I have developed the object detection algorithm, and now I’d like to segment the detected objects. But in some cases the detected bounding box is smaller than the true (ground-truth) box. This will effect the segmentation accuracy. What do you suggest to solve this problem? shall I consider a larger box when I want to do the segmentation? Is it a good idea to for example double the size of the detected box before feeding the segmentation network?

    Thank you

  39. Han-Cheol Cho July 28, 2019 at 11:47 pm #

    Thank you for a great article on every aspect of IoU 🙂

    • Adrian Rosebrock August 7, 2019 at 1:00 pm #

      You are welcome!

  40. Sahi Chachere September 11, 2019 at 9:42 am #

    I am training custom object detector from yolov3, calculated anchors using darknet and got avg IoU = 69.74, I have 6 classes in my dataset and total images of 10,225, how can I improve IoU for my dataset.

  41. Malhar October 16, 2019 at 12:21 pm #

    Nice post, just wanted to point out that Figure 1 and 2 are incorrectly captioned ?

    They mention “Intersection of Union” instead of “Intersection Over Union”

    • Adrian Rosebrock October 17, 2019 at 7:46 am #

      Thanks for pointing out the typo! I’ve gone ahead and fixed them.

  42. Krone December 24, 2019 at 10:05 am #

    Hello, your blog is really good. May I translate it in Chinese and put it on my blog@csdn.

Before you leave a comment...

Hey, Adrian here, author of the PyImageSearch blog. I'd love to hear from you, but before you submit a comment, please follow these guidelines:

  1. If you have a question, read the comments first. You should also search this page (i.e., ctrl + f) for keywords related to your question. It's likely that I have already addressed your question in the comments.
  2. If you are copying and pasting code/terminal output, please don't. Reviewing another programmers’ code is a very time consuming and tedious task, and due to the volume of emails and contact requests I receive, I simply cannot do it.
  3. Be respectful of the space. I put a lot of my own personal time into creating these free weekly tutorials. On average, each tutorial takes me 15-20 hours to put together. I love offering these guides to you and I take pride in the content I create. Therefore, I will not approve comments that include large code blocks/terminal output as it destroys the formatting of the page. Kindly be respectful of this space.
  4. Be patient. I receive 200+ comments and emails per day. Due to spam, and my desire to personally answer as many questions as I can, I hand moderate all new comments (typically once per week). I try to answer as many questions as I can, but I'm only one person. Please don't be offended if I cannot get to your question
  5. Do you need priority support? Consider purchasing one of my books and courses. I place customer questions and emails in a separate, special priority queue and answer them first. If you are a customer of mine you will receive a guaranteed response from me. If there's any time left over, I focus on the community at large and attempt to answer as many of those questions as I possibly can.

Thank you for keeping these guidelines in mind before submitting your comment.

Leave a Reply