Function Repository Resource:

ObjectDetectionAnchorBoxes

Source Notebook

Create an array of candidate bounding boxes for object detection (anchors)

Contributed by: Davit Baghdasaryan

ResourceFunction["ObjectDetectionAnchorBoxes"][{w,h},{s1,s2,},{r1,r2,}]

computes the array of anchor boxes with combinations of sizes si and aspect ratios ri for each cell in a grid with size {w,h}.

Details and Options

Ratios ri and sizes si must be real values from 0 to 1.
The number of anchors centered on each cell of a grid is generated with width and height {si*Sqrt[rj],si/Sqrt[rj]} with different combinations of si and rj.
Anchor coordinates values typically are from 0 to 1.
The following options are supported:
MethodFullthe method to generate anchors per pixel
"ClippingQ"Falsewhether to clip out-of-boundary anchors
"Steps"Automaticgird steps in vertical and horizontal directions
"Offsets"{0.5,0.5}anchor offsets from the bottom left corner of a grid cell
"CoordinateSystem"“ReversedGraphicsCoordinates"coordinate system of the anchors
"BoundingBoxFormat""Corner"anchor format: "Corner", "Center" or "Rectangle"
"CoordinateSystem" can be "GraphicsCoordinates" with origin in the bottom left corner or "ReversedGraphicsCoordinates" with origin in the top left corner.
Cells are sorted from left to right, from top to bottom independent of the "CoordinateSystem" value.
If Method option is “SSD", size and ratio combinations are {si*Sqrt[r1],si/Sqrt[r1]}, and {s1*Sqrt[rj],s1/Sqrt[rj]}, with i from 1 to n, j from 2 to m, with total number n+m-1, and where n and m are the length of the scale and ratios lists. If Method is Full, all combinations of sizes and ratios are used with a total number n*m per cell.
For "BoundingBoxFormat""Corner", the result is a list of the form {xmin,ymin,xmax,ymax} while for "BoundingBoxFormat""Center", the result is in the form {xcenter,ycenter,width,height}.
Setting “BoundingBoxFormat to "Rectangle"returns list of Rectangle objects.

Examples

Basic Examples (3) 

Generate anchor box coordinates:

In[1]:=
ResourceFunction["ObjectDetectionAnchorBoxes"][{2, 2}, {0.2}, {1} ]
Out[1]=

Generate the anchor boxes in Rectangle format and then show them using Graphics function:

In[2]:=
anchors = ResourceFunction["ObjectDetectionAnchorBoxes"][{3, 3}, {0.2}, {1}, "BoundingBoxFormat" -> "Rectangle" ]
Out[2]=
In[3]:=
Graphics[{EdgeForm[Black], Opacity[0.1], anchors}, PlotRange -> {{0, 1}, {0, 1}}, Axes -> True]
Out[3]=

Generate the anchor boxes for 3 by 3 grid:

In[4]:=
anchors = ResourceFunction[
  "ObjectDetectionAnchorBoxes"][{3, 3}, {0.2}, {0.5, 2}, "BoundingBoxFormat" -> "Rectangle", Method -> "SSD"]
Out[4]=
In[5]:=
Graphics[{EdgeForm[Black], Opacity[0.1], anchors}, PlotRange -> {{0, 1}, {0, 1}}, Axes -> True]
Out[5]=

Scope (5) 

Generate and visualize anchors with one size and one ratio:

In[6]:=
anchors = ResourceFunction["ObjectDetectionAnchorBoxes"][{3, 3}, {0.2}, {1.}, "BoundingBoxFormat" -> "Rectangle"];
Graphics[{EdgeForm[Black], Opacity[0.1], anchors}, PlotRange -> {{0, 1}, {0, 1}}, Axes -> True]
Out[7]=

Generate anchors with two sizes and one ratio:

In[8]:=
anchors = ResourceFunction[
   "ObjectDetectionAnchorBoxes"][{3, 3}, {.2, .3}, {1.}, "BoundingBoxFormat" -> "Rectangle"];
Graphics[{EdgeForm[Black], Opacity[0.1], {RandomColor[], anchors}}, PlotRange -> {{0, 1}, {0, 1}}, Axes -> True]
Out[9]=

Generate anchors with two ratios and one size:

In[10]:=
anchors = ResourceFunction[
   "ObjectDetectionAnchorBoxes"][{3, 3}, {.2}, {1., 2}, "BoundingBoxFormat" -> "Rectangle"];
Graphics[{EdgeForm[Black], Opacity[0.1], {RandomColor[], anchors}}, PlotRange -> {{0, 1}, {0, 1}}, Axes -> True]
Out[11]=

Note that generating the Anchors with two aspect ratios and two sizes leads to 3 anchors per pixel because of the default Method-> "SSD" (see Options section):

In[12]:=
anchors = ResourceFunction[
   "ObjectDetectionAnchorBoxes"][{3, 3}, {.2, .3}, {1., 2}, "BoundingBoxFormat" -> "Rectangle"];
Graphics[{EdgeForm[Black], Opacity[0.1], {RandomColor[], anchors}}, PlotRange -> {{0, 1}, {0, 1}}, Axes -> True]
Out[13]=

Use MethodFull instead:

In[14]:=
anchors = ResourceFunction[
   "ObjectDetectionAnchorBoxes"][{3, 3}, {.2, .3}, {1., 2}, "BoundingBoxFormat" -> "Rectangle", Method -> Full, "ClippingQ" -> True];
Graphics[{EdgeForm[Black], Opacity[0.1], {RandomColor[], anchors}}, PlotRange -> {{0, 1}, {0, 1}}, Axes -> True,
 PlotRangePadding -> 0.1]
Out[15]=

Options (7) 

Method (2) 

With the default method all combinations of the sizes and aspect ratios are used:

In[16]:=
ResourceFunction[
 "ObjectDetectionAnchorBoxes"][{1, 1}, {.2, .3}, {1., 2}, "BoundingBoxFormat" -> "Center"]
Out[16]=

With the "SSD" Method option only the first size and first ratio are combined with the others:

In[17]:=
ResourceFunction[
 "ObjectDetectionAnchorBoxes"][{1, 1}, {.2, .3}, {1., 2}, "BoundingBoxFormat" -> "Center", Method -> "SSD"]
Out[17]=

ClippingQ (2) 

If the scale of the bounding boxes is large enough then it can be out of the image boundary:

In[18]:=
anchors = ResourceFunction[
   "ObjectDetectionAnchorBoxes"][{3, 3}, {.5, .6}, {1.}, "BoundingBoxFormat" -> "Rectangle", Method -> Full, "ClippingQ" -> False];
Graphics[{EdgeForm[Black], Opacity[0.1], {RandomColor[], anchors}}, PlotRange -> {{0, 1}, {0, 1}}, Axes -> True,
 PlotRangePadding -> 0.1]
Out[19]=

The above can be fixed by specifying the "ClippingQ" option to True to clip the bounding boxes:

In[20]:=
anchors = ResourceFunction[
   "ObjectDetectionAnchorBoxes"][{3, 3}, {.5, .6}, {1.}, "BoundingBoxFormat" -> "Rectangle", Method -> Full, "ClippingQ" -> True];
Graphics[{EdgeForm[Black], Opacity[0.1], {RandomColor[], anchors}}, PlotRange -> {{0, 1}, {0, 1}}, Axes -> True,
 PlotRangePadding -> 0.1]
Out[21]=

BoundingBoxFormat (3) 

By default bounding boxes are returned as {xmin,ymin,xmax,ymax}:

In[22]:=
ResourceFunction["ObjectDetectionAnchorBoxes"][{2, 2}, {0.2}, {1}]
Out[22]=

Use "BoundingBoxFormat""Center" to create anchors in the format {xcenter,ycenter,w,h}:

In[23]:=
ResourceFunction["ObjectDetectionAnchorBoxes"][{2, 2}, {0.2}, {1}, "BoundingBoxFormat" -> "Center"]
Out[23]=

Use "BoundingBoxFormat""Rectangle" to create anchor in the format of Rectangle[{xmin,ymin},{xmax,ymax}]:

In[24]:=
ResourceFunction["ObjectDetectionAnchorBoxes"][{2, 2}, {0.2}, {1}, "BoundingBoxFormat" -> "Rectangle"]
Out[24]=

Neat Examples (2) 

Retrieve a large image:

In[25]:=
image = ExampleData[{"TestImage", "Apples"}];
In[26]:=
ImageDimensions[image]
Out[26]=

Create image patches using ObjectDetectionAnchorBoxes:

In[27]:=
ImageTrim[image, ResourceFunction[
   "ObjectDetectionAnchorBoxes"][{4, 4}, {.2, .3}, {1.}, "BoundingBoxFormat" -> "Rectangle"], DataRange -> {{0, 1}, {0, 1}}][[;; 5]]
Out[27]=

Version History

  • 1.0.0 – 22 March 2022

Related Resources

License Information