forked from m-r-s/reference-feature-extraction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheq.m
59 lines (52 loc) · 1.65 KB
/
heq.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function out = heq(in, points)
% usage: out = heq(in, points)
% in values
% points number of fixed points for mapping
% out normalized values
%
% - Histogram equalization v1.0 -
%
% This script performs a histogram equalization (HEQ) of each row of 'in'
% using 'points' fixed percentiles/quantiles for the mapping.
% A detailed explanation is given in [1].
%
% Copyright (C) 2015 Marc René Schädler
% E-mail [email protected]
% Institute Carl-von-Ossietzky University Oldenburg, Germany
%
%-----------------------------------------------------------------------------
%
% Release Notes:
% v1.0 - Inital release
%
% Default number of fixed points
if nargin < 2 || isempty(points)
points = 100;
end
% Calculate the expected minimum and maximum quantiles
% when drawing 'context' samples from the unknown distribution
% C.f. Equation 4 in [1]
context = size(in,2);
exptected_min = 1-context./(context+1);
exptected_max = context./(context+1);
% Define source and target quantiles
source_quantiles = linspace(0, 1, points);
target_quantiles = linspace(exptected_min, exptected_max, points);
% Get source quantiles from data
quantiles = quantile(in, source_quantiles, 2);
% Allocate memory
out = zeros(size(in));
% Loop over all rows
for i = 1:size(in,1)
% Handle the case if all input values are almost equal
if (quantiles(i,end) - quantiles(i,1)) < 100.*eps
out(i,:) = 0.5;
else
mask = [true diff(quantiles(i,:))>0];
out(i,:) = interp1q(quantiles(i,mask).', target_quantiles(mask).', in(i,:).');
end
end
% Map the quantiles to the Gaussian distribution
% using the inverse error function
out = erfinv(out.*2 - 1);
end