-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimage.jl
225 lines (193 loc) · 8.8 KB
/
image.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
In order to send images to Python, we have to convert them to a primtive
array. We overload view_image here for specific types which would be difficult
to display in Napari otherwise
"""
module Image
# The main reason to have this in a discrete module is really to contain the imports
# until we can be more explicit about what we are importing
#
# In lieu of PR 876 in PyCall, we use NumPyArrays, to perform no-copy conversions into Python friendly arrays
# https://github.com/JuliaPy/PyCall.jl/pull/876
using Images, FixedPointNumbers, PyCall, AxisArrays, ImageMetadata, NumPyArrays
import Napari: napari, view_image, add_image
# This module
view_image(A::AbstractArray{C}, args...; kwargs...) where C <: Colorant{T,3} where T <: FixedPoint{F} where F =
view_image(primitive_array(A), args...; kwargs...)
add_image(viewer, A::AbstractArray{C}, args...; kwargs...) where C <: Colorant{T,3} where T <: FixedPoint{F} where F =
add_image(viewer, primitive_array(A), args...; kwargs...)
function primitive_array(A::AbstractArray{C}) where C <: Colorant{T,3} where T <: FixedPoint{F} where F
# 1. Grab channel view which gets us Array{T,3}. This is a Base.ReinterpretArray
# 2. Convert to Array{F,3}, still a Base.ReinterpretArray
# 3. Permute dims such that the channels are the last dimension, without copying
PermutedDimsArray( reinterpret(F,channelview(A)), (2,3,1))
end
view_image(A::AbstractArray{C}, args...; kwargs...) where C <: Colorant{T,4} where T <: FixedPoint{F} where F =
view_image(primitive_array(A), args...; kwargs...)
add_image(viewer, A::AbstractArray{C}, args...; kwargs...) where C <: Colorant{T,4} where T <: FixedPoint{F} where F =
add_image(viewer, primitive_array(A), args...; kwargs...)
function primitive_array(A::AbstractArray{C}) where C <: Colorant{T,4} where T <: FixedPoint{F} where F
# 1. Grab channel view which gets us Array{T,3}. This is a Base.ReinterpretArray
# 2. Convert to Array{F,3}, still a Base.ReinterpretArray
# 3. Permute dims such that the channels are the last dimension, without copying
PermutedDimsArray( reinterpret(F,channelview(A)), (2,3,1))
end
view_image(A::AbstractArray{C}, args...; kwargs...) where C <: Colorant{T,1} where T =
view_image(primitive_array(A), args...; kwargs...)
add_image(viewer, A::AbstractArray{C}, args...; kwargs...) where C <: Colorant{T,1} where T =
add_image(viewer, primitive_array(A), args...; kwargs...)
function primitive_array(A::AbstractArray{C}) where C <: Colorant{T,1} where T <: FixedPoint{F} where F
# 1. Grab channel view which gets us Array{T,3}. This is a Base.ReinterpretArray
# 2. Convert to Array{F,3}, still a Base.ReinterpretArray
reinterpret(F,channelview(A))
end
function primitive_array(A::AbstractArray{C}) where C <: Colorant{F, 1} where F <: AbstractFloat
reinterpret(F,channelview(A))
end
view_image(A::AbstractArray{C}, args...; kwargs...) where C <: TransparentColor{Gray{F}, F} where F =
view_image(primitive_array(A), args...; kwargs...)
add_image(A::AbstractArray{C}, args...; kwargs...) where C <: TransparentColor{Gray{F}, F} where F =
add_image(primitive_array(A), args...; kwargs...)
function primitive_array(A::AbstractArray{C}) where C <: TransparentColor{Gray{F}, F} where F
# Napari does not know how to combine alpha channel and gray
primitive_array( RGBA.(A) )
end
"""
view_image(img::ImageMeta, ...)
This function adds the following keywords if not provided:
channel_axis = axis with name :channel
order = gives axes in order of :time, :channel, :y, :x
axis_labels = axisnames(img)
metadata = properties(img)
"""
function view_image(img::ImageMeta, args...; kwargs...)
# ImageMeta might be from OMETIFF
# For example, testimage("multi-channel-time-series.ome.tif")
kwdict = Dict{Symbol,Any}(kwargs)
if !haskey(kwdict, :metadata)
kwdict[:metadata] = properties(img)
end
view_image( arraydata(img) , args... ; kwdict...)
end
"""
add_image(viewer, img::ImageMeta, ...)
This function adds the following keywords if not provided:
channel_axis = axis with name :channel
metadata = properties(img)
Unlike view_image(img::ImageMeta, ...) it does change the
order or axis_labels
"""
function add_image(viewer, img::ImageMeta, args...; kwargs...)
# ImageMeta might be from OMETIFF
# For example, testimage("multi-channel-time-series.ome.tif")
kwdict = Dict{Symbol,Any}(kwargs)
if !haskey(kwdict, :metadata)
kwdict[:metadata] = properties(img)
end
add_image(viewer, arraydata(img) , args... ; kwdict...)
end
"""
view_image(img::AxisArray{Gray{T}}, ...)
This function adds the following keywords if not provided:
channel_axis = axis with name :channel
order = gives axes in order of :time, :channel, :y, :x
axis_labels = axisnames(img)
"""
function view_image(img::AxisArray, args...; kwargs...)
A = primitive_array( img )
dims = collect( 1:ndims( img ) )
dim_names = axisnames(img)
time_dim = findfirst(in((:time, :T)), dim_names)
channel_dim = findfirst(in((:channel, :C)), dim_names)
kwdict = Dict{Symbol,Any}(kwargs)
# If we have an image with (:y, :x, :channel, :time)
# transform it to (:time, :channel, :y, :x)
if !isnothing(time_dim)
popat!(dims, time_dim)
if !isnothing(channel_dim)
popat!(dims, channel_dim)
pushfirst!(dims, channel_dim)
if !haskey(kwdict, :channel_axis)
kwdict[:channel_axis] = channel_dim - 1
end
end
pushfirst!(dims, time_dim)
if !haskey(kwdict, :order)
kwdict[:order] = dims .- 1
end
else
# No time axis found
if !isnothing(channel_dim)
popat!(dims, channel_dim)
pushfirst!(dims, channel_dim)
if !haskey(kwdict, :channel_axis)
kwdict[:channel_axis] = channel_dim - 1
end
end
if !haskey(kwdict, :order)
order = dims .- 1
end
end
if !haskey(kwdict, :axis_labels)
kwdict[:axis_labels] = String.(dim_names)
end
@info kwdict
view_image(A, args... ; kwdict...)
end
"""
add_image(viewer, img::AxisArrays{Gray{T},...})
This function adds the channel_axis keyword if not provided.
Unlike view_image(viewer, img::AxisArrays{Gray{T},...}) it does
not change the order or add axis_labels.
"""
function add_image(viewer, img::AxisArray, args...; kwargs...)
A = primitive_array( img )
dims = collect( 1:ndims( img ) )
dim_names = axisnames(img)
time_dim = findfirst(in((:time, :T)), dim_names)
channel_dim = findfirst(in((:channel, :C)), dim_names)
kwdict = Dict{Symbol,Any}(kwargs)
# If we have an image with (:y, :x, :channel, :time)
# transform it to (:time, :channel, :y, :x)
if !isnothing(time_dim)
popat!(dims, time_dim)
if !isnothing(channel_dim)
popat!(dims, channel_dim)
pushfirst!(dims, channel_dim)
if !haskey(kwdict, :channel_axis)
# We are going to permute the array
kwdict[:channel_axis] = 1
end
end
pushfirst!(dims, time_dim)
# We permute the array since we are adding to an existing viewer
@info "Permuting Dims"
A = PermutedDimsArray(A, dims)
else
# No time axis found
if !isnothing(channel_dim)
popat!(dims, channel_dim)
pushfirst!(dims, channel_dim)
if !haskey(kwdict, :channel_axis)
kwdict[:channel_axis] = channel_dim - 1
end
end
end
add_image(viewer, A, args... ; kwdict...)
end
function primitive_array(A::AxisArray{Gray{T},N}) where T <: FixedPoint{F} where {F,N}
reinterpret(F, arraydata(A))
end
function primitive_array(A::AxisArray{T}) where T
reinterpret(T, arraydata(A))
end
# Use NumPyArray for types with strides and a parent
view_image(img::PermutedDimsArray, args...; kwargs...) = view_image(NumPyArray(img), args...; kwargs...)
add_image(viewer, img::PermutedDimsArray, args...; kwargs...) = add_image(viewer, NumPyArray(img), args...; kwargs...)
view_image(img::SubArray, args...; kwargs...) = view_image(NumPyArray(img), args...; kwargs...)
add_image(viewer, img::SubArray, args...; kwargs...) = add_image(viewer, NumPyArray(img), args...; kwargs...)
view_image(img::Base.ReinterpretArray, args...; kwargs...) = view_image(NumPyArray(img), args...; kwargs...)
add_image(viewer, img::Base.ReinterpretArray, args...; kwargs...) = add_image(viewer, NumPyArray(img), args...; kwargs...)
view_image(img::Base.ReshapedArray, args...; kwargs...) = view_image(NumPyArray(img), args...; kwargs...)
add_image(viewer, img::Base.ReshapedArray, args...; kwargs...) = add_image(viewer, NumPyArray(img), args...; kwargs...)
end # PyConvert module end