Skip to content

Commit ed8ef6e

Browse files
authored
Add mask_outline & bump LazyGrids compat (#29)
* Add mask_outline * Show outline in docs * LazyGrids compat and v0.10
1 parent e0bfd1a commit ed8ef6e

File tree

4 files changed

+40
-36
lines changed

4 files changed

+40
-36
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ImageGeoms"
22
uuid = "9ee76f2b-840d-4475-b6d6-e485c9297852"
33
authors = ["Jeff Fessler <[email protected]> and contributors"]
4-
version = "0.9"
4+
version = "0.10"
55

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

1111
[compat]
1212
FillArrays = "0.12, 0.13"
13-
LazyGrids = "0.4"
13+
LazyGrids = "0.4, 0.5"
1414
Requires = "1.3"
1515
julia = "1.6"

docs/lit/examples/2-mask.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ This page was generated from a single Julia file:
1616

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

2323

2424
# ### Setup
@@ -28,8 +28,8 @@ This page was generated from a single Julia file:
2828
using MIRTjim: jim, prompt # must be first!
2929
using ImageGeoms: ImageGeom, MaskCircle, MaskAllButEdge
3030
using ImageGeoms: maskit, embed, embed!, getindex!, jim #, size
31+
using ImageGeoms: mask_outline
3132
using Unitful: mm
32-
using InteractiveUtils: versioninfo
3333

3434

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

4040

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

8181

82-
# ### Mask operations
82+
# ## Mask operations
8383

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

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

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

101101

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

110110

111-
# ### Reproducibility
112-
113-
# This page was generated with the following version of Julia:
114-
115-
io = IOBuffer(); versioninfo(io); split(String(take!(io)), '\n')
116-
117-
118-
# And with the following package versions
119-
120-
import Pkg; Pkg.status()
111+
#=
112+
## Mask outline
113+
Sometimes we need the outline of the mask.
114+
=#
115+
ig = ImageGeom(MaskCircle() ; dims=(40,32))
116+
outline = mask_outline(ig.mask)
117+
jim(
118+
jim(ig.mask, "Mask"; prompt=false),
119+
jim(outline, "Outline"; prompt=false),
120+
)

src/mask.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@ mask_or(mask::AbstractArray{Bool,3}) =
4646
mask_or(mask::AbstractArray{Bool}) = throw("ndims(mask) = $(ndims(mask))")
4747

4848

49-
50-
#=
5149
"""
5250
mask_outline(mask)
53-
return outer boundary of 2D mask (or mask_or for 3D)
51+
Return outer boundary of 2D `mask` (or `mask_or` for 3D).
5452
"""
5553
function mask_outline(mask::AbstractMatrix{Bool})
56-
tmp = imfilter(mask, centered(ones(Int32,3,3))) # dilate
57-
# tmp = tmp[2:end-1,2:end-1] # 'same'
58-
return (tmp .> 1) .& (.! mask)
54+
# out = imfilter(mask, centered(ones(Int32,3,3))) # dilate
55+
# out = out[2:end-1,2:end-1] # 'same'
56+
out = copy(mask)
57+
for s1 in -1:1, s2 in -1:1
58+
out .|= circshift(mask, (s1,s2))
59+
end
60+
return @. (out > 0) & !mask
5961
end
6062
mask_outline(mask::AbstractArray{Bool,3}) = mask_outline(mask_or(mask))
61-
=#
62-
6363

6464

6565
"""
@@ -99,7 +99,7 @@ Out:
9999
function embed(x::AbstractMatrix{<:Number}, mask::AbstractArray{Bool})
100100
L = size(x,2)
101101
out = zeros(eltype(x), prod(size(mask)), L)
102-
for l=1:L
102+
for l in 1:L
103103
out[:,l] = vec(embed(x[:,l], mask))
104104
end
105105
reshape(out, size(mask)..., L)

test/mask.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# mask.jl
22

33
using ImageGeoms: embed, embed!, mask_or, maskit, getindex!
4-
#using ImageGeoms: mask_outline
4+
using ImageGeoms: mask_outline
55

66
using Test: @test, @testset, @test_throws, @inferred
77
#using SparseArrays: sparse
@@ -44,15 +44,21 @@ function mask_or_test()
4444
end
4545

4646

47-
#=
48-
function mask_outline_test() # todo
47+
function mask_outline_test()
4948
mask2 = trues(3,4)
50-
@test (@inferred mask_outline(mask2)) == falses(3,4)
49+
out2 = @inferred mask_outline(mask2)
50+
@test out2 == falses(3,4)
5151
mask3 = trues(3,4,5)
52-
@test (@inferred mask_outline(mask3)) == falses(3,4)
52+
out3 = @inferred mask_outline(mask3)
53+
@test out3 == falses(3,4)
54+
mask4 = trues(5,6)
55+
f = (a,b) -> [a, a+1, b-1, b]
56+
mask4[f(begin, end), begin:end] .= 0
57+
mask4[begin:end, f(begin,end)] .= 0
58+
out4 = @inferred mask_outline(mask4)
59+
@test count(out4) == 10
5360
true
5461
end
55-
=#
5662

5763
function maskit_test()
5864
mask = [false true true; true false false]
@@ -75,11 +81,9 @@ end
7581
@testset "mask_or" begin
7682
@test mask_or_test()
7783
end
78-
#=
7984
@testset "mask_outline" begin
8085
@test mask_outline_test()
8186
end
82-
=#
8387
@testset "maskit" begin
8488
@test maskit_test()
8589
end

0 commit comments

Comments
 (0)