Skip to content

Commit

Permalink
Add mask_outline & bump LazyGrids compat (#29)
Browse files Browse the repository at this point in the history
* Add mask_outline

* Show outline in docs

* LazyGrids compat and v0.10
  • Loading branch information
JeffFessler authored Nov 28, 2022
1 parent e0bfd1a commit ed8ef6e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ImageGeoms"
uuid = "9ee76f2b-840d-4475-b6d6-e485c9297852"
authors = ["Jeff Fessler <[email protected]> and contributors"]
version = "0.9"
version = "0.10"

[deps]
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
Expand All @@ -10,6 +10,6 @@ Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[compat]
FillArrays = "0.12, 0.13"
LazyGrids = "0.4"
LazyGrids = "0.4, 0.5"
Requires = "1.3"
julia = "1.6"
34 changes: 17 additions & 17 deletions docs/lit/examples/2-mask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ This page was generated from a single Julia file:

#md # The corresponding notebook can be viewed in
#md # [nbviewer](http://nbviewer.jupyter.org/) here:
#md # [`1-overview.ipynb`](@__NBVIEWER_ROOT_URL__/1-overview.ipynb),
#md # [`2-mask.ipynb`](@__NBVIEWER_ROOT_URL__/2-mask.ipynb),
#md # and opened in [binder](https://mybinder.org/) here:
#md # [`1-overview.ipynb`](@__BINDER_ROOT_URL__/1-overview.ipynb).
#md # [`2-mask.ipynb`](@__BINDER_ROOT_URL__/2-mask.ipynb).


# ### Setup
Expand All @@ -28,8 +28,8 @@ This page was generated from a single Julia file:
using MIRTjim: jim, prompt # must be first!
using ImageGeoms: ImageGeom, MaskCircle, MaskAllButEdge
using ImageGeoms: maskit, embed, embed!, getindex!, jim #, size
using ImageGeoms: mask_outline
using Unitful: mm
using InteractiveUtils: versioninfo


# The following line is helpful when running this file as a script;
Expand All @@ -38,9 +38,9 @@ using InteractiveUtils: versioninfo
isinteractive() ? jim(:prompt, true) : prompt(:draw);


# ### Mask overview

#=
## Mask overview
In tomographic image reconstruction, patients are usually more "round"
than "square" so often we only want to estimate the pixels inside some
support `mask`: a `Bool` array indicating which pixels are to be estimated.
Expand Down Expand Up @@ -79,7 +79,7 @@ ig = ImageGeom(MaskAllButEdge() ; dims=(32,32,16))
jim(ig)


# ### Mask operations
# ## Mask operations

# Often we need to extract the pixel values within a mask:

Expand All @@ -95,7 +95,7 @@ core = ramp[ig.mask]
# Or equivalently:
maskit(ramp, ig.mask)

# Conversely, we can embed that list of pixels back into an array:
# Conversely, we can `embed` that list of pixels back into an array:
array = embed(core, ig.mask)


Expand All @@ -108,13 +108,13 @@ array = collect(zeros(Float16, ig))
embed!(array, core, ig.mask)


# ### Reproducibility

# This page was generated with the following version of Julia:

io = IOBuffer(); versioninfo(io); split(String(take!(io)), '\n')


# And with the following package versions

import Pkg; Pkg.status()
#=
## Mask outline
Sometimes we need the outline of the mask.
=#
ig = ImageGeom(MaskCircle() ; dims=(40,32))
outline = mask_outline(ig.mask)
jim(
jim(ig.mask, "Mask"; prompt=false),
jim(outline, "Outline"; prompt=false),
)
18 changes: 9 additions & 9 deletions src/mask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ mask_or(mask::AbstractArray{Bool,3}) =
mask_or(mask::AbstractArray{Bool}) = throw("ndims(mask) = $(ndims(mask))")



#=
"""
mask_outline(mask)
return outer boundary of 2D mask (or mask_or for 3D)
Return outer boundary of 2D `mask` (or `mask_or` for 3D).
"""
function mask_outline(mask::AbstractMatrix{Bool})
tmp = imfilter(mask, centered(ones(Int32,3,3))) # dilate
# tmp = tmp[2:end-1,2:end-1] # 'same'
return (tmp .> 1) .& (.! mask)
# out = imfilter(mask, centered(ones(Int32,3,3))) # dilate
# out = out[2:end-1,2:end-1] # 'same'
out = copy(mask)
for s1 in -1:1, s2 in -1:1
out .|= circshift(mask, (s1,s2))
end
return @. (out > 0) & !mask
end
mask_outline(mask::AbstractArray{Bool,3}) = mask_outline(mask_or(mask))
=#



"""
Expand Down Expand Up @@ -99,7 +99,7 @@ Out:
function embed(x::AbstractMatrix{<:Number}, mask::AbstractArray{Bool})
L = size(x,2)
out = zeros(eltype(x), prod(size(mask)), L)
for l=1:L
for l in 1:L
out[:,l] = vec(embed(x[:,l], mask))
end
reshape(out, size(mask)..., L)
Expand Down
20 changes: 12 additions & 8 deletions test/mask.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# mask.jl

using ImageGeoms: embed, embed!, mask_or, maskit, getindex!
#using ImageGeoms: mask_outline
using ImageGeoms: mask_outline

using Test: @test, @testset, @test_throws, @inferred
#using SparseArrays: sparse
Expand Down Expand Up @@ -44,15 +44,21 @@ function mask_or_test()
end


#=
function mask_outline_test() # todo
function mask_outline_test()
mask2 = trues(3,4)
@test (@inferred mask_outline(mask2)) == falses(3,4)
out2 = @inferred mask_outline(mask2)
@test out2 == falses(3,4)
mask3 = trues(3,4,5)
@test (@inferred mask_outline(mask3)) == falses(3,4)
out3 = @inferred mask_outline(mask3)
@test out3 == falses(3,4)
mask4 = trues(5,6)
f = (a,b) -> [a, a+1, b-1, b]
mask4[f(begin, end), begin:end] .= 0
mask4[begin:end, f(begin,end)] .= 0
out4 = @inferred mask_outline(mask4)
@test count(out4) == 10
true
end
=#

function maskit_test()
mask = [false true true; true false false]
Expand All @@ -75,11 +81,9 @@ end
@testset "mask_or" begin
@test mask_or_test()
end
#=
@testset "mask_outline" begin
@test mask_outline_test()
end
=#
@testset "maskit" begin
@test maskit_test()
end

2 comments on commit ed8ef6e

@JeffFessler
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator() register

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/73034

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.10.0 -m "<description of version>" ed8ef6edde2ca19a508668cc438f15ee007d882d
git push origin v0.10.0

Please sign in to comment.