# ImageDraw Boundingbox MWE

**URL:** <https://discourse.julialang.org/t/imagedraw-boundingbox-mwe/4838>\
**Category:** Visualization\
**Tags:** question\
**Created:** [July 13, 2017, 2:28pm UTC](https://discourse.julialang.org/t/imagedraw-boundingbox-mwe/4838 "2017-07-13T14:28:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rand5](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rand5/32/867_2.png) [@rand5](https://discourse.julialang.org/u/rand5)\
**Post date:** [July 13, 2017, 2:28pm UTC](https://discourse.julialang.org/t/imagedraw-boundingbox-mwe/4838/1 "2017-07-13T14:28:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ken-B](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken-b/32/1780_2.png) [@Ken-B](https://discourse.julialang.org/u/Ken-B)\
**Post date:** [July 13, 2017, 10:19pm UTC](https://discourse.julialang.org/t/imagedraw-boundingbox-mwe/4838/2 "2017-07-13T22:19:40Z")

</div>

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](https://github.com/Ken-B/AstroHack2017/blob/master/final_presentation_AstroWhack.ipynb) for inspiration.

---

<div class="post-metadata">

**Author:** ![annimesh2809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/annimesh2809/32/1766_2.png) [@annimesh2809](https://discourse.julialang.org/u/annimesh2809)\
**Post date:** [July 14, 2017, 12:50pm UTC](https://discourse.julialang.org/t/imagedraw-boundingbox-mwe/4838/3 "2017-07-14T12:50:09Z")

</div>

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:

```julia
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))
end

```

---

<div class="post-metadata">

**Author:** ![rand5](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rand5/32/867_2.png) [@rand5](https://discourse.julialang.org/u/rand5)\
**Post date:** [July 14, 2017, 1:04pm UTC](https://discourse.julialang.org/t/imagedraw-boundingbox-mwe/4838/4 "2017-07-14T13:04:22Z")

</div>

Perfect- thanks @annimesh2809
