ImageDraw Boundingbox MWE



Could anyone share a MWE using ImageDraw or similar to plot boundboxes on an image?
The bounding boxes were generated using Images.component_boxes
I’ve been looking at the Path methods in ImageDraw, but it doesn’t seem set up to work with the output of component_boxes.

Thanks in advance


I once quickly made my own drawbox! function during a hackaton, using the output of Images.label_components. You could check out the code in the resulting notebook for inspiration.


Sorry we don’t have the docs for ImageDraw.jl yet, but we are working on it and it wil be added soon.

component_boxes returns an array of rectangles, where each rectangle is represented by its upper left and bottom right corner points. Currently we do not have any direct method to draw such a representation of a rectangle, but you can use Polygon to construct a rectangle and then draw it using draw!.
Polygon is used to represent any closed polygon in the form of its vertices in order. So to use Polygon you would need to generate the vertices of the rectangle in order.

Here is a working example:

using Images, ImageDraw;
img = zeros(Gray{N0f8}, (300,300));
draw!(img, CirclePointRadius(Point(100,150),50));  #construct a circle with center at (x,y)=(100,150) and radius 50

diag_rects = component_boxes(label_components(img));

#get the `Polygon` representation from `diag_rects`
polys = [[(r[1][2], r[1][1]), (r[2][2], r[1][1]), (r[2][2], r[2][1]), (r[1][2], r[2][1])] for r in diag_rects];

#draw them on the same image (or a different one using `draw`)
for poly in polys
    draw!(img, Polygon(poly), Gray{N0f8}(1.0))


Perfect- thanks @annimesh2809