forked from matt-black/vk4mat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vk4_correctHeightTilt.m
66 lines (61 loc) · 2.4 KB
/
vk4_correctHeightTilt.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
60
61
62
63
64
65
66
function [ H_corr ] = vk4_correctHeightTilt ( hgtImg, method )
%VK4_CORRECTHEIGHTTILT corrects tilts in height image
if ischar (hgtImg)
[~, ~, ext] = fileparts (hgtImg);
ext = lower (ext(2:end));
switch ext
case 'vk4' % read in height image
hgtImg = vk4_readImageType (hgtImg, 'h');
case 'tif'
hgtImg = vk4_readTiff (hgtImg);
case 'tiff'
hgtImg = vk4_readTiff (hgtImg);
otherwise
error ('vk4_correctHeightTilt :: Unknown file type %s', ext)
end
end
hgtImg = double (hgtImg);
% make sure we have a numeric array to work with
assert (isnumeric (hgtImg), ['vk4_correctHeightTilt :: hgtImg isnt' ...
' a numeric']);
method = lower (method);
switch method
case 'curvature'
H_corr = correctHeightTiltByCurvFilt (hgtImg);
case 'linfitedge'
H_corr = correctHeightTiltByEdgeLine (hgtImg);
otherwise
error ('vk4_correctHeightTilt :: unknown method %s', method)
end
end
function [ H_corr ] = correctHeightTiltByCurvFilt ( hgtImg )
%CORRECTHEIGHTTILTBYCURVFILT
[Xm,Ym] = meshgrid (1:size(hgtImg,2), 1:size(hgtImg,1));
[K, H] = surfature (Xm, Ym, imgaussfilt (hgtImg, 10));
lowBound = quantile (K, 0.2);
mask = hgtImg < quantile (hgtImg, 0.33) & K < quantile (K, 0.33);
Xs = Xm(mask); Ys = Ym(mask); Hs = hgtImg(mask);
surfFit = fit ([Xs, Ys], Hs, 'poly11');
H_corr = hgtImg - feval (surfFit, Xm, Ym);
end
function [ H_corr ] = correctHeightTiltByEdgeLine ( hgtImg )
%CORRECTHEIGHTTILTBYEDGELINE
imgSze = size (hgtImg);
%
rows = (1:imgSze(1))';
rvals = mean (hgtImg(:,1:11), 2);
rvals2 = mean (hgtImg(:,end-11:end), 2);
rcols = ones (size (rvals)) .* mean (1:11);
rcols2 = ones (size (rvals2)) .* mean ((imgSze(2)-11):imgSze(2));
cols = (1:imgSze(2))';
cvals = mean (hgtImg(1:11,:), 1)';
cvals2 = mean (hgtImg(end-11:end,:), 1)';
crows = ones (size (cvals)) .* mean (1:11);
crows2 = ones (size (cvals2)) .* mean ((imgSze(1)-11):imgSze(1));
Xs = vertcat (cols, cols, rcols, rcols2);
Ys = vertcat (crows, crows2, rows, rows);
Hs = vertcat (cvals, cvals2, rvals, rvals2);
surfFit = fit ([Xs, Ys], Hs, 'poly11');
[Xm, Ym] = meshgrid (1:size(hgtImg,2), 1:size(hgtImg,1));
H_corr = hgtImg - feval (surfFit, Xm, Ym);
end