# Index Image with BitArray

**URL:** <https://discourse.julialang.org/t/index-image-with-bitarray/28625>\
**Category:** General Usage\
**Tags:** images\
**Created:** [September 10, 2019, 9:53pm UTC](https://discourse.julialang.org/t/index-image-with-bitarray/28625 "2019-09-10T21:53:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rand5](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rand5/32/867_2.png) [@rand5](https://discourse.julialang.org/u/rand5)\
**Post date:** [September 10, 2019, 9:53pm UTC](https://discourse.julialang.org/t/index-image-with-bitarray/28625/1 "2019-09-10T21:53:39Z")

</div>

Is it no longer possible to index an image with a BitArray?

I see the following example on the `Images.jl` doc page [https://github.com/JuliaImages/ImageView.jl:](https://github.com/JuliaImages/ImageView.jl:)

```julia
using ImageView, TestImages, Colors

# Prepare the data
mri = testimage("mri")
mriseg = RGB.(mri)
mriseg[mri .> 0.5] = colorant"red"

```

But get the following error when I try to replicate it:

```julia
MethodError: no method matching setindex_shape_check(::RGB{Normed{UInt8,8}}, ::Int64)
Closest candidates are:
  setindex_shape_check(!Matched::AbstractArray{#s72,1} where #s72, ::Integer) at indices.jl:218
  setindex_shape_check(!Matched::AbstractArray{#s72,1} where #s72, ::Integer, !Matched::Integer) at indices.jl:221
  setindex_shape_check(!Matched::AbstractArray{#s72,2} where #s72, ::Integer, !Matched::Integer) at indices.jl:225
  ...

Stacktrace:
 [1] macro expansion at .\multidimensional.jl:694 [inlined]
 [2] _unsafe_setindex!(::IndexLinear, ::Array{RGB{Normed{UInt8,8}},2}, ::RGB{Normed{UInt8,8}}, ::Base.LogicalIndex{Int64,BitArray{2}}) at .\multidimensional.jl:689
 [3] _setindex! at .\multidimensional.jl:684 [inlined]
 [4] setindex!(::Array{RGB{Normed{UInt8,8}},2}, ::RGB{Normed{UInt8,8}}, ::BitArray{2}) at .\abstractarray.jl:1020
 [5] top-level scope at In[66]:3
```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [September 10, 2019, 10:02pm UTC](https://discourse.julialang.org/t/index-image-with-bitarray/28625/2 "2019-09-10T22:02:47Z")

</div>

Since Julia 0.7, if you want to set an array elementwise, you need to use `.=`

```julia
julia> x = rand(5, 5)
5×5 Array{Float64,2}:
 0.867726 0.790569 0.586163 0.851478 0.51114 
 0.281574 0.619415 0.495319 0.181507 0.908403
 0.0461658 0.435964 0.796173 0.593969 0.208124
 0.434098 0.614399 0.276483 0.47161 0.620724
 0.323744 0.321267 0.483477 0.739554 0.635631

julia> x[x .> 0.5] = 2
ERROR: MethodError: no method matching setindex_shape_check(::Int64, ::Int64)
Closest candidates are:
  setindex_shape_check(::AbstractArray{#s72,1} where #s72, ::Integer) at indices.jl:218
  setindex_shape_check(::AbstractArray{#s72,1} where #s72, ::Integer, ::Integer) at indices.jl:221
  setindex_shape_check(::AbstractArray{#s72,2} where #s72, ::Integer, ::Integer) at indices.jl:225
  ...
Stacktrace:
 [1] macro expansion at ./multidimensional.jl:694 [inlined]
 [2] _unsafe_setindex!(::IndexLinear, ::Array{Float64,2}, ::Int64, ::Base.LogicalIndex{Int64,BitArray{2}}) at ./multidimensional.jl:689
 [3] _setindex! at ./multidimensional.jl:684 [inlined]
 [4] setindex!(::Array{Float64,2}, ::Int64, ::BitArray{2}) at ./abstractarray.jl:1020
 [5] top-level scope at none:0

julia> x[x .> 0.5] .= 2
13-element view(::Array{Float64,1}, [1, 6, 7, 9, 11, 13, 16, 18, 20, 21, 22, 24, 25]) with eltype Float64:
 2.0                                                                                                                                                                                                                                                                                                                                  
 2.0                                                                                                                                                                                                                                                                                                                                  
 2.0                                                                                                                                                                                                                                                                                                                                  
 2.0                                                                                                                                                                                                                                                                                                                                  
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0

julia> x
5×5 Array{Float64,2}:
 2.0 2.0 2.0 2.0 2.0     
 0.281574 2.0 0.495319 0.181507 2.0     
 0.0461658 0.435964 2.0 2.0 0.208124
 0.434098 2.0 0.276483 0.47161 2.0     
 0.323744 0.321267 0.483477 2.0 2.0   

```

The same thing works in your case:

```julia
julia> mriseg[mri .> 0.5] .= colorant"red"

```

---

<div class="post-metadata">

**Author:** ![rand5](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rand5/32/867_2.png) [@rand5](https://discourse.julialang.org/u/rand5)\
**Post date:** [September 10, 2019, 10:04pm UTC](https://discourse.julialang.org/t/index-image-with-bitarray/28625/3 "2019-09-10T22:04:18Z")

</div>

> [@rand5](#):
>
> mri

Thank you!
