How to extract the RGB values from the image and calculate its output?

1.3k views Asked by At

I have a function where I have got the RGB-values from the image and calculate the mean value for each pixel, how can I approach this problem?

function RGB2G_mean(img::Matrix{RGB{T}}) where T<:Fractional
    # get RGB-values from img
     # R,G,B=...

     output =
    return Gray.(output)
end
1

There are 1 answers

0
tholy On
julia> using Colors, ColorVectorSpace, FixedPointNumbers, Statistics

julia> a = rand(RGB{N0f8}, 4)
4-element Array{RGB{N0f8},1} with eltype RGB{N0f8}:
 RGB{N0f8}(0.588,0.459,0.529)
 RGB{N0f8}(0.592,0.247,0.408)
 RGB{N0f8}(0.31,0.18,0.396)
 RGB{N0f8}(0.235,1.0,0.675)

julia> mean(a)
RGB{Float64}(0.43137254901960786,0.4715686274509804,0.5019607843137255)

To address a concern of BatWannaBe in the comments, yes, mean guards against overflow.