Adding random noise to color images

What would be the best way to go about adding random noise to a color image using Julia?
Does Images.jl have a way to do this?

You can extract the values from an image with channelview, add the noise, and then recreate the image with colorview. Both of these functions are in Images.jl (or one of the associated packages).


This is one way to do it manually:

julia> using Images

julia> img = rand(RGB{N0f8}, 4, 4)
4×4 Array{RGB{N0f8},2}:
 RGB{N0f8}(0.957,0.89,0.612)   RGB{N0f8}(0.882,0.925,0.271)  RGB{N0f8}(0.059,0.51,0.263)   RGB{N0f8}(0.792,0.004,0.502)
 RGB{N0f8}(0.529,0.004,0.329)  RGB{N0f8}(0.204,0.365,0.427)  RGB{N0f8}(0.616,0.043,0.282)  RGB{N0f8}(0.047,0.89,0.584) 
 RGB{N0f8}(0.62,0.973,0.533)   RGB{N0f8}(0.106,0.671,0.22)   RGB{N0f8}(0.153,0.373,0.063)  RGB{N0f8}(0.231,0.059,0.129)
 RGB{N0f8}(0.639,0.529,0.42)   RGB{N0f8}(0.965,0.008,0.886)  RGB{N0f8}(0.647,0.518,0.051)  RGB{N0f8}(0.847,0.086,0.784)

julia> A = channelview(img);

julia> A = eltype(A).(clamp.(A .+ randn.() .* 0.01, 0, 1));

julia> img2 = colorview(RGB, A)
4×4 Array{RGB{N0f8},2}:
 RGB{N0f8}(0.961,0.902,0.612)  RGB{N0f8}(0.875,0.91,0.275)   RGB{N0f8}(0.071,0.502,0.263)  RGB{N0f8}(0.792,0.016,0.502)
 RGB{N0f8}(0.537,0.004,0.345)  RGB{N0f8}(0.208,0.365,0.439)  RGB{N0f8}(0.635,0.027,0.275)  RGB{N0f8}(0.071,0.902,0.596)
 RGB{N0f8}(0.624,0.973,0.549)  RGB{N0f8}(0.114,0.659,0.208)  RGB{N0f8}(0.141,0.376,0.051)  RGB{N0f8}(0.231,0.075,0.137)
 RGB{N0f8}(0.655,0.533,0.396)  RGB{N0f8}(0.969,0.0,0.871)    RGB{N0f8}(0.651,0.506,0.067)  RGB{N0f8}(0.843,0.106,0.78) 

Other alternatives:

img + 0.1*rand(eltype(img), size(img)) # if you don't need it to be zero-centered
img + 0.1*(rand(RGB{Float64}, size(img)) - RGB(0.5,0.5,0.5))

and related options. If it would be more convenient, someone could write a randn(RGB{T}, sz) method and add it to ColorTypes.


Or rather take it all the way and connect it to Distributions.jl, this way you’d be able to create any type of noise you like, not just Gaussian.