Skip to content

Commit

Permalink
Fix #59: Migrate from Plots.jl to Makie.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
juliohm committed May 29, 2024
1 parent cd7f3ed commit 3df6956
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 87 deletions.
9 changes: 7 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ ImageFiltering = "6a3955dd-da59-5b1f-98d4-e7296123deb5"
ImageMorphology = "787d08f9-d448-5407-9aad-5290dd7ab264"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[weakdeps]
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"

[extensions]
ImageQuiltingMakieExt = "Makie"

[compat]
CUDA = "3.11, 4.0, 5.0"
CpuId = "0.3"
Expand All @@ -26,9 +31,9 @@ Graphs = "1.4"
GraphsFlows = "0.1"
ImageFiltering = "0.7"
ImageMorphology = "0.4"
Makie = "0.20"
ProgressMeter = "1.1"
Random = "1.9"
RecipesBase = "1.0"
SparseArrays = "1.9"
Statistics = "1.9"
StatsBase = "0.33, 0.34"
Expand Down
14 changes: 8 additions & 6 deletions docs/src/voxel-reuse.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ voxelreuse
## Plot recipe

A plot recipe is provided for tile design in image quilting. In order to plot the voxel
reuse of a training image, install [Plots.jl](https://github.com/JuliaPlots/Plots.jl) and
any of its supported backends (e.g. [GR.jl](https://github.com/jheinen/GR.jl)):
reuse of a training image, install any of the [Makie.jl](https://docs.makie.org) backends.

```julia
] add Plots
] add CairoMakie
```

The example below uses training images from the
Expand All @@ -22,12 +21,15 @@ The example below uses training images from the
```julia
using ImageQuilting
using GeoStatsImages
using Plots
using CairoMakie

TI₁ = geostatsimage("Strebelle")
TI₂ = geostatsimage("StoneWall")

voxelreuseplot(TI₁, label="Strebelle")
voxelreuseplot!(TI₂, label="StoneWall")
timg₁ = asarray(TI₁, :facies)
timg₂ = asarray(TI₂, :Z)

voxelreuseplot(timg₁)
voxelreuseplot!(timg₂)
```
![Voxel reuse plot](images/voxelreuse.png)
85 changes: 85 additions & 0 deletions ext/ImageQuiltingMakieExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENCE in the project root.
# ------------------------------------------------------------------

module ImageQuiltingMakieExt

using ImageQuilting
using Random

import Makie
import ImageQuilting: voxelreuseplot, voxelreuseplot!

Makie.@recipe(VoxelReusePlot, trainimg) do scene
Makie.Attributes(;
tmin=nothing,
tmax=nothing,
overlap=(1 / 6, 1 / 6, 1 / 6),
nreal=10,
rng=Random.default_rng()
)
end

Makie.preferred_axis_type(::VoxelReusePlot) = Makie.Axis

function Makie.plot!(plot::VoxelReusePlot)
# retrieve inputs
trainimg = plot[:trainimg][]
overlap = plot[:overlap][]
nreal = plot[:nreal][]
rng = plot[:rng][]

ndims(trainimg) == 3 || throw(ArgumentError("image is not 3D (add ghost dimension in 2D)"))

# choose tile size range
tmin, tmax, idx = tminmax(plot)

# save support as an array
ts = collect(tmin:tmax)

# compute voxel reuse for each tile size
μσ = map(ts) do t
tilesize = ntuple(i -> idx[i] ? t : 1, 3)
voxelreuse(trainimg, tilesize; overlap=overlap, nreal=nreal, rng=rng)
end
μs = first.(μσ)
σs = last.(μσ)

# optimal tile size range
rank = sortperm(μs, rev=true)
best = ts[rank[1:min(5, length(ts))]]
t₋, t₊ = minimum(best), maximum(best)

Makie.vspan!(plot, [t₋], [t₊], alpha = 0.5, color = :slategray3)
Makie.band!(plot, ts, μs - σs, μs + σs, alpha = 0.5, color = :slategray3)
Makie.lines!(plot, ts, μs, color = :slategray3)
end

function Makie.data_limits(plot::VoxelReusePlot)
tmin, tmax, idx = tminmax(plot)
pmin = Makie.Point3f(0, 0, 0)
pmax = Makie.Point3f(tmax, 1, 0)
Makie.Rect3f([pmin, pmax])
end

function tminmax(plot::VoxelReusePlot)
# retrieve inputs
trainimg = plot[:trainimg][]
tmin = plot[:tmin][]
tmax = plot[:tmax][]

# image size
dims = size(trainimg)
idx = [dims...] .> 1

# default support
isnothing(tmin) && (tmin = 7)
isnothing(tmax) && (tmax = min(100, minimum(dims[idx])))

tmin > 0 || throw(ArgumentError("`tmin` must be positive"))
tmin < tmax || throw(ArgumentError("`tmin`` must be smaller than `tmax`"))

tmin, tmax, idx
end

end
11 changes: 8 additions & 3 deletions src/ImageQuilting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ using StatsBase: sample, weights
using ProgressMeter: Progress, next!
using FFTW: set_num_threads
using CpuId: cpucores
using RecipesBase
using CUDA

using Base: @nexprs, @nloops, @nref
Expand All @@ -21,14 +20,20 @@ using Statistics: mean, std
using Random

include("utils.jl")
include("plotrecipes.jl")
include("imfilter.jl")
include("relaxation.jl")
include("taumodel.jl")
include("graphcut.jl")
include("iqsim.jl")
include("voxelreuse.jl")

export iqsim, voxelreuse
export
# simulation
iqsim,

# voxel reuse
voxelreuse,
voxelreuseplot,
voxelreuseplot!

end
76 changes: 0 additions & 76 deletions src/plotrecipes.jl

This file was deleted.

16 changes: 16 additions & 0 deletions src/voxelreuse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ function voxelreuse(

μ, σ
end

"""
voxelreuseplot(trainimg; [options])
Mean voxel reuse plot of `trainimg`.
## Available options:
* `tmin` - minimum tile size (default to `7`)
* `tmax` - maximum tile size (default to `minimum(size(trainimg))`)
* `overlap` - the percentage of overlap (default to 1/6 of tile size)
* `nreal` - number of realizations (default to 10)
* `rng` - random number generator (default to `Random.default_rng()`)
"""
function voxelreuseplot end
function voxelreuseplot! end

0 comments on commit 3df6956

Please sign in to comment.