From 97bd915d3be6a8fa384c0acd70fb12cf4248374b Mon Sep 17 00:00:00 2001 From: Felix Horger Date: Thu, 24 Mar 2022 15:34:09 +0000 Subject: [PATCH 1/6] Value range determined from whole volume, not first slice --- src/ImageView.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ImageView.jl b/src/ImageView.jl index 226d888..30b20e5 100644 --- a/src/ImageView.jl +++ b/src/ImageView.jl @@ -183,7 +183,7 @@ function imshow(img::AbstractArray; imgmapped, kwargs = kwhandler(_mappedarray(scalei, img), axes; kwargs...) zr, sd = roi(imgmapped, axes) v = slice2d(imgmapped, value(zr), sd) - imshow(imgmapped, default_clim(v), zr, sd; name=name, aspect=aspect, kwargs...) + imshow(imgmapped, default_clim(img), zr, sd; name=name, aspect=aspect, kwargs...) end imshow(img::AbstractVector; kwargs...) = (@nospecialize; imshow(reshape(img, :, 1); kwargs...)) @@ -482,7 +482,7 @@ function hoverinfo(lbl, btn, img, sd::SliceData{transpose}) where transpose end end -function valuespan(img::AbstractMatrix) +function valuespan(img::AbstractArray) minval = minimum_finite(img) maxval = maximum_finite(img) if minval > maxval @@ -499,7 +499,7 @@ default_clim(img::AbstractArray{C}) where {C<:GrayLike} = _default_clim(img, elt default_clim(img::AbstractArray{C}) where {C<:AbstractRGB} = _default_clim(img, eltype(C)) _default_clim(img, ::Type{Bool}) = nothing _default_clim(img, ::Type{T}) where {T} = _deflt_clim(img) -function _deflt_clim(img::AbstractMatrix) +function _deflt_clim(img::AbstractArray) minval, maxval = valuespan(img) Signal(CLim(saferound(gray(minval)), saferound(gray(maxval))); name="CLim") end From d28e0bf5ae1982495f7012d683545c27168b2d9c Mon Sep 17 00:00:00 2001 From: Felix Horger Date: Wed, 6 Jul 2022 23:33:23 +0100 Subject: [PATCH 2/6] Added function fast_finite_extrema() for computing the value span more efficiently. Allowed RGB arrays to be multi-dimensional instead of matrix only. --- src/ImageView.jl | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/ImageView.jl b/src/ImageView.jl index 30b20e5..b081440 100644 --- a/src/ImageView.jl +++ b/src/ImageView.jl @@ -482,13 +482,23 @@ function hoverinfo(lbl, btn, img, sd::SliceData{transpose}) where transpose end end +function fast_finite_extrema(a::AbstractArray) + mini = a[1] + maxi = a[1] + @simd for v in a + if isfinite(v) + if v <= mini + mini = v + elseif v > maxi + maxi = v + end + end + end + return mini, maxi +end function valuespan(img::AbstractArray) - minval = minimum_finite(img) - maxval = maximum_finite(img) - if minval > maxval - minval = zero(typeof(minval)) - maxval = oneunit(typeof(maxval)) - elseif minval == maxval + minval, maxval = fast_finite_extrema(img) + if minval == maxval maxval = minval+1 end return minval, maxval @@ -503,8 +513,7 @@ function _deflt_clim(img::AbstractArray) minval, maxval = valuespan(img) Signal(CLim(saferound(gray(minval)), saferound(gray(maxval))); name="CLim") end - -function _deflt_clim(img::AbstractMatrix{T}) where {T<:AbstractRGB} +function _deflt_clim(img::AbstractArray{T}) where {T<:AbstractRGB} minval = RGB(0.0,0.0,0.0) maxval = RGB(1.0,1.0,1.0) Signal(CLim(minval, maxval); name="CLim") From 29bb0f008001508c7d314053d891f392f13d1f50 Mon Sep 17 00:00:00 2001 From: Felix Horger Date: Mon, 9 Jan 2023 12:50:45 +0000 Subject: [PATCH 3/6] Removed roi since name clash --- src/ImageView.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageView.jl b/src/ImageView.jl index b081440..57c62db 100644 --- a/src/ImageView.jl +++ b/src/ImageView.jl @@ -13,7 +13,7 @@ import ImageCore: scaleminmax export AnnotationText, AnnotationPoint, AnnotationPoints, AnnotationLine, AnnotationLines, AnnotationBox export CLim, annotate!, annotations, canvasgrid, imshow, imshow!, imshow_gui, imlink, - roi, scalebar, slice2d + scalebar, slice2d const AbstractGray{T} = Color{T,1} const GrayLike = Union{AbstractGray,Number} From b0bcf02fdd1266351a072e3d55214118cc867709 Mon Sep 17 00:00:00 2001 From: Felix Horger Date: Sat, 11 Feb 2023 23:53:15 +0000 Subject: [PATCH 4/6] Fixed infinite/NaN value issue in value span determination. Added limit of values to be checked. --- src/ImageView.jl | 49 +++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/src/ImageView.jl b/src/ImageView.jl index 37cdf8f..d647dc6 100644 --- a/src/ImageView.jl +++ b/src/ImageView.jl @@ -501,24 +501,39 @@ function hoverinfo(lbl, btn, img, sd::SliceData{transpose}) where transpose end end -function fast_finite_extrema(a::AbstractArray) - mini = a[1] - maxi = a[1] - @simd for v in a - if isfinite(v) - if v <= mini - mini = v - elseif v > maxi - maxi = v - end - end - end - return mini, maxi -end -function valuespan(img::AbstractArray) +function fast_finite_extrema(a::AbstractArray{T}) where T + mini = typemax(T) + maxi = typemin(T) + @simd for v in a + if isfinite(v) + if v <= mini + mini = v + end + if v > maxi + maxi = v + # Needs to have a separate if-block, + # for the case that all values in a are equal + end + end + end + return mini, maxi +end +function valuespan(img::AbstractArray; checkmax=10^8) + if length(img) > checkmax + img = randsubseq(img, checkmax / length(img)) + end minval, maxval = fast_finite_extrema(img) - if minval == maxval - maxval = minval+1 + invalid_min, invalid_max = (!isfinite).((minval, maxval)) + (invalid_min || invalid_max) && @warn "Could not determine valid value span" + if invalid_min && invalid_max + minval = 0 + maxval = 1 + elseif invalid_min + minval = maxval - 1 + elseif invalid_max + maxval = minval + 1 + elseif minval == maxval + maxval = minval + 1 end return minval, maxval end From ed64583656c296e267f20c27d0ffa26b2850a4f0 Mon Sep 17 00:00:00 2001 From: Felix Horger Date: Sat, 11 Feb 2023 23:55:20 +0000 Subject: [PATCH 5/6] Added 'roi' to imports in test/core.jl, it was removed from ImageView.jl exports in previous commits --- test/core.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/core.jl b/test/core.jl index 2002f74..07b116f 100644 --- a/test/core.jl +++ b/test/core.jl @@ -1,5 +1,5 @@ using GtkObservables, ImageCore, ImageView -using ImageView: sliceinds +using ImageView: sliceinds, roi using Test using AxisArrays: AxisArrays, Axis, AxisArray From 405c33d859f525f054e2984383928de779d0cd05 Mon Sep 17 00:00:00 2001 From: Felix Horger Date: Sun, 12 Feb 2023 19:18:25 +0000 Subject: [PATCH 6/6] Added Random.jl to provide randsubseq() for value span determination --- Project.toml | 1 + src/ImageView.jl | 1 + 2 files changed, 2 insertions(+) diff --git a/Project.toml b/Project.toml index 44423ba..fa38811 100644 --- a/Project.toml +++ b/Project.toml @@ -14,6 +14,7 @@ ImageBase = "c817782e-172a-44cc-b673-b171935fbb9e" ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534" ImageMetadata = "bc367c6b-8a6b-528e-b4bd-a4b897500b49" MultiChannelColors = "d4071afc-4203-49ee-90bc-13ebeb18d604" +Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" RoundingIntegers = "d5f540fe-1c90-5db3-b776-2e2f362d9394" SnoopPrecompile = "66db9d55-30c0-4569-8b51-7e840670fc0c" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" diff --git a/src/ImageView.jl b/src/ImageView.jl index d647dc6..a0d8dc9 100644 --- a/src/ImageView.jl +++ b/src/ImageView.jl @@ -16,6 +16,7 @@ using GtkObservables.Observables using AxisArrays: AxisArrays, Axis, AxisArray, axisnames, axisvalues using ImageMetadata using Compat # for @constprop :none +using Random export AnnotationText, AnnotationPoint, AnnotationPoints, AnnotationLine, AnnotationLines, AnnotationBox