# Access each element in the broadcast operation

**URL:** https://discourse.julialang.org/t/access-each-element-in-the-broadcast-operation/108728
**Category:** New to Julia
**Tags:** images, broadcast
**Created:** [January 12, 2024, 2:20pm UTC](https://discourse.julialang.org/t/access-each-element-in-the-broadcast-operation/108728 "2024-01-12T14:20:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Afonso\_Britto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/afonso_britto/32/205972_2.png) [@Afonso\_Britto](https://discourse.julialang.org/u/Afonso_Britto)
#### Post date: [January 12, 2024, 2:20pm UTC](https://discourse.julialang.org/t/access-each-element-in-the-broadcast-operation/108728/1 "2024-01-12T14:20:45Z")

</div>

Is there anyway I can acess the current element I’m iterating through when I use a broadcast operation? I would want to make this code in just one line, so, for every pixel I’m iterating in my image I would want to perform the operation the function is doing, but without having to call it. Like the for loop i did.

> function bright(pixel)
> 
> pixel = RGB(clamp(pixel.r \* 2, 0, 1), clamp(pixel.g \* 2, 0, 1), clamp(pixel.b \* 2, 0, 1))
> 
> return pixel  
> end

> bright.(img)

> begin  
> h = size(img, 1)  
> w = size(img, 2)
> 
> for i in 1:h  
> for j in 1:w  
> img1[i, j] = RGB(clamp(img1[i,j].r \* 2, 0, 1), clamp(img1[i,j].g \* 2, 0, 1), clamp(img1[i,j].b \* 2, 0, 1))  
> end  
> end
> 
> img1  
> end

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [January 12, 2024, 2:26pm UTC](https://discourse.julialang.org/t/access-each-element-in-the-broadcast-operation/108728/2 "2024-01-12T14:26:45Z")

</div>

Yes for example `pixel.r` is just a simpler way of writing `getproperty(pixel, :r)` (that’s what we call syntax sugar). So to broadcast this access to the `r` field you can write `getproperty.(pixel, :r)`.

(PS: see [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) for tips on how to format your code on this forum.)

---

<div class="post-metadata">

### Author: ![Afonso\_Britto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/afonso_britto/32/205972_2.png) [@Afonso\_Britto](https://discourse.julialang.org/u/Afonso_Britto)
#### Post date: [January 13, 2024, 10:51pm UTC](https://discourse.julialang.org/t/access-each-element-in-the-broadcast-operation/108728/3 "2024-01-13T22:51:08Z")

</div>

I don’t get how that applies to my code, sorry. What I would like to is acess the pixel [i,j] over the broadcasting, something like:

```julia
img .= RGB(clamp(img1[i,j].r * 2, 0, 1), clamp(img1[i,j].g * 2, 0, 1), clamp(img1[i,j].b * 2, 0, 1))

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 13, 2024, 10:57pm UTC](https://discourse.julialang.org/t/access-each-element-in-the-broadcast-operation/108728/4 "2024-01-13T22:57:50Z")

</div>

> [@Afonso\_Britto](#):
>
> `img.r`

Replace this with the `getproperty` call.

But to be honest, the option with the function definition and the `bright.(img)` call is so much cleaner…

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [January 14, 2024, 1:13am UTC](https://discourse.julialang.org/t/access-each-element-in-the-broadcast-operation/108728/5 "2024-01-14T01:13:54Z")

</div>

Perhaps this will help.

The `r`, `g`, and `b` fields have accessor functions called `red`, `green`, and `blue`.

```julia-repl
julia> using Colors

julia> img = rand(RGB, 2, 2)
2×2 Array{RGB{Float64},2} with eltype RGB{Float64}:
 RGB{Float64}(0.873815,0.178169,0.869838) … RGB{Float64}(0.198527,0.556571,0.661415)
 RGB{Float64}(0.380691,0.850955,0.472295) RGB{Float64}(0.844428,0.151697,0.343053)

julia> getproperty.(img, :r)
2×2 Matrix{Float64}:
 0.873815 0.198527
 0.380691 0.844428

julia> getproperty.(img, :g)
2×2 Matrix{Float64}:
 0.178169 0.556571
 0.850955 0.151697

julia> getproperty.(img, :b)
2×2 Matrix{Float64}:
 0.869838 0.661415
 0.472295 0.343053

julia> red.(img)
2×2 Matrix{Float64}:
 0.873815 0.198527
 0.380691 0.844428

julia> green.(img)
2×2 Matrix{Float64}:
 0.178169 0.556571
 0.850955 0.151697

julia> blue.(img)
2×2 Matrix{Float64}:
 0.869838 0.661415
 0.472295 0.343053

julia> x->clamp(x*2,0,1)
#16 (generic function with 1 method)

julia> c = x->clamp(x*2,0,1)
#18 (generic function with 1 method)

julia> c.(red.(img)), c.(green.(img)), c.(blue.(img))
([1.0 0.3970532281590171; 0.7613825068122753 1.0], [0.35633721857052847 1.0; 1.0 0.3033949814585586], [1.0 1.0; 0.9445902569521101 0.6861064554657248])

julia> RGB.(c.(red.(img)), c.(green.(img)), c.(blue.(img)))
2×2 Array{RGB{Float64},2} with eltype RGB{Float64}:
 RGB{Float64}(1.0,0.356337,1.0) … RGB{Float64}(0.397053,1.0,1.0)
 RGB{Float64}(0.761383,1.0,0.94459) RGB{Float64}(1.0,0.303395,0.686106)

```

Another solution is to use `channelview` from Images.jl or more precisely ImageCore.jl. This turns your image or 2D RGB array into a 3D array of numbers wherw dimension has a size of 3, one each for r, g, and b.

You can then `clamp` the entire array since you are treating all the sizes the same.

To get back a RGB image, we can then use `reinterpret`.

```julia-repl
julia> using ImageCore

julia> channelview(img)
3×2×2 reinterpret(reshape, Float64, ::Array{RGB{Float64},2}) with eltype Float64: [:, :, 1] =
 0.873815 0.380691
 0.178169 0.850955
 0.869838 0.472295

[:, :, 2] =
 0.198527 0.844428
 0.556571 0.151697
 0.661415 0.343053

julia> c.(channelview(img))
3×2×2 Array{Float64, 3}:
[:, :, 1] =
 1.0 0.761383
 0.356337 1.0
 1.0 0.94459

[:, :, 2] =
 0.397053 1.0
 1.0 0.303395
 1.0 0.686106

julia> reinterpret(RGB{Float64}, c.(channelview(img)))[1,:,:]
2×2 Array{RGB{Float64},2} with eltype RGB{Float64}:
 RGB{Float64}(1.0,0.356337,1.0) … RGB{Float64}(0.397053,1.0,1.0)
 RGB{Float64}(0.761383,1.0,0.94459) RGB{Float64}(1.0,0.303395,0.686106)

```

I recalling there might be a shortcut for reinterpret here, but it is not coming to mind immediately.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [January 14, 2024, 10:19pm UTC](https://discourse.julialang.org/t/access-each-element-in-the-broadcast-operation/108728/6 "2024-01-14T22:19:59Z")

</div>

> [@mkitti](#):
>
> `julia> getproperty.(img, (:r,))`

Wrapping symbols in a tuple seems redundant. They are scalars, I believe.

---

<div class="post-metadata">

### Author: ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)
#### Post date: [January 15, 2024, 8:11am UTC](https://discourse.julialang.org/t/access-each-element-in-the-broadcast-operation/108728/7 "2024-01-15T08:11:03Z")

</div>

How about

```julia
map(img) do pixel RGB(clamp(pixel.r * 2, 0, 1), clamp(pixel.g * 2, 0, 1), clamp(pixel.b * 2, 0, 1)) end

```

but it would be more common to split this into 3 lines

```julia
map(img) do pixel
   RGB(clamp(pixel.r * 2, 0, 1), clamp(pixel.g * 2, 0, 1), clamp(pixel.b * 2, 0, 1))
end

```

An alternative is  
[https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions)
