# bboxOverlapRatio

Compute bounding box overlap ratio

## Syntax

## Description

returns
the overlap ratio between each pair of bounding boxes `overlapRatio`

= bboxOverlapRatio(`bboxA`

,`bboxB`

)`bboxA`

and `bboxB`

.
The function returns the `overlapRatio`

value between `0`

and `1`

,
where `1`

implies a perfect overlap.

additionally lets you specify the method to use for computing the ratio. You must
set the `overlapRatio`

= bboxOverlapRatio(`bboxA`

,`bboxB`

,`ratioType`

)`ratioType`

to either `'Union'`

or
`'Min'`

.

## Examples

### Compute the Overlap Ratio Between Two Bounding Boxes

Define two bounding boxes in the format [x y width height].

bboxA = [150,80,100,100]; bboxB = bboxA + 50;

Display the bounding boxes on an image.

I = imread('peppers.png'); RGB = insertShape(I,'FilledRectangle',bboxA,'Color','green'); RGB = insertShape(RGB,'FilledRectangle',bboxB,'Color','yellow'); imshow(RGB)

Compute the overlap ratio between the two bounding boxes.

overlapRatio = bboxOverlapRatio(bboxA,bboxB)

overlapRatio = 0.0833

### Compute Overlap Ratio Between Each Pair of Bounding Boxes

Randomly generate two sets of bounding boxes.

bboxA = 10*rand(5,4); bboxB = 10*rand(10,4);

Ensure that the width and height of the boxes are positive.

bboxA(:,3:4) = bboxA(:,3:4) + 10; bboxB(:,3:4) = bboxB(:,3:4) + 10;

Compute the overlap ratio between each pair.

overlapRatio = bboxOverlapRatio(bboxA,bboxB)

`overlapRatio = `*5×10*
0.2431 0.2329 0.3418 0.5117 0.7972 0.1567 0.1789 0.4339 0.0906 0.5766
0.3420 0.1655 0.7375 0.5188 0.2786 0.3050 0.2969 0.4350 0.2477 0.2530
0.4844 0.3290 0.3448 0.1500 0.1854 0.4976 0.5629 0.4430 0.5027 0.2685
0.3681 0.0825 0.3499 0.0840 0.0658 0.5921 0.6498 0.1930 0.7433 0.0676
0.3752 0.1114 0.3114 0.0696 0.0654 0.5408 0.6234 0.2046 0.7557 0.0717

## Input Arguments

`bboxA`

— Bounding box

*M*-by-4 matrix | *M*-by-5 matrix

Bounding boxes, specified as an *M*-by-4 or
*M*-by-5 nonsparse numeric matrix. *M*
is the number of bounding boxes. Each row of the matrix defines a bounding
box as either an axis-aligned rectangle or a rotated rectangle. This table
describes the format for each bounding box.

Bounding Box | Row | Description |
---|---|---|

Axis-aligned rectangle | [x_{min},
y_{min},
width,
height] | This type of bounding box is defined in pixel
coordinates as an M-by-4 matrix
representing M bounding boxes |

Rotated rectangle | [x_{center},
y_{center},
width, height,
yaw] | This type of bounding box is defined in spatial
coordinates as an M-by-5 matrix
representing M bounding boxes. The
x_{center}
and y_{center}
coordinates represent the center of the bounding box.
The width and
height elements represent the
length of the box along the x and
y axes, respectively. The
yaw represents the rotation angle
in degrees. The amount of rotation about the center of
the bounding box is measured in the clockwise
direction. |

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `uint8`

| `uint16`

| `uint32`

`bboxB`

— Bounding box

*M*-by-4 matrix | *M*-by-5 matrix

Bounding boxes, specified as an *M*-by-4 or
*M*-by-5 nonsparse numeric matrix. *M*
is the number of bounding boxes. Each row, *M*, of the
matrix defines a bounding box as either an axis-aligned rectangle or a
rotated rectangle. This table describes the format for each bounding
box.

Bounding Box | Row | Description |
---|---|---|

Axis-aligned rectangle | [x_{min},
y_{min},
width,
height] | This type of bounding box is defined in pixel
coordinates as an M-by-4 matrix
representing M bounding boxes |

Rotated rectangle | [x_{center},
y_{center},
width, height,
yaw] | This type of bounding box is defined in spatial
coordinates as an M-by-5 matrix
representing M bounding boxes. The
x_{center}
and y_{center}
coordinates represent the center of the bounding box.
The width and
height elements represent the
length of the box along the x and
y axes, respectively. The
yaw represents the rotation angle
in degrees. The amount of rotation about the center of
the bounding box is measured in the clockwise
direction. |

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `uint8`

| `uint16`

| `uint32`

`ratioType`

— Ratio type

`'Union'`

(default) | `'Min'`

Ratio type, specified as the character vector `'Union'`

or `'Min'`

.

Set the ratio type to

`'Union'`

to compute the ratio as the area of intersection between`bboxA`

and`bboxB`

, divided by the area of the union of the two.Set the ratio type to

`'Min'`

to compute the ratio as the area of intersection between`bboxA`

and`bboxB`

, divided by the minimum area of the two bounding boxes.

**Data Types: **`char`

## Output Arguments

`overlapRatio`

— Overlap ratio between two bounding boxes

*M*-by-*N* matrix

Overlap ratio between two bounding boxes, returned as an *M*-by-*N* matrix.
Each (*I*, *J*) element in the output
matrix corresponds to the overlap ratio between row *I* in `bboxA`

and
row *J* in `bboxB`

. The function
returns `overlapRatio`

in the between 0 and 1,
where `1`

implies a perfect overlap. If either `bboxA`

or `bboxB`

is
double, then the function returns `overlapRatio`

as `double`

.
Otherwise, the function returns it as `single`

.

The function computes the overlap ratio based on the ratio type.
You can set `ratioType`

to `'Union'`

or `'Min'`

:

**Data Types: **`single`

| `double`

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Code generation is not supported for rotated rectangle inputs.

**Introduced in R2014b**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

# 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)