From be4978196acc1a75d8600c9a247ba6799a7c6235 Mon Sep 17 00:00:00 2001 From: mronian Date: Wed, 23 Mar 2016 03:55:53 +0530 Subject: [PATCH] Adds Histogram Equalisation Function --- docs/src/function_reference.md | 6 ++++++ src/Images.jl | 2 ++ src/algorithms.jl | 36 ++++++++++++++++++++++++++++++++++ test/algorithms.jl | 11 +++++++++++ 4 files changed, 55 insertions(+) diff --git a/docs/src/function_reference.md b/docs/src/function_reference.md index 823c32a4..e960f06e 100644 --- a/docs/src/function_reference.md +++ b/docs/src/function_reference.md @@ -268,6 +268,12 @@ include the following functions: padarray } +# Exposure + +@{ + histeq +} + # Filtering kernels @{ diff --git a/src/Images.jl b/src/Images.jl index 97c2073f..66925dec 100644 --- a/src/Images.jl +++ b/src/Images.jl @@ -202,6 +202,7 @@ export # types imfilter_fft, imfilter_gaussian, imfilter_gaussian!, + histeq, imfilter_LoG, imgaussiannoise, imgradients, @@ -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` diff --git a/src/algorithms.jl b/src/algorithms.jl index 117554f2..6f542386 100644 --- a/src/algorithms.jl +++ b/src/algorithms.jl @@ -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) diff --git a/test/algorithms.jl b/test/algorithms.jl index fe31bb04..1e651116 100644 --- a/test/algorithms.jl +++ b/test/algorithms.jl @@ -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]