我有一个函数,我从图像中获取 RGB 值并计算每个像素的平均值,我该如何解决这个问题?
function RGB2G_mean(img::Matrix{RGB{T}}) where T<:Fractional
# get RGB-values from img
# R,G,B=...
output =
return Gray.(output)
end
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)
为了解决评论中对 BatWannaBe 的担忧,是的,mean
防止溢出。