
function g = grayscale(f, R, parent)
    g = zeros(size(f));
    a = area(f, R, parent);
    
    [_, s] = sort(f(:), "descend");
    
    for x = s'
        y = x;
        
        while true
            g(y) = g(y) + f(x) / a(y);
            
            if y == parent(y)
                break;
            end
            
            y = parent(y);
        end
    end
end
