-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageStretchByTol.m
51 lines (45 loc) · 1.22 KB
/
ImageStretchByTol.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
function [stretchedImage, t] = ImageStretchByTol(image, tol, tol_type)
if ~exist('tol','var')
tol = [0.01, 0.99];
end
if ~exist('tol_type','var')
tol_type = 1;
end
nDims=size(image,3);
stretchedImage=image;
[N,M,~] = size(image);
if tol_type == 0
stretchedImage = ImageStretch(image);
t(1) = 0; t(2) = 1;
elseif tol_type == 1
for i=1:nDims
b = image(:,:,i);
t(1) = quantile(b(:),tol(1));
t(2) = quantile(b(:),tol(2));
b(b<t(1))=t(1);
b(b>t(2))=t(2);
b = (b-t(1))/(t(2)-t(1));
stretchedImage(:,:,i) = reshape(b,N,M);
end
elseif tol_type == 2
stretchedImage = ImageStretchByfixedTol(image, tol);
elseif tol_type == 3
for i=1:nDims
b = image(:,:,i);
assert(abs(mean2(b))<1);
t(1) = quantile(b(:),tol(1));
t(2) = quantile(b(:),tol(2));
if abs(t(1)) > abs(t(1))
tt = abs(t(1));
else
tt = abs(t(2));
end
b(b<-tt)=-tt;
b(b>tt)=tt;
b = (b+tt)/(tt*2);
stretchedImage(:,:,i) = reshape(b,N,M);
end
stretchedImage = ImageStretchByfixedTol(image, tol);
else
stretchedImage = image;
end