Function Repository Resource:

# NonMaximumSuppression

Filter regions based on their overlap

Contributed by: Giulio Alessandrini
 ResourceFunction["NonMaximumSuppression"][{reg1,reg2,…}] keeps the rectangular regions regi with low overlap between each other. ResourceFunction["NonMaximumSuppression"][{reg1,…}→{w1,…}] uses the weights wi to score the importance of each regi. ResourceFunction["NonMaximumSuppression"][regions,property] gives the specified property for the filtered regions.

## Details and Options

ResourceFunction["NonMaximumSuppression"] is used to filter regions based on their mutual overlap and their weights.
Regions must be given in the form Rectangle[{xmin,ymin},{xmax,ymax}].
If the weights are not provided, regions are scored in linearly descending order with w1=1 and wn=1/n.
The following properties are supported:
 "Index" position of the selected region "Region" selected region "Weight" the weight used to score the region {prop1,…} a list of properties All all the available properties
The following options are supported:
 AcceptanceThreshold Automatic region acceptance threshold MaxOverlapFraction Automatic maximum allowed overlap fraction Method "Hard" method to use
Possible values for Method are:
 "Hard" remove regions with overlap greater than the specified threshold "Soft" iteratively rescore the regions based on their overlap

## Examples

### Basic Examples (2)

Filter out rectangles from list that have a big overlap with other rectangles in the list:

 In[1]:=
 Out[1]=

Define and display a list of rectangle shapes:

 In[2]:=
 Out[3]=
 In[4]:=
 Out[4]=

Delete rectangles with high overlap and display the results in red along with the original rectangles in black:

 In[5]:=
 Out[6]=

Only keep non-overlapping rectangles:

 In[7]:=
 Out[8]=

### Scope (4)

Perform non-max suppression on a list of regions:

 In[9]:=
 Out[10]=
 In[11]:=
 Out[11]=
 In[12]:=
 Out[12]=

Provide an explicit list of weights:

 In[13]:=
 Out[15]=

Performing non-maximum suppression eliminated some of the rectangles:

 In[16]:=
 Out[17]=

Show the result:

 In[18]:=
 Out[18]=

 In[19]:=
 Out[20]=

The index is determined by the order of the scores for rectangles that contained in the result:

 In[21]:=
 Out[21]=

Ask for a list of properties:

 In[22]:=
 Out[24]=

 In[25]:=
 Out[25]=

### Options (5)

#### AcceptanceThreshold (1)

Specify an AcceptanceThreshold for the regions:

 In[26]:=
 Out[27]=
 In[28]:=
 Out[28]=

#### MaxOverlapFraction (2)

Use to only keep regions with an overlap smaller than f:

 In[29]:=
 Out[30]=
 In[31]:=
 Out[31]=

If soft NMS is used, MaxOverlapFraction is used to defined the scaling behaviour:

 In[32]:=
 Out[34]=

If the a scaling value is given explicitly, the option is ignored:

 In[35]:=
 Out[35]=

#### Method (2)

By default, regions are removed using a hard overlap threshold:

 In[36]:=
 Out[37]=
 In[38]:=
 Out[38]=

Filter boxes using a soft NMS that scales the weights based on the overlap:

 In[39]:=
 Out[40]=

Specify the scaling value used to update the regions weights:

 In[41]:=
 Out[42]=

Soft NMS can become very slow if boxes are not removed:

 In[43]:=
 In[44]:=
 Out[44]=
 In[45]:=
 Out[45]=

Even a small threshold can significantly speed-up the computation:

 In[46]:=
 Out[46]=

## Requirements

Wolfram Language 13.0 (December 2021) or above

## Version History

• 1.0.1 – 06 December 2023
• 1.0.0 – 14 December 2021