Skip to content

Commit

Permalink
Adds Histogram Equalisation Function
Browse files Browse the repository at this point in the history
  • Loading branch information
mronian committed Mar 24, 2016
1 parent cf1efb3 commit be49781
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/src/function_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ include the following functions:
padarray
}

# Exposure

@{
histeq
}

# Filtering kernels

@{
Expand Down
2 changes: 2 additions & 0 deletions src/Images.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export # types
imfilter_fft,
imfilter_gaussian,
imfilter_gaussian!,
histeq,
imfilter_LoG,
imgaussiannoise,
imgradients,
Expand Down Expand Up @@ -266,6 +267,7 @@ Algorithms:
- Reductions: `maxfinite`, `maxabsfinite`, `minfinite`, `meanfinite`, `sad`, `ssd`
- Resizing: `restrict`, `imresize` (not yet exported)
- Exposure: `histeq`
- Filtering: `imfilter`, `imfilter_fft`, `imfilter_gaussian`, `imfilter_LoG`, `imROF`, `ncc`, `padarray`
- Filtering kernels: `ando[345]`, `guassian2d`, `imaverage`, `imdog`, `imlaplacian`, `prewitt`, `sobel`
- Gradients: `backdiffx`, `backdiffy`, `forwarddiffx`, `forwarddiffy`, `imgradients`
Expand Down
36 changes: 36 additions & 0 deletions src/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,42 @@ function copytail!(dest, A, coloffset, strd, len)
dest
end


# Histogram Equalisation Function
"""
```
hist_equalised_img = histeq(img, nbins)
Returns a grayscale histogram equalised image with a granularity of `nbins` number of bins.
```
"""
function histeq{T,N}(img::AbstractArray{T,N}, nbins::Int)
imgv = img[:]
imgmax=maxfinite(imgv)
imgmin=minfinite(imgv)
newmaxintensity=1
if T<:Colorant
imgv = [(ColorTypes.cconvert(Gray, imgv[i])) for i=1:length(imgv)]
elseif T<:Number
imgv = [(imgv[i]-imgmin)/(imgmax-imgmin) for i=1:length(imgv)]
end
_,histogram = hist(imgv, 0:1/nbins:1)
cdfmin = 0
cdf = zeros(Int64, nbins)
for i = 1:nbins
cdf[i:nbins] += histogram[i]
if cdf[i] == 0
cdfmin += 1
end
end
retimg = [(imgv[i] == 0)? 1:Int(ceil(nbins*imgv[i])) for i = 1:length(imgv)]
retimg = [newmaxintensity*((cdf[retimg[i]]-cdfmin)/(length(imgv)-cdfmin)) for i = 1:length(retimg)]
retimg = reshape(retimg, size(img))
retimg = convert(Image{ColorTypes.Gray{U8}}, retimg')
retimg
end

# Laplacian of Gaussian filter
# Separable implementation from Huertas and Medioni,
# IEEE Trans. Pat. Anal. Mach. Int., PAMI-8, 651, (1986)
Expand Down
11 changes: 11 additions & 0 deletions test/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ facts("Algorithms") do
@fact img2 --> roughly(reinterpret(Float32, img))
end

context("Exposure") do
im1 = reshape(1:1:100, 10, 10)
ret1 = histeq(im1, 10)
@fact hist(im1[:], 0:10:100)[2]-->hist(ret1[:], 0:0.1:1)[2]

im2 = reshape(1:1:100, 10, 10)
im2 = grayim(im2)
ret2 = histeq(im2, 10)
@fact hist(im2[:], 0:10:100)[2]-->hist(ret2[:], 0:0.1:1)[2]
end

context("Array padding") do
A = [1 2; 3 4]
@fact Images.padindexes(A, 1, 0, 0, "replicate") --> [1,2]
Expand Down

0 comments on commit be49781

Please sign in to comment.