2

我有一个函数,我从图像中获取 RGB 值并计算每个像素的平均值,我该如何解决这个问题?

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

     output =
    return Gray.(output)
end
4

1 回答 1

2
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防止溢出。

于 2021-10-30T08:19:46.210 回答