-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trouble with colors of segments #96
Comments
Hey Niccolo, Can you please provide a MWE? |
Yes, of course. This is my main code : link = joinpath(@__DIR__, "..", "input_example_demo", "slideExample1", "SlideExample_mini_1.tif")
# load slide
println("LOAD SLIDE ...")
svs_image = read(link)
img = ImageMagick.load_(svs_image)
bw = Gray.(img) .> 0.15
dist = 1 .- distance_transform(feature_transform(bw))
markers = label_components(dist .< -0.3)
# watershed
println("APPLY SEGMENTATION ...")
segments = watershed(dist, markers)
# build segmented slide
println("BUILD SEGMENTED SLIDE ...")
labels = labels_map(segments)
colored_labels = IndirectArray(labels, distinguishable_colors(maximum(labels)))
masked_colored_labels = colored_labels .* (1 .- bw)
# build graph
println("BUILD GRAPH ...")
weight_fn(i,j) = euclidean(segment_pixel_count(segments,i), segment_pixel_count(segments,j))
df_labels = DataFrame()
G, vert_map, df_labels = region_adjacency_graph(segments, weight_fn) I have modified the function function region_adjacency_graph(s::SegmentedImage, weight_fn::Function)
function neighbor_regions!(df_cartesian_indices::AbstractArray, G::SimpleWeightedGraph, visited::AbstractArray, s::SegmentedImage, I::CartesianIndex)
# n = Set{Int} - visited = Array - s = segmented image - p = CartesianIndex which define neighbors
# R contains each possible index in s
R = CartesianIndices(axes(s.image_indexmap))
# I1 contains a Vector of only 1 with dimension equal to visited
I1 = _oneunit(CartesianIndex{ndims(visited)})
# Ibegin and Iend contains the first and last index of R
Ibegin, Iend = first(R), last(R)
# t is only a empty Vector with dimension equal to visited
t = Vector{CartesianIndex{ndims(visited)}}()
# add index I to Vector t
push!(t, I)
while !isempty(t)
# Extract last element of t and save it in temp
temp = pop!(t)
# set index temp to true
visited[temp] = true
# _colon build an object CartesianIndices which include all the index from I to J (range) :
for J in _colon(max(Ibegin, temp-I1), min(Iend, temp+I1))
if s.image_indexmap[temp] != s.image_indexmap[J]
if !Graphs.has_edge(G, vert_map[s.image_indexmap[I]], vert_map[s.image_indexmap[J]])
Graphs.add_edge!(G, vert_map[s.image_indexmap[I]], vert_map[s.image_indexmap[J]], weight_fn(s.image_indexmap[I], s.image_indexmap[J]))
end
elseif !visited[J]
# If they are equal, I place them in t, so that,
# as long as t is not empty, I can explore all the neighbors
# that have the same color.
push!(t,J)
end
end
end
end
# Start
visited = fill(false, axes(s.image_indexmap)) # Array to mark the pixels that are already visited
G = SimpleWeightedGraph() # The region_adjacency_graph
vert_map = Dict{Int,Int}() # Map that stores (label, vertex) pairs
# Build object for label (vertex) dataframe
df_label = DataFrame()
df_cartesian_indices = []
df_color_indices = []
# add vertices to graph
Graphs.add_vertices!(G,length(s.segment_labels))
# setup `vert_map`
for (i,l) in enumerate(s.segment_labels)
vert_map[l] = i
end
# add edges to graph
# For each CartesianIndices in s where the image_indexmap represent the image wich is a Multidimensional Array
# The index of s.image_indexmap represent the pixel position in the segmented image
# The value of s.image_indexmap represent the pixel color in the segmented image
for p in CartesianIndices(axes(s.image_indexmap))
# check if p of the segmented image s is not visited
if !visited[p]
push!(df_cartesian_indices, p)
# n = Set{Int16}()
# Call neighbor_regions where :
# n = Set{Int} - visited = Array - s = segmented image - p = CartesianIndex which define neighbors
try
neighbor_regions!(df_cartesian_indices, G, visited, s, p)
catch oom
if isa(oom, OutOfMemoryError)
# n = Set{Int}()
GC.gc()
println(">>> OOM")
exit()
end
end
end
end
for i in s.segment_labels
push!(df_color_indices, s.segment_means[i])
end
df_label.label = s.segment_labels
df_label.position_label = df_cartesian_indices
df_label.color_label = df_color_indices
G, vert_map, df_label
end The main difference lies in creating a DataFrame containing label information (vertices). The DataFrame consists of three columns: the index |
Do you have any updates for me? |
Sorry for the delayed reply, your issue starts right at |
Yes, I am using |
Let me share an example:
|
Hi, after starting the segmentation process, I need to retrieve the colors assigned to the labels in the segmented image. From the code, it seems that the color intensity of the labels is given by the attribute
segments_mean
. However, this attribute returns data of type:Real
. Is there a way to convert the:Real
value to its corresponding RGB:Colorant
value ?Thanks in advance,
NM
The text was updated successfully, but these errors were encountered: