Hello everyone,
I just started using Julia and faced some difficulty understanding how to convert an Array into a Segmented Image. My array of Matrix{Float64} is an array consisting of labels from 1 to 9, where each value indicates a segment. I would like to use the attributes of SegmentedImage like segment_pixel_count, hence I would like to convert to a SegmentedImage.
I don’t think there is a more efficient way than manual construction (pixel counting).
It might be a good idea to make a feature request in ImageSegmentation.jl.
For example:
using Images, ImageSegmentation # Images#master
function from_indexmap(img::AbstractArray{<:Real})
image_indexmap = convert.(Int, img)
segment_pixel_count = Dict{Int, Int}()
for label in image_indexmap
count = get!(segment_pixel_count, label, 0)
@inbounds segment_pixel_count[label] = count + 1
end
segment_labels = sort!(collect(keys(segment_pixel_count)))
segment_means = Dict(map(x -> x => eltype(img)(x), segment_labels))
SegmentedImage(image_indexmap, segment_labels, segment_means, segment_pixel_count)
end
julia> indexmap = Float64[3 1 4 1 5; 9 2 6 5 3; 5 8 9 7 9]
3×5 Matrix{Float64}:
3.0 1.0 4.0 1.0 5.0
9.0 2.0 6.0 5.0 3.0
5.0 8.0 9.0 7.0 9.0
julia> segments = from_indexmap(indexmap)
Segmented Image with:
labels map: 3×5 Matrix{Int64}
number of labels: 9
julia> labels_map(segments)
3×5 Matrix{Int64}:
3 1 4 1 5
9 2 6 5 3
5 8 9 7 9
julia> segment_pixel_count(segments, 1)
2
julia> segment_mean(segments, 9)
9.0