-
Notifications
You must be signed in to change notification settings - Fork 2
Octree quantization #7
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
|
||
struct OctreeQuantization <: AbstractColorQuantizer | ||
numcolors::Int | ||
function OctreeQuantization(numcolors::Int=256; kwargs...) | ||
return new(numcolors) | ||
end | ||
end | ||
|
||
function (alg::OctreeQuantization)(img::AbstractArray) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tried running using ColorQuantization, TestImages
img = testimage("fabio")
img = RGB{N0f8}.(img)
alg = OctreeQuantization(64)
alg(img) however, the algorithm doesn't terminate after waiting several minutes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is fabio having high number of colors combined with how I prune the tree, lines using allleaves(root) is where trouble is
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's actually horrifyingly slow ;-; |
||
return octreequantization!(img; numcolors=alg.numcolors) | ||
end | ||
|
||
function octreequantization!(img; numcolors = 256, precheck::Bool = false) | ||
# ensure the img is in RGB colorspace | ||
if (eltype(img) != RGB{N0f8}) | ||
error("Octree Algorithm requires img to be in RGB colorspace") | ||
end | ||
Comment on lines
+15
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could surely but then we are quantising a different image entirely in a different colorspace, we def could avoid deepcopying if we are not gonna accept it if it not right type |
||
|
||
# checks if image has more colors than in numcolors | ||
if precheck == true | ||
unumcolors = length(unique(img)) | ||
# @show unumcolors | ||
if unumcolors <= numcolors | ||
@debug "Image has $unumcolors unique colors" | ||
return unique(img) | ||
end | ||
end | ||
|
||
# step 1: creating the octree | ||
root = Cell(SVector(0.0, 0.0, 0.0), SVector(1.0, 1.0, 1.0), [0, [], RGB{N0f8}.(0.0, 0.0, 0.0), 0]) | ||
cases = map(p -> [0, Vector{Int}([]), RGB{N0f8}.(0.0, 0.0, 0.0), 1], 1:8) | ||
split!(root, cases) | ||
inds = collect(1:length(img)) | ||
|
||
function putin(c::Cell, i::Int, level::Int) | ||
if (level == 8) | ||
push!(c.data[2], i) | ||
c.data[3] = img[i] | ||
return | ||
else | ||
if (isleaf(c) == true && level <= 8) | ||
cadata = map(p -> [0, Vector{Int}([]), RGB{N0f8}.(0.0, 0.0, 0.0), level + 1], 1:8) | ||
split!(c, cadata) | ||
end | ||
r, g, b = map(p -> bitstring(UInt8(p * 255)), channelview([img[i]])) | ||
rgb = r[level] * g[level] * b[level] | ||
rgb = parse(Int, rgb, base=2) + 1 | ||
c.children[rgb].data[1] += 1 | ||
putin(c.children[rgb], i, level + 1) | ||
end | ||
end | ||
|
||
level = 1 | ||
root.data[1] = length(inds) | ||
|
||
# build the tree | ||
for i in inds | ||
r, g, b = map(p -> bitstring(UInt8(p * 255)), channelview([img[i]])) | ||
rgb = r[level] * g[level] * b[level] | ||
rgb = parse(Int, rgb, base=2) + 1 | ||
root.children[rgb].data[1] += 1 | ||
putin(root.children[rgb], i, level) | ||
end | ||
|
||
# step 2: reducing tree to a certain number of colors | ||
# there is scope for improvements in allleaves as it's found again n again | ||
leafs = [p for p in allleaves(root)] | ||
filter!(p -> !iszero(p.data[1]), leafs) | ||
tobe_reduced = leafs[1] | ||
|
||
while (length(leafs) > numcolors) | ||
parents = unique([parent(p) for p in leafs]) | ||
parents = sort(parents; by = c -> c.data[1]) | ||
tobe_reduced = parents[1] | ||
|
||
for i = 1:8 | ||
append!(tobe_reduced.data[2], tobe_reduced.children[i].data[2]) | ||
tobe_reduced.data[3] += | ||
tobe_reduced.children[i].data[3] * tobe_reduced.children[i].data[1] | ||
end | ||
|
||
tobe_reduced.data[3] /= tobe_reduced.data[1] | ||
filter!(!in(tobe_reduced.children),leafs) | ||
push!(leafs, tobe_reduced) | ||
tobe_reduced.children = nothing | ||
end | ||
|
||
# step 3: palette formation and quantisation now | ||
da = [p.data for p in leafs] | ||
for i in da | ||
for j in i[2] | ||
img[j] = i[3] | ||
end | ||
end | ||
|
||
colors = [p[3] for p in da] | ||
return colors | ||
end | ||
|
||
function octreequantization(img; kwargs...) | ||
img_copy = deepcopy(img) | ||
palette = octreequantization!(img_copy; kwargs...) | ||
return img_copy, palette | ||
end | ||
|
Uh oh!
There was an error while loading. Please reload this page.