Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Flood Fill Implementation #60

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

ashwanirathee
Copy link
Member

@ashwanirathee ashwanirathee commented May 15, 2021

Initial Base

Tasks:

  • Lots of tests
  • Buggy implementation fix(it works for some cases, in some others it doesn't)
  • Doc string of flood fill
  • Documentation of both boundary fill and flood fill

@ashwanirathee ashwanirathee changed the title Flood Fill Implementation WIP: Flood Fill Implementation May 15, 2021
@ashwanirathee
Copy link
Member Author

ashwanirathee commented May 23, 2021

When I don't specify a kwarg in the algorithm( which will be really optional for FloodFill as it won't really need it unless we make it to be so), Ambiguity error is thrown. The draw in core.jl is very general, so there is that.

julia> res = draw(img, verts, FloodFill(350,200,fill_value =RGB(0), current_value = img[200,350]))
ERROR: MethodError: draw(::Matrix{RGB{FixedPointNumbers.N0f8}}, ::Vector{CartesianIndex{2}}, ::FloodFill{RGB{FixedPointNumbers.N0f8}}) is ambiguous. Candidates:
  draw(img::AbstractMatrix{T}, args...) where T<:Colorant in ImageDraw at /home/ashwani/julia-related/ImageDraw.jl/src/core.jl:385
  draw(img::AbstractArray{var"#s36", N} where var"#s36"<:Colorant, verts::Array{CartesianIndex{N}, 1}, f::ImageDraw.AbstractPolyFillAlgorithm; kwargs...) where N in ImageDraw at /home/ashwani/julia-related/ImageDraw.jl/src/polygonfill2d.jl:118
Possible fix, define
  draw(::AbstractMatrix{T}, ::Vector{CartesianIndex{2}}, ::ImageDraw.AbstractPolyFillAlgorithm) where T<:Colorant
Stacktrace:
  • How and where current_value in flood fill is set is also slight bit of problem as our structs as immutable, so user kind of need to specify color of the seed point. I can't specify a default value in constructor, as fetching color at a particular point in image require access to that image which isn't there at that time.
    Example:
 res = draw(img, verts, FloodFill(350,200,fill_value =RGB(0), current_value = img[200,350]);closed=true)

user need to specify the draw like this for current _value, otherwise it won't work. We are not able to set default value in constructor as it doesn't have access to img/res during object creation. It would be better if we set it internally though to avoid error.

FloodFill(x::Int = 1, y::Int = 1; fill_value::Colorant = RGB(1), current_value::Colorant = res[y, x]) = FloodFill(x, y, fill_value, current_value)
FloodFill(p::CartesianIndex{2}; fill_value::Colorant = RGB(1), current_value::Colorant = res[y,x]) = FloodFill(p[1], p[2], fill_value, current_value)
julia> res = draw(img, verts, FloodFill(350,200,fill_value =RGB(0));closed=true)
ERROR: UndefVarError: res not defined
Stacktrace:
 [1] top-level scope
   @ REPL[42]:1

And we can't change it inside the program as it's immutable(with setfield we can) but it needs to specified at the start as it only needs initial color value of seed.

Copy link
Member

@johnnychen94 johnnychen94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I don't specify a kwarg in the algorithm( which will be really optional for FloodFill as it won't really need it unless we make it to be so), Ambiguity error is thrown.

It looks like the ambiguity is triggered because the type annotation of img is not the same; so Julia can't figure out an order here.

Does this patch fix it?

- draw(img::AbstractArray{T,2}, args...) where {T<:Colorant} = draw!(copy(img), args...)
+ draw(img::AbstractArray{T}, args...; kwargs...) where {T<:Colorant} = draw!(copy(img), args...; kwargs...)

x::Int,
y::Int,
) where {T<:Colorant}
if (res[y, x] != f.current_value || res[y, x] == f.fill_value) return end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get current_value through something like res[x, y] (or res[y,x])?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that can be done. We want to set the value of current_value at time when struct creation is called, but at that time res is not accessible to struct . There will be quick way to solve it 😄 , I need to lookup .

) where {T<:Colorant}
if (res[y, x] != f.current_value || res[y, x] == f.fill_value) return end
) where {T<:Colorant, N}
if (res[y, x] != f.current_value || res[y, x] == f.fill_value) return res end
if checkbounds(Bool, res, y, x) res[y, x] = f.fill_value end
f(res, verts, x + 1, y)
f(res, verts, x - 1, y)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can still be {T, 2} as the implementation is specified for matrix; this won't affect the method ambiguity on draw so no need to change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants