JuliaImages / ImageTransformations.jl

Geometric transformations on images for Julia
Other
46 stars 27 forks source link

Can warp also be used as a remap like in opencv? #64

Open jc211 opened 5 years ago

jc211 commented 5 years ago

Is it possible/recommended to add an overload to warp that takes a lookup table instead of a transform. The functionality would be similar to OpenCVs remap found here.

johnnychen94 commented 3 years ago

Absolutely, tform can be any function or function-like object that accepts StaticArrays.SVector as input and also outputs an SVector.

The default range inference does not apply to generic function, so in this case you have to specify indices as well.

A quick example:

using ImageTransformations, ImageCore, ImageShow, TestImages
using StaticArrays

function shrink_to(R0)
    function _shrink(I)
        I_first, I_last = first(R0).I, last(R0).I
        ntuple(ndims(R0)) do n
            i, from, to = I[n], I_first[n], I_last[n]
            clamp(i, from, to)
        end |> SVector
    end
end

img = testimage("cameraman")

imgs = warp(img, shrink_to(CartesianIndices((64:448, 64:448))), axes(img))

mosaic(img, imgs; nrow=1)

which gives

demo

I just came up with this very "meaningless" demo, if you have a better showcase, please share it here, or add a demo card to https://juliaimages.org/dev/examples/