Skip to content

Commit

Permalink
Allow alpha maps in heatmap_overlay (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrhill authored Mar 6, 2024
1 parent 2cacfed commit b3fca8d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# VisionHeatmaps.jl
## Version `v1.5.0`
* ![Feature][badge-feature] Allow alpha maps in `heatmap_overlay` ([#11][pr-11])

## Version `v1.4.0`
* ![Feature][badge-feature] Add heatmap overlays on Explanations ([#10][pr-10])
* ![Maintenance][badge-maintenance] Use Configurations.jl for keyword argument handling ([#9][pr-9])


## Version `v1.3.1`
* ![Feature][badge-feature] Add XAIBase dependency ([#7][pr-7], [#8][pr-8])

Expand All @@ -17,6 +19,7 @@
## Version `v1.0.0`
* Initial release

[pr-11]: https://github.com/Julia-XAI/VisionHeatmaps.jl/pull/11
[pr-10]: https://github.com/Julia-XAI/VisionHeatmaps.jl/pull/10
[pr-9]: https://github.com/Julia-XAI/VisionHeatmaps.jl/pull/9
[pr-8]: https://github.com/Julia-XAI/VisionHeatmaps.jl/pull/8
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "VisionHeatmaps"
uuid = "27106da1-f8bc-4ca8-8c66-9b8289f1e035"
authors = ["Adrian Hill <gh@adrianhill.de>"]
version = "1.4.0"
version = "1.5.0-DEV"

[deps]
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Expand Down
10 changes: 6 additions & 4 deletions src/overlay.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Assumes 4D input array following the WHCN convention
(width, height, color channels, batch dimension) and batch size 1.
## Keyword arguments
- `alpha::Real`: Opacity of the heatmap overlay. Defaults to `$DEFAULT_OVERLAY_ALPHA`.
- `alpha`: Opacity of the heatmap overlay. Defaults to `$DEFAULT_OVERLAY_ALPHA`.
If an array is passed, it will be broadcast with the image and heatmap.
All alpha values are expected to be between 0 and 1.
- `resize_method`: Method used to resize the heatmap in case of a size mismatch with the image.
Defaults to `Lanczos(1)` from Interpolations.jl.
Expand All @@ -32,7 +34,7 @@ end
function heatmap_overlay(
val::AbstractArray{T,N},
im::AbstractImage,
alpha::Real,
alpha,
resize_method,
options::HeatmapOptions,
) where {T,N}
Expand All @@ -44,7 +46,7 @@ function heatmap_overlay(
),
)
end
if alpha < 0 || alpha > 1
if any(<(0), alpha) || any(>(1), alpha)
throw(ArgumentError("alpha must be in the range [0, 1]"))
end

Expand All @@ -54,7 +56,7 @@ function heatmap_overlay(
if hmsize != imsize
hm = imresize(hm, imsize; method=resize_method)
end
return im * (1 - alpha) + hm * alpha
return @. im * (1 - alpha) + hm * alpha
end

#=================#
Expand Down
3 changes: 3 additions & 0 deletions test/references/overlay_alpha/matrix.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
▀▀▀▀▀▀
▀▀▀▀▀▀
▀▀▀▀▀▀
3 changes: 3 additions & 0 deletions test/references/overlay_alpha/scalar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
▀▀▀▀▀▀
▀▀▀▀▀▀
▀▀▀▀▀▀
21 changes: 21 additions & 0 deletions test/test_heatmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ end
end
end

@testset "Overlay alpha masks" begin
alpha = 0.2
ho = heatmap_overlay(A, img2; alpha=alpha)
@test size(ho) == size(img2)
@test_reference "references/overlay_alpha/scalar.txt" ho

alpha = [(x + y) / 2 for x in 0:0.2:1, y in 0:0.2:1]
ho = heatmap_overlay(A, img2; alpha=alpha)
@test size(ho) == size(img2)
@test_reference "references/overlay_alpha/matrix.txt" ho

alpha = fill!(alpha, 0.2)
ho = heatmap_overlay(A, img2; alpha=alpha)
@test size(ho) == size(img2)
@test_reference "references/overlay_alpha/scalar.txt" ho

alpha[2, 2] = 1.1
@test_throws ArgumentError heatmap_overlay(A, img2; alpha=alpha)
@test_throws ArgumentError heatmap_overlay(A, img2; alpha=-0.1)
end

@testset "Batched input" begin
for reducer in reducers
for rangescale in rangescales
Expand Down

0 comments on commit b3fca8d

Please sign in to comment.