# Julia Images adaptive histogram equalization vs MATLAB's implementation

**URL:** https://discourse.julialang.org/t/julia-images-adaptive-histogram-equalization-vs-matlabs-implementation/119469
**Category:** Signal and Image Processing
**Tags:** question
**Created:** [September 16, 2024, 5:12pm UTC](https://discourse.julialang.org/t/julia-images-adaptive-histogram-equalization-vs-matlabs-implementation/119469 "2024-09-16T17:12:05Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![cpaniaguam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpaniaguam/32/33747_2.png) [@cpaniaguam](https://discourse.julialang.org/u/cpaniaguam)
#### Post date: [September 16, 2024, 5:12pm UTC](https://discourse.julialang.org/t/julia-images-adaptive-histogram-equalization-vs-matlabs-implementation/119469/1 "2024-09-16T17:12:05Z")

</div>

I’m hoping to get an expert’s opinion on this matter.

In MATLAB:

```matlab
>> img = imread('cameraman.tif');
>> class(img)

ans =

    'uint8'

>> sum(img(:))

ans =

     7780728

>> img_eq = adapthisteq(img, Range="original", NBins=256, NumTiles=[8 8], ClipLimit=0.01);
>> figure; imshow(cat(2,img,img_eq));

```

The images look like this (original on left):  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/3/032d6ced8341e2b742b04761c7a2c273e088d59e.jpeg)

For the same workflow in Julia I exported `cameraman` from MATLAB to a file to read in (the one in `TestImages` is not the same for some reason) but for the illustration below we’ll stick to the one from `TestImages`

```julia
using Images, ImageContrastAdjustment, TestImages
img = testimage("cameraman")
img = Int64.(img .* 255) # converting to ints as `load` produces eltype Gray{N0f8} and the algo doesn't like that
maxval, minval = maximum(img), minimum(img)

algo = AdaptiveEqualization(minval=minval, maxval=maxval, nbins=256, rblocks=8, cblocks=8, clip=0.01)
img_adjusted = adjust_histogram(img, algo)
Gray.(hcat(img, img_adjusted) / 255) # for the visualization

```

You get this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/3/d3ebb691283400cb9d6665fbc8b2e14aa9a47013.jpeg)

As you can see the image is exceedingly oversharpened. Is there something I am overlooking? I noticed the default values of `(minval, maxval)` are set to `(0, 1)` which seem low for uint8 images. However, I converted the input image to gray with `Gray.(img / 255)` using those default values and got the same effect still.

---

<div class="post-metadata">

### Author: ![zygmuntszpak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zygmuntszpak/32/2591_2.png) [@zygmuntszpak](https://discourse.julialang.org/u/zygmuntszpak)
#### Post date: [September 21, 2024, 1:04pm UTC](https://discourse.julialang.org/t/julia-images-adaptive-histogram-equalization-vs-matlabs-implementation/119469/2 "2024-09-21T13:04:55Z")

</div>

> [@cpaniaguam](#):
>
> `img = testimage("cameraman")`

It has been a while since I implemented this, so I don’t immediately recall all of the details. I’m not sure what how MATLAB has interpreted the parameters, but it looks like my parameter `clip` and MATLABs `ClipLimit` are not exactly the same. At first glance, it looks like `clip ≈ 1 - ClipLimit`. So if you try

```julia
using Images, ImageContrastAdjustment, TestImages
img = testimage("cameraman")
algo = AdaptiveEqualization(minval=minval, maxval=maxval, nbins=256, rblocks=8, cblocks=8, clip=0.99)
img_adjusted = adjust_histogram(img, algo)

```

you should get something similar to your MATLAB result.

 ![original_cam](https://global.discourse-cdn.com/julialang/original/3X/9/a/9a25d26bf64ebccfa69f0edaba1978080811e1bb.png)  
 ![adjusted_cam](https://global.discourse-cdn.com/julialang/original/3X/f/a/fa93ec43502c5d37d9f7ae63069f39145e69594f.png)

You don’t need to convert the image to Int64. The algorithm will work on the `Gray{N0f8}` input. All images are represented on a [0…1] intensity scale, so 0 is black and 1 is white. The rationale for this representation is explained in the docmentation: [Quickstart · JuliaImages](https://juliaimages.org/latest/tutorials/quickstart/)[JuliaImages Quickstart](https://juliaimages.org/latest/tutorials/quickstart/)
