JuliaImages / JuliaImages/ImageDraw.jl
Implementation of poly2mask from matlab
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 27
- Forks
- 16
- PR merge metrics
- No merged PRs in 30d
Description
Hi,
I was looking for implementation of a function, which would fill an image region based on the provided polygon. It exists in Matlab (poly2mask) and also was re-implemented in python (see the discussion).
The implementation is almost trivial (see below), and the function seems generally useful. Would you be interested in me making a PR?
function point_in_polygon(poly_xs::Vector{T}, poly_ys::Vector{T}, x::T, y::T) where T<: Real
n_verts = length(poly_xs)
j = n_verts
c = false
for i in 1:n_verts
if (((poly_ys[i] <= y) && (y < poly_ys[j])) || ((poly_ys[j] <= y) && (y < poly_ys[i]))) &&
(x < (poly_xs[j] - poly_xs[i]) * (y - poly_ys[i]) / (poly_ys[j] - poly_ys[i]) + poly_xs[i])
c = !c
end
j = i
end
return c
end
function draw_polygon!(mask::Matrix{T2}, poly_xs::Vector{T}, poly_ys::Vector{T}, value::T2) where T<: Integer where T2 <: Real
min_x, max_x = max(minimum(poly_xs), 1), min(maximum(poly_xs), size(mask, 2))
min_y, max_y = max(minimum(poly_ys), 1), min(maximum(poly_ys), size(mask, 1))
for y in min_y:max_y
for x in min_x:max_x
if point_in_polygon(poly_xs, poly_ys, x, y)
mask[y, x] = value
end
end
end
end
function polygons_to_mask(polygons::Array{Matrix{T}, 1} where T <: Real, max_x::Int, max_y::Int)
poly_mask = zeros(Int, max_y, max_x);
for (i,p) in enumerate(polygons)
draw_polygon!(poly_mask, round.(Int, p[:,1]), round.(Int, p[:,2]), i)
end
return poly_mask
end
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the issue's MATLAB poly2mask reference and the supplied Julia point_in_polygon, draw_polygon!, and polygons_to_mask examples. Confirm the desired polygon and mask behavior, then add coverage showing that polygon regions are converted into the expected mask values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- computer-vision
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100