Skip to content

Commit 7cf2612

Browse files
committed
Move ImageView into package
This is in preparation for the GUI-centric workflow.
1 parent 5f91dfb commit 7cf2612

File tree

7 files changed

+78
-92
lines changed

7 files changed

+78
-92
lines changed

Project.toml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,10 @@ ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
1010
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
1111
ImageMorphology = "787d08f9-d448-5407-9aad-5290dd7ab264"
1212
ImageSegmentation = "80713f31-8817-5129-9cf8-209ff8fb23e1"
13+
ImageView = "86fae568-95e7-573e-a6b2-d8a6b900c9ef"
1314
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1415
XLSX = "fdbf4ff8-1666-58a4-91e7-1b58723a45e0"
1516

16-
[weakdeps]
17-
ImageView = "86fae568-95e7-573e-a6b2-d8a6b900c9ef"
18-
19-
[extensions]
20-
CounterMarkingImageViewExt = "ImageView"
21-
2217
[compat]
2318
FileIO = "1"
2419
Glob = "1"
@@ -31,8 +26,7 @@ XLSX = "0.10"
3126
julia = "1.10"
3227

3328
[extras]
34-
ImageView = "86fae568-95e7-573e-a6b2-d8a6b900c9ef"
3529
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3630

3731
[targets]
38-
test = ["ImageView", "Test"]
32+
test = ["Test"]

ext/CounterMarkingImageViewExt.jl

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/CounterMarking.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ using ImageMorphology: label_components
66
using FileIO
77
using XLSX
88
using Glob
9+
using ImageView
10+
using Random
911

1012
export segment_image, stimulus_index, spots, Spot, upperleft
1113
export writexlsx, process_images
1214
export randshow, meanshow
1315

1416
include("segment.jl")
15-
include("stubs.jl")
1617
include("xlxs.jl")
18+
include("gui.jl")
1719

1820
end

src/gui.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
3+
4+
function linkpair(img, imgc)
5+
zr, slicedata = roi(img)
6+
gd = imshow_gui((800, 800), (2,1); slicedata=slicedata)
7+
imshow(gd["frame"][1,1], gd["canvas"][1,1], img, nothing, zr, slicedata)
8+
imshow(gd["frame"][2,1], gd["canvas"][2,1], imgc, nothing, zr, slicedata)
9+
return gd
10+
end
11+
12+
# For visualization
13+
function get_random_color(seed)
14+
Random.seed!(seed)
15+
rand(RGB{N0f8})
16+
end
17+
18+
"""
19+
randshow(seg; kwargs...)
20+
randshow(img, seg; kwargs...)
21+
22+
Display a segmented image using random colors for each segment. The version with
23+
`img` displays the original image and the segmented image one atop the other,
24+
and zooming on one will zoom on the other.
25+
26+
!!! note You must load the `ImageView` package to use this function.
27+
"""
28+
function randshow end
29+
30+
"""
31+
meanshow(seg; kwargs...)
32+
meanshow(img, seg; kwargs...)
33+
34+
Display a segmented image using the mean color of each segment. The version with
35+
`img` displays the original image and the segmented image one atop the other,
36+
and zooming on one will zoom on the other.
37+
38+
!!! note
39+
You must load the `ImageView` package to use this function.
40+
"""
41+
function meanshow end
42+
43+
44+
randshow(seg; kwargs...) = imshow(map(i->get_random_color(i), labels_map(seg)); kwargs...)
45+
meanshow(seg; kwargs...) = imshow(map(i->segment_mean(seg, i), labels_map(seg)); kwargs...)
46+
47+
randshow(img, seg; kwargs...) = linkpair(img, map(i->get_random_color(i), labels_map(seg)); kwargs...)
48+
meanshow(img, seg; kwargs...) = linkpair(img, map(i->segment_mean(seg, i), labels_map(seg)); kwargs...)

src/segment.jl

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ function segment_image(
1313
min_size::Int = 50, # minimum size of segments to keep
1414
)
1515
seg = unseeded_region_growing(img, threshold)
16-
L = label_components(labels_map(seg)) # insist on contiguous regions
17-
seg = SegmentedImage(img, L)
1816
if prune
1917
println("Pruning segments smaller than $min_size pixels")
2018
seg = prune_segments(seg, label -> segment_pixel_count(seg, label) < min_size, (l1, l2) -> colordiff(segment_mean(seg, l1), segment_mean(seg, l2)))
@@ -36,6 +34,19 @@ function stimulus_index(seg::SegmentedImage, colorproj = RGB{Float32}(1, 1, -2))
3634
return i
3735
end
3836

37+
function contiguous(seg::SegmentedImage, img::AbstractMatrix{<:Color}; min_size::Int = 50)
38+
L = label_components(labels_map(seg)) # insist on contiguous regions
39+
newseg = SegmentedImage(img, L)
40+
newseg = prune_segments(newseg, label -> segment_pixel_count(newseg, label) < min_size, (l1, l2) -> colordiff(segment_mean(newseg, l1), segment_mean(newseg, l2)))
41+
mapping = Dict(k => Set{Int}() for k in segment_labels(seg))
42+
for (i, l) in pairs(seg.image_indexmap)
43+
push!(mapping[l], newseg.image_indexmap[i])
44+
end
45+
return mapping
46+
end
47+
contiguous(seg::SegmentedImage, img::AbstractMatrix{<:Colorant}; kwargs...) =
48+
contiguous(seg, color.(img); kwargs...)
49+
3950
struct Spot
4051
npixels::Int
4152
centroid::Tuple{Int, Int}
@@ -54,7 +65,7 @@ stimulus segment and the second element is the `Spot` object for that segment.
5465
Spots larger than `max_size_frac * npixels` (default: 10% of the image) are ignored.
5566
"""
5667
function spots(
57-
seg;
68+
seg::SegmentedImage;
5869
max_size_frac=0.1, # no spot is bigger than max_size_frac * npixels
5970
)
6071
keypair(i, j) = i < j ? (i, j) : (j, i)
@@ -128,3 +139,12 @@ function upperleft(spotdict::AbstractDict{Int, Spot}, stimulus, imgsize)
128139
end
129140
return Dict(k => flip(v) for (k, v) in spotdict), sidx => flip(ss)
130141
end
142+
143+
function colorize(seg::SegmentedImage, coloridx::AbstractDict, colors=distinguishable_colors(length(unique(values(coloridx)))))
144+
label = seg.image_indexmap
145+
img = similar(label, eltype(colors))
146+
for idx in eachindex(label)
147+
img[idx] = colors[coloridx[label[idx]]]
148+
end
149+
return img
150+
end

src/stubs.jl

Lines changed: 0 additions & 34 deletions
This file was deleted.

test/runtests.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
using CounterMarking
22
using FileIO
33
using XLSX
4+
using ImageView
5+
using Glob
46
using Test
57

68
@testset "CounterMarking.jl" begin
79
testdir = joinpath(@__DIR__, "..", "docs", "src", "assets")
810
img = load(joinpath(testdir, "Picture.png"))
911
seg = segment_image(img)
10-
# Without ImageView loaded, we can't visualize it, but we get a helpful error
11-
if !isdefined(@__MODULE__, :ImageView)
12-
@test_throws "using ImageView" randshow(seg)
13-
@test_throws "using ImageView" meanshow(seg)
14-
end
15-
@eval using ImageView
1612
dct = meanshow(seg)
1713
@test haskey(dct, "gui")
1814
dct = randshow(seg)

0 commit comments

Comments
 (0)