Algo je jednoduchý: Ak sú farby podobného rázu, len ich zmeň na rovnaké kontrastné farby:
https://hrubos.tech/blogy/content/images/20260627174701-hardlevel.jpeg

https://hrubos.tech/blogy/content/images/20260627174814-out_segmented.png
###############################################################
# Region Growing Color Segmentation
###############################################################
using Pkg
Pkg.add("Images")
Pkg.add("ImageIO")
Pkg.add("Colors")
Pkg.add("FixedPointNumbers")
using Images
using ImageIO
using Colors
###############################################################
# Nastavenia
###############################################################
INPUT = "hardlevel.jpeg"
OUTPUT = "out_segmented.png"
# Prah podobnosti farby
THRESHOLD = 0.12
# minimálna veľkosť oblasti
MIN_REGION = 20
###############################################################
# Načítanie obrázka
###############################################################
img = load(INPUT)
H,W = size(img)
###############################################################
# Pomocné funkcie
###############################################################
function colorDistance(c1,c2)
dr = red(c1)-red(c2)
dg = green(c1)-green(c2)
db = blue(c1)-blue(c2)
sqrt(dr*dr + dg*dg + db*db)
end
###############################################################
# Označenie regiónov
###############################################################
labels = zeros(Int,H,W)
region = 0
neighbors = (
(-1,-1),(-1,0),(-1,1),
(0,-1), (0,1),
(1,-1),(1,0),(1,1)
)
###############################################################
# BFS
###############################################################
for y in 1:H
for x in 1:W
if labels[y,x] != 0
continue
end
global region += 1
seed = RGB(img[y,x])
queue = [(y,x)]
labels[y,x]=region
pixels = [(y,x)]
while !isempty(queue)
cy,cx = popfirst!(queue)
for (dy,dx) in neighbors
ny = cy + dy
nx = cx + dx
if ny<1 || ny>H
continue
end
if nx<1 || nx>W
continue
end
if labels[ny,nx]!=0
continue
end
c = RGB(img[ny,nx])
if colorDistance(seed,c) < THRESHOLD
labels[ny,nx]=region
push!(queue,(ny,nx))
push!(pixels,(ny,nx))
end
end
end
#######################################################
# odstránenie veľmi malých regiónov
#######################################################
if length(pixels) < MIN_REGION
for (py,px) in pixels
labels[py,px]=0
end
region -= 1
end
end
end
println("Počet regiónov = ",region)
###############################################################
# Paleta výrazných farieb
###############################################################
palette = RGB[
RGB(1,0,0),
RGB(0,1,0),
RGB(0,0,1),
RGB(1,1,0),
RGB(1,0,1),
RGB(0,1,1),
RGB(1,0.5,0),
RGB(0.5,0,1),
RGB(0,0.5,1),
RGB(0,1,0.5),
RGB(0.5,1,0),
RGB(1,0,0.5),
RGB(0.3,0.8,1),
RGB(1,0.3,0.8),
RGB(0.8,1,0.3),
RGB(0.2,0.2,1),
RGB(1,0.2,0.2),
RGB(0.2,1,0.2)
]
###############################################################
# Výstupný obrázok
###############################################################
out = Array{RGB{Float64}}(undef,H,W)
for y in 1:H
for x in 1:W
id = labels[y,x]
if id==0
out[y,x]=RGB(0,0,0)
else
out[y,x]=palette[(id-1)%length(palette)+1]
end
end
end
save(OUTPUT,out)
println("Hotovo.")

Comments “Je toto len chyták 1453 alebo je za analýzou skryté opakujúce sa číslo 7?”