Skip to content

Adding Gaussian Pyramid #39

@SimonDanisch

Description

@SimonDanisch

After I couldn't find a gaussian pyramid implementation in Julia I quickly made a simple implementation:

struct Pyramid{T,M<:AbstractMatrix{T}} <: AbstractMatrix{T}
    data::Vector{M}
end

function Pyramid(data::AbstractMatrix; min_resolution=1024, mode=Linear())
    ranges(d) = (LinRange(1, size(data, 1), size(d, 1)), LinRange(1, size(data, 2), size(d, 2)))
    ET = ImageBase.restrict_eltype(first(data))
    resized = convert(Matrix{ET}, data)
    pyramid = [interpolate(eltype(ET), ET, ranges(resized), resized, Gridded(mode))]
    while any(x -> x > min_resolution, size(resized))
        resized = restrict(resized)
        interp = interpolate(eltype(ET), ET, ranges(resized), resized, Gridded(mode))
        push!(pyramid, interp)
    end
    Pyramid(pyramid)
end

function (p::Pyramid)(x::LinRange, y::LinRange)
    xystep = step.((x, y))
    maxsize = size(p.data[1])
    val, idx = findmin(p.data) do data 
        steps = step.(LinRange.(1, maxsize, size(data)))
        norm(xystep .- steps)
    end
    level = p.data[idx]
    return level(x, y)
end

function Base.size(p::Pyramid)
    return size(p.data[1])
end
function Base.show(io::IO, ::MIME"text/plain", p::Pyramid)
    show(io, p)
end
function Base.show(io::IO, p::Pyramid)
    println(io, "Pyramid with levels: $(size.(p.data))")
end

Is this something we want to add to ImageBase?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions