# 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:** 9\
**Page:** 2

<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, 10:15am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/21 "2020-06-19T10:15:04Z")

</div>

It’s still not possible for us to know what `image[i,j]` contains, but FWIW note that `1` is a literal that automatically gets parsed as `Int` (`Int64` on most systems):

```julia
julia> typeof(1)
Int64

```

I don’t understand why you think you need the `UInt8()` conversion here, but adding `1` to the result of `UInt8()` makes this conversion superfluous anyway due to type promotion that happens when adding a `UInt8` and an `Int`:

```julia
julia> UInt8(255)
0xff

julia> UInt8(255) + 1
256

julia> typeof(UInt8(255) + 1)
Int64

```

---

<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, 10:19am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/22 "2020-06-19T10:19:34Z")

</div>

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

Again, this is not a good idea, since you will get an error unless `image[i,j] * 255` is _exactly_ a whole number. Therefore, you should use `round` or `trunc` instead.

---

<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, 10:26am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/23 "2020-06-19T10:26:37Z")

</div>

I understand now how it works. Thanks!

if you see the code, I’m didn’t add “+1” to

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

which doesn’t make sense to get a histogram. so i updated that line to

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

```

The explanation for using `UInt8` and `multiplying by 255` is explained my tutor as follow,

> You need to cast like this in order to use the image values for access. The internal representation uses Normed{UInt8, 8}, since this corresponds directly to the underlying data representation (UInt8), but also behaves like a floating point number, so you can do useful calculations on the images without converting between representations.

and the image is `Gray image`

i couldn’t upload the image as it’s in `.tif` file.

---

<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, 10:55am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/24 "2020-06-19T10:55:23Z")

</div>

But you’re doing it here:

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

`UInt8(image[i,j] * 255) +1` will not be an `UInt8`, because an `UInt8` plus an `Int64` is an `Int64`, and `1` is an `Int64`, look:

```julia
julia> typeof(UInt8(5) + 1)
Int64

```

Since you don’t provide any example image data, it’s difficult to say whether what your tutor is saying is correct, but it looks strange to me. As far as I understand `image[i,j] * 255` will be a `Float32`, and in that case there’s no obvious need to use `UInt8`, but I guess no harm, either. But I am very skeptical of using `UInt8()` instead of `round(UInt8, )` or `trunc(UInt8, )`.

---

<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, 11:13am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/25 "2020-06-19T11:13:24Z")

</div>

Okay to reduce the confusion I went ahead an installed `TestImages`. Here’s what’s happening (I think):

```julia
julia> using Images, TestImages

julia> image = testimage("cameraman.tif");

julia> typeof(image)
Array{Gray{Normed{UInt8,8}},2}

```

So `image` is an array of `Gray{Normed{UInt8,8}}` elements:

```julia
julia> image[1,1]
Gray{N0f8}(0.612)

```

If you multiply those by an `Int`, as you do, you end up with:

```julia
julia> image[1,1]*255
Gray{Float32}(156.0f0)

```

that doesn’t work for indexing, you need an integer. You converted this back to `UInt8`:

```julia
julia> UInt8(image[1,1]*255)
0x9c

```

but then you’re directly adding a `1`, which as we said is just a literal that gets parsed as an `Int64`, and adding that to your `UInt8` just gives you a regular `Int`:

```julia
julia> UInt8(image[1,1]*255)+1
157

```

So this is essentially the same as doing:

```julia
julia> Int(image[1,1]*255)+1
157

```

---

<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, 11:29am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/26 "2020-06-19T11:29:22Z")

</div>

Thanks for looking that up.

Question is, is

> [@nilshg](#):
>
> `image[1,1]*255`

guaranteed to always be a whole number?

---

<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, 11:40am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/27 "2020-06-19T11:40:26Z")

</div>

I don’t know enough about how images are represented, but there might be something about the `Gray{N0f8}` that means it holds discrete levels of 255 shades of gray which will always be an integer between 1 and 255 when multiplied by 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 20, 2020, 2:41am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/28 "2020-06-20T02:41:57Z")

</div>

@DNF I noticed once of your reply saying, where the below code is initialized.

`out[UInt8([i,j] * 255)]`

Dumb me, I didn’t do that. Now I initialize with,

```julia
out = zeros(Uint8, sizeof(image))

```

As I already initialized `out` with UInt8, I don’t need to do the conversion again in the code. So now I can change the line to

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

```

And, I have attached an `.png` file and the code to show the working of histogram for the line, ` frequency[UInt8(image[i,j] * 255)+1 ] += 1 `

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

```

![obstA](https://global.discourse-cdn.com/julialang/original/3X/c/9/c95d99e0a42a3b6eb05af7f86c601ad11cc7c51d.png)

---

<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 20, 2020, 8:25am UTC](https://discourse.julialang.org/t/histogram-equalization/41682/29 "2020-06-20T08:25:36Z")

</div>

between 0 and 255.  
0 represents black and 255 represents white

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