# Histogram Equalization

**URL:** https://discourse.julialang.org/t/histogram-equalization/41682
**Category:** General Usage
**Created:** [June 18, 2020, 5:01pm UTC](https://discourse.julialang.org/t/histogram-equalization/41682 "2020-06-18T17:01:37Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Mohammed\_Sabiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohammed_sabiya/32/15779_2.png) [@Mohammed\_Sabiya](https://discourse.julialang.org/u/Mohammed_Sabiya)
#### Post date: [June 18, 2020, 5:01pm UTC](https://discourse.julialang.org/t/histogram-equalization/41682/1 "2020-06-18T17:01:37Z")

</div>

Hey, I’m trying a histogram equalization. I’m getting the following error. Can someone please help me with this?  
Thanks in advance!

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/9/89b41aefdd86d7cacdcdc2c6a666ca137425b963.jpeg)

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 18, 2020, 6:37pm UTC](https://discourse.julialang.org/t/histogram-equalization/41682/2 "2020-06-18T18:37:13Z")

</div>

Please make sure to post actual code rather than screenshots or literal photos of your screen, as it makes reading your question & helping you a lot more cumbersome!

That said the error you’re seeing is telling you that an index you are using is not an integer, but a floating point (decimal) number. This is likely in your call to `cdf[image[i, j] + 1]`, where `image[i, j] + 1` might be an integer?

Are you attempting to call the `cdf` function here? If so, use round brackets `cdf(image[i, j] + 1)` - in general, square brackets `[]` are for indexing, round brackets `()` are for calling.

---

<div class="post-metadata">

### Author: ![Mohammed\_Sabiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohammed_sabiya/32/15779_2.png) [@Mohammed\_Sabiya](https://discourse.julialang.org/u/Mohammed_Sabiya)
#### Post date: [June 19, 2020, 12:34am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/3 "2020-06-19T00:34:32Z")

</div>

Hey sorry for the picture.  
Here is my code:  
I have a histogram of the image,  
hist = myHis(image)  
cum = cumsum (hist)  
cdf = cum ./ sum(cum) #type Array{Float64,1}

#for histogram equalization  
m, n = size(image)  
for I in 1: m-1  
for j in 1: n-1  
map[UInt8( [i,j] \* 255)] = cdf(image[i,j] + 1)  
end  
end

I changed the square brackets to (). Now the error is “MethodError: objects of type Array {Float64, 1} are not callable. Use square brackets for indexing an array”

When I’m using square brackets, the error is “ArgumentError: invalid index: 1.3019608f0 of type Float32”

Thanks!

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [June 19, 2020, 3:31am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/4 "2020-06-19T03:31:36Z")

</div>

> [@Mohammed\_Sabiya](#):
>
> #for histogram equalization  
> m, n = size(image)  
> for I in 1: m-1  
> for j in 1: n-1  
> map[UInt8( [i,j] \* 255)] = cdf(image[i,j] + 1)  
> end  
> end

use triple backticks to surround your code…

in this code isn’t the outer loop variable an “ell” ( l ) not an “eye” ( i )  
that’s your problem. you’re indexing with i

---

<div class="post-metadata">

### Author: ![Mohammed\_Sabiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohammed_sabiya/32/15779_2.png) [@Mohammed\_Sabiya](https://discourse.julialang.org/u/Mohammed_Sabiya)
#### Post date: [June 19, 2020, 3:38am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/5 "2020-06-19T03:38:55Z")

</div>

Oops! Sorry It’s a typo. It’s actually “i”, not ell.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [June 19, 2020, 4:10am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/6 "2020-06-19T04:10:56Z")

</div>

> [@Mohammed\_Sabiya](#):
>
> map[UInt8( [i,j] \* 255)] = cdf(image[i,j] + 1)

This line doesn’t make sense to me. I’m not even able to figure out what you’re trying to do here. Can you describe in words what you’re trying to do?

---

<div class="post-metadata">

### Author: ![Mohammed\_Sabiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohammed_sabiya/32/15779_2.png) [@Mohammed\_Sabiya](https://discourse.julialang.org/u/Mohammed_Sabiya)
#### Post date: [June 19, 2020, 4:27am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/7 "2020-06-19T04:27:47Z")

</div>

I’m mapping the pixel in the image to the new one.  
(i,j) is the intensity of the particular rows and column. I’m casting it as an UInt8 using UInt8(). though it says UInt() I’m pretty sure the values are floating points (it was previously divided by 255 whatever) so I multiply it by 255 and then I cast it as UInt8.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [June 19, 2020, 4:56am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/8 "2020-06-19T04:56:52Z")

</div>

Perhaps you meant `map[i,j] =Uint8(cdf(image[i,j] +1)*255)`

---

<div class="post-metadata">

### Author: ![Mohammed\_Sabiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohammed_sabiya/32/15779_2.png) [@Mohammed\_Sabiya](https://discourse.julialang.org/u/Mohammed_Sabiya)
#### Post date: [June 19, 2020, 5:04am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/9 "2020-06-19T05:04:27Z")

</div>

Isn’t that same? But I tried out and getting the same error “ArgumentError: invalid index: 1.3019608f0 of type Float32”

---

<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: [June 19, 2020, 5:16am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/10 "2020-06-19T05:16:52Z")

</div>

So `cdf` is apparently an array, and so is `map`? It’s easy to get confused, because those two are also the names of functions. In particular, I think you should change the name of the `map` array to make things less confusing.

Maybe you can try

```julia
map[i, j] = cdf[round(UInt8, image[i, j]) + 1] 

```

---

<div class="post-metadata">

### Author: ![Mohammed\_Sabiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohammed_sabiya/32/15779_2.png) [@Mohammed\_Sabiya](https://discourse.julialang.org/u/Mohammed_Sabiya)
#### Post date: [June 19, 2020, 5:35am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/11 "2020-06-19T05:35:06Z")

</div>

So here, cdf is not an in-built function. Cdf is actually from my code as follows:

```julia
cum = cumsum(histogram)
cdf = cum ./ sum(cum) 

```

Oh right! If I change my code like,

```julia
m, n = size(image)
for i in 1-m
      for j in 1-n
            out[i,j] = cdf [round(UInt8, image[i,j]) + 1]
      end
end

```

So image[i,j] is Already a type of UInt8. I don’t think it’s okay to use round(UInt8, image[i,j) because it will throw an TypeError.

---

<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: [June 19, 2020, 5:41am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/12 "2020-06-19T05:41:04Z")

</div>

If `image[i, j]` is already of type `UInt8` then it’s not _necessary_ to round or convert it, but it should work. But why are you trying to change things into `UInt8`, then? Just try

```julia
map[i, j] = cdf[image[i, j] + 1]

```

---

<div class="post-metadata">

### Author: ![Mohammed\_Sabiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohammed_sabiya/32/15779_2.png) [@Mohammed\_Sabiya](https://discourse.julialang.org/u/Mohammed_Sabiya)
#### Post date: [June 19, 2020, 5:51am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/13 "2020-06-19T05:51:02Z")

</div>

I tried that out. It throws an error, “ArgumentError: invalid index: 1.3019608f0 of type Float32”

So I thought about converting it. So i casted UInt8, multiplying it by 255. Unfortunately, it throws the same error.

---

<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: [June 19, 2020, 5:53am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/14 "2020-06-19T05:53:51Z")

</div>

But then `image` _isn’t_ a `UInt8` array after all, but an array of `Float32`. Why can’t you use `round` like I suggested, then?

---

<div class="post-metadata">

### Author: ![Mohammed\_Sabiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohammed_sabiya/32/15779_2.png) [@Mohammed\_Sabiya](https://discourse.julialang.org/u/Mohammed_Sabiya)
#### Post date: [June 19, 2020, 6:03am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/15 "2020-06-19T06:03:50Z")

</div>

Typeof(image) is Array{Gray{Normed{UInt8,8}}, 2}

This is what I’m confused about.

Type of my histogram and cdf both are, Array{Float64, 1}

So I tried using round.() to my cdf, to convert it into UInt8. The plot is more like step side function, not a cumulative histogram.

---

<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: [June 19, 2020, 6:17am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/16 "2020-06-19T06:17:24Z")

</div>

So, the thing is, there’s apparently a lot of important information that we don’t have access to. In order to help, we need to know what all the different variables are, and what they mean. Perhaps you can read this [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) , before going further? Just the first post, not the whole thread.

---

<div class="post-metadata">

### Author: ![Mohammed\_Sabiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohammed_sabiya/32/15779_2.png) [@Mohammed\_Sabiya](https://discourse.julialang.org/u/Mohammed_Sabiya)
#### Post date: [June 19, 2020, 6:52am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/17 "2020-06-19T06:52:14Z")

</div>

well then,

```julia
julia> using Images, ImageView, Plots, ImageCore

#Histogram function 
julia> function myHis(image)
        m, n = size(image)
        frequency = zeros(256)
    
            for i in 1:m, j in 1:n
                 frequency[UInt8(image[i,j] * 255)] += 1    
            end
    
         return frequency ./ sizeof(image)
         end

julia> image = load("bookstore_dark.tif")
        hist = myHis(image)
        cum = cumsum(hist)
        cdf = cum ./ sum(cum)

#Histogram Equalization
julia> m, n = size(image)
        for i in 1:m-1
            for j in 1:n-1
                out[UInt8([i,j] * 255)]= cdf[image[i,j] + 1]
            end
        end
      
Error:
ArgumentError: invalid index: 1.3019608f0 of type Float32

Stacktrace:
 [1] to_index(::Float32) at .\indices.jl:297
 [2] to_index(::Array{Float64,1}, ::Float32) at .\indices.jl:274
 [3] to_indices at .\indices.jl:325 [inlined]
 [4] to_indices at .\indices.jl:322 [inlined]
 [5] getindex(::Array{Float64,1}, ::Float32) at .\abstractarray.jl:980
 [6] top-level scope at .\In[102]:4

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 19, 2020, 8:13am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/18 "2020-06-19T08:13:44Z")

</div>

Note that this code depends on a file local to your computer (`bookstore_dark.tif`), so it’s not possible for others to run.

That said, the problem still seems to be the one initially conjectured: your `image` object likely contains `Float32` element, so when you do `cdf[image[i, j] + 1]` you are essentially trying to get the 1.3rd element of the `cdf` vector, which is not an operation that makes sense.

This isn’t the only issue in that loop though, as:

```julia
julia> UInt8([1,1]*255)
ERROR: MethodError: no method matching UInt8(::Array{Int64,1})
Closest candidates are:
  UInt8(::Union{Bool, Int32, Int64, UInt32, UInt64, UInt8, Int128, Int16, Int8, UInt128, UInt16}) at boot.jl:709
  UInt8(::Float32) at float.jl:686
  UInt8(::Float64) at float.jl:686

```

You can’t create an 8-bit unsigned integer from a vector. You might be trying to convert the vector into `UInt8`s, in which case you want to broadcast `UInt8.([1,1]*255)`. Note however that idexing with `UInt8` doesn’t work:

```julia
julia> rand(10, 10)[UInt8.([1,1]*255)]
ERROR: BoundsError: attempt to access 10×10 Array{Float64,2} at index [UInt8[0xff, 0xff]]
Stacktrace:
 [1] throw_boundserror(::Array{Float64,2}, ::Tuple{Array{UInt8,1}}) at ./abstractarray.jl:537
 [2] checkbounds at ./abstractarray.jl:502 [inlined]
 [3] _getindex at ./multidimensional.jl:726 [inlined]
 [4] getindex(::Array{Float64,2}, ::Array{UInt8,1}) at ./abstractarray.jl:980
 [5] top-level scope at REPL[19]:1

```

So given that you’re just using the result of this `UInt8` construction to index your `out` array, it’s probably best to forget about this altogether.

---

<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: [June 19, 2020, 9:36am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/19 "2020-06-19T09:36:13Z")

</div>

> [@Mohammed\_Sabiya](#):
>
> `out[UInt8([i,j] * 255)]`

Where does `out` come from? It has to be initialized somewhere.

So your problem is indexing. We don’t know what your image is, so that makes it harder, but at least we can point out things that do not make sense:

> [@Mohammed\_Sabiya](#):
>
> ```julia
> frequency[UInt8(image[i,j] * 255)] += 1
> 
> ```

If `image` is an array of floats, then `UInt8(image[i,j] * 255)` is unlikely to work, since `UInt8` can only convert integer values:

```julia
julia> UInt8(1.0) # this works
0x01

julia> UInt8(1.6) # this doesn't works
ERROR: InexactError: UInt8(1.6)

```

Therefore I think you should round or truncate instead of convert:

```julia
julia> round(UInt8, 1.0) # works
0x01

julia> round(UInt8, 1.6) # works
0x02

julia> trunc(UInt8, 1.0) # works
0x01

julia> trunc(UInt8, 1.6) # works
0x01

```

In fact, when indexing you should normally just use `Int`s instead of `UInt8`.

So you can try

```julia
frequency[trunc(UInt8, image[i,j] * 255)] += 1

```

but, I got the impression that `image[i,j]` isn’t a normal float, but a `RGB` type, and I don’t know how to handle them.

> [@Mohammed\_Sabiya](#):
>
> `cdf[image[i,j] + 1]`

Here you are trying to index into `cdf` using the contents of `image[i,j]`, and since that is not an integer, you cannot use it for indexing.

And then there’s this:

> [@Mohammed\_Sabiya](#):
>
> `out[UInt8([i,j] * 255)]`

Here you are trying to convert the vector `[i, j]` of `Int`s to `UInt8`. I don’t understand why you are trying to do that, since you can (and should) use ordinary `Int`s for indexing, and also because `UInt8([i,j]*255)` could never work anyway. `[i,j]*255` is a vector, so then you would have to use `UInt8.([i,j]*255)`, but even that wouldn’t work, because `i*255` and `j*255` is _bigger_ than 255 except when `i` and `j` are 0 or 1.

---

<div class="post-metadata">

### Author: ![Mohammed\_Sabiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohammed_sabiya/32/15779_2.png) [@Mohammed\_Sabiya](https://discourse.julialang.org/u/Mohammed_Sabiya)
#### Post date: [June 19, 2020, 9:59am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/20 "2020-06-19T09:59:13Z")

</div>

I understand now how it works. Thanks!  
i just found i missed something in my histogram function.

```julia
  julia> function myHis(image)
           m, n = size(image)
           frequency = zeros(256)
        for i in 1:m, j in 1:n
        frequency[UInt8(image[i,j] * 255)+1 ] += 1   
    end
    
    return frequency ./ sizeof(image)
end

```

[Next page](https://discourse.julialang.org/t/histogram-equalization/41682.md?page=2)
