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

swirl operation #146

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/examples/operations/swirl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# and pass it to `warp`. This swirl example comes from
# [the Princeton Computer Graphics course for Image Warping (Fall 2000)](https://www.cs.princeton.edu/courses/archive/fall00/cs426/lectures/warp/warp.pdf)
# and [scikit-image swirl example](https://scikit-image.org/docs/dev/auto_examples/transform/plot_swirl.html).
# To apply the swirl effect, please use the existing `swirl` function in ImageTransformations.jl directly.

using ImageTransformations
using OffsetArrays, StaticArrays
Expand Down
4 changes: 3 additions & 1 deletion src/ImageTransformations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ export
warpedview,
InvWarpedView,
invwarpedview,
imrotate
imrotate,
swirl

include("autorange.jl")
include("interpolations.jl")
include("warp.jl")
include("warpedview.jl")
include("invwarpedview.jl")
include("resizing.jl")
include("swirl.jl")
include("compat.jl")
include("deprecated.jl")

Expand Down
37 changes: 37 additions & 0 deletions src/swirl.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
swirl(img, rotation, strength, radius, x0 = OffsetArrays.center(img))

Create whirlpool effect on `img`

# Arguments
- `img`: to specify input image array
- `rotation`: to specify rotation angle(clockwise)
- `strength`:to specify amount of swirl
- `radius`:to specify extent of swirl
- `x0` (optional): to specify the center point of the swirl. The default value is `OffsetArrays.center(img)`.
# Examples

```julia
using ImageTransformations, TestImages


img = imresize(testimage("cameraman"), (256, 256));

preview = swirl(img, 0, 10, 50, 100) # swirl with rotation 0, strength 10, radius 50, x0 center 100
```
"""
function swirl(img, rotation, strength, radius, x0 = OffsetArrays.center(img))
r = log(2)*radius/5

function swirl_map(x::SVector{N}) where N
xd = x .- x0
ρ = norm(xd)
θ = atan(reverse(xd)...)

θ̃ = θ + rotation + strength * exp(-ρ/r)

SVector{N}(x0 .+ ρ .* reverse(sincos(θ̃)))
end

warp(img, swirl_map, axes(img))
end
5 changes: 3 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ ambs = detect_ambiguities(ImageTransformations, CoordinateTransformations, Base,
@test isempty(setdiff(ambs, refambs))

# helper function to compare NaN
nearlysame(x, y) = x ≈ y || (isnan(x) & isnan(y))
nearlysame(A::AbstractArray, B::AbstractArray) = all(map(nearlysame, A, B))
nearlysame(x, y, atol = 0.0) = isapprox(x, y; atol = atol) || (isnan(x) & isnan(y))
nearlysame(A::AbstractArray, B::AbstractArray; atol = 0.0) = all(map((a, b, atol)-> nearlysame(a, b, atol), A, B, atol))

tests = [
"autorange.jl",
"resizing.jl",
"interpolations.jl",
"warp.jl",
"swirl.jl",
"deprecated.jl" # test deprecations in the last
]

Expand Down
20 changes: 20 additions & 0 deletions test/swirl.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@testset "Swirl Operation" begin

img = [0 0 0 0 0
0 0 1 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 0 0]
Comment on lines +3 to +7
Copy link
Member

Choose a reason for hiding this comment

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

I think we would also want to test OffsetArrays and Gray/RGB images.

expected = [ NaN NaN 0.0 0.0 NaN
0.0 0.431797 0.476449 0.431797 NaN
0.0 0.476449 0.0 0.393894 0.0
NaN 0.431797 0.0825553 0.0 0.0
NaN 0.0 0.0 NaN NaN]
res = swirl(img, 1, 10, 1)
ashwanirathee marked this conversation as resolved.
Show resolved Hide resolved
replace!(res, NaN=>0.0)
replace!(expected, NaN=>0.0)
@test nearlysame(res, expected)

@test nearlysame(swirl(img, 1, 10, 1), swirl(img, 1, 10, 1, OffsetArrays.center(img)))

end