# Converting region of interest (ROI) polygon to a mask

**URL:** https://discourse.julialang.org/t/converting-region-of-interest-roi-polygon-to-a-mask/56545
**Category:** Visualization
**Tags:** images
**Created:** [March 5, 2021, 11:14am UTC](https://discourse.julialang.org/t/converting-region-of-interest-roi-polygon-to-a-mask/56545 "2021-03-05T11:14:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mateuszbaran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mateuszbaran/32/221842_2.png) [@mateuszbaran](https://discourse.julialang.org/u/mateuszbaran)
#### Post date: [March 5, 2021, 11:14am UTC](https://discourse.julialang.org/t/converting-region-of-interest-roi-polygon-to-a-mask/56545/1 "2021-03-05T11:14:01Z")

</div>

I’d like to share a simple piece of code for converting a region of interest given by vertices of a polygon to a binary mask in Julia. This turned out to be more difficult than I expected so someone else may find this useful. I want to thank @cormullion for help and a really great library ([Luxor.jl](https://github.com/JuliaGraphics/Luxor.jl)) that I used.

So, here is my code:

```julia
using Luxor
using Images
using ImageView

w = 512
h = 512
buffer = zeros(UInt32, w, h)
@imagematrix! buffer begin
    randompoints = [Luxor.Point(1.0, 1.0), Luxor.Point(10.0, 50.0), Luxor.Point(100.0, 25.0)]
    sethue("white")
    poly(randompoints, :fill)
end 512 512

rgb_buffer = map(p -> reinterpret(Images.RGB24, p), buffer)
my_mask = Gray.(rgb_buffer) .> 0.5
imshow(my_mask)

```

You just need to change `randompoints` to your sequence of vertices and change the size if needed and that’s it 🙂 . Let me know if there is a better way to do this.

---

<div class="post-metadata">

### Author: ![Gussinsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gussinsky/32/12130_2.png) [@Gussinsky](https://discourse.julialang.org/u/Gussinsky)
#### Post date: [March 7, 2021, 10:30am UTC](https://discourse.julialang.org/t/converting-region-of-interest-roi-polygon-to-a-mask/56545/2 "2021-03-07T10:30:33Z")

</div>

I tried this way:

```julia

using Images
using GeometricalPredicates

pts = [(1.0, 1.0), (10.0, 50.0), (100.0, 25.0)]
adj = 256
randompoints = [Point(p[1] + adj, p[2] + adj) for p in pts]

w = 512
h = 512
buffer = zeros(Int, w, h)
poly = Polygon(randompoints...)
[if inpolygon(poly, Point(x, y)) buffer[x, y] = 1 end for x in collect(1:1:w), y in collect(1:1:h)];

```

then doing

```julia
Gray.(buffer)

```

should come up with a triangle.
