get("#348") -- rgb functions get("#388") -- rect functions get("#410") -- shrinkRectangle get("#380") -- hashImage w, h = img.width, img.height g = 4 -- grid size gw, gh = math.floor(w/g), math.floor(h/g) -- width & height of grid grid = {} for gy = 0, h-g, g do for gx = 0, w-g, g do local min, max = 1, 0 for y = gy, gy+g-1 do for x = gx, gx+g-1 do local b = bright(rgb(img.getInt(x, y))) min, max = math.min(min, b), math.max(max, b) if min < 0.5 and max > 0.5 then grid[(gy/g)*gw+(gx/g)+1] = true goto next end end end ::next:: end end -- stack layout: {{x, y}, ...} local r function fill(stack) while #stack > 0 do local x, y = unpack(stack[#stack]) stack[#stack] = nil if not (x < 0 or y < 0 or x >= gw or y >= gh) then local idx = y*gw+x+1 if grid[idx] then grid[idx] = nil local me = newRectangle(x, y, 1, 1) if r == nil then r = me else r = mergeRectangles(r, me) end stack[#stack+1] = {x-1, y} stack[#stack+1] = {x+1, y} stack[#stack+1] = {x, y-1} stack[#stack+1] = {x, y+1} end end end end result = {} for y = 0, gh-1 do for x = 0, gw-1 do r = nil fill {{x, y, 1}} if r then --table.insert(result, (x*g).."/"..(y*g).."-"..c) r = scaleRectangle(r, g) r = shrinkRectangle(r, img) if r.width < 200 then local cropped = {width=r.width, height=r.height, getInt = function(x, y) return img.getInt(x+r.x, y+r.y) end} local hash = hashImage(cropped) table.insert(result, recttostring(r).." -> "..hash) end end end end return "Floodfill: "..table.concat(result, "|")