# Adding random noise to color images

**URL:** <https://discourse.julialang.org/t/adding-random-noise-to-color-images/7206>\
**Category:** Visualization\
**Created:** [November 21, 2017, 12:38am UTC](https://discourse.julialang.org/t/adding-random-noise-to-color-images/7206 "2017-11-21T00:38:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phil\_Tomson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phil_tomson/32/3575_2.png) [@Phil\_Tomson](https://discourse.julialang.org/u/Phil_Tomson)\
**Post date:** [November 21, 2017, 12:38am UTC](https://discourse.julialang.org/t/adding-random-noise-to-color-images/7206/1 "2017-11-21T00:38:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [November 21, 2017, 12:41am UTC](https://discourse.julialang.org/t/adding-random-noise-to-color-images/7206/2 "2017-11-21T00:41:52Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [November 21, 2017, 7:59am UTC](https://discourse.julialang.org/t/adding-random-noise-to-color-images/7206/3 "2017-11-21T07:59:48Z")

</div>

This is one way to do it manually:

```julia
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) 

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 22, 2017, 2:03pm UTC](https://discourse.julialang.org/t/adding-random-noise-to-color-images/7206/4 "2017-11-22T14:03:17Z")

</div>

Other alternatives:

```julia
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.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [November 22, 2017, 7:48pm UTC](https://discourse.julialang.org/t/adding-random-noise-to-color-images/7206/5 "2017-11-22T19:48:02Z")

</div>

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.
