forked from ndwork/dworkLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplyRadialDistortion.m
More file actions
49 lines (41 loc) · 1.47 KB
/
applyRadialDistortion.m
File metadata and controls
49 lines (41 loc) · 1.47 KB
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
function out = applyRadialDistortion( img, ks, varargin )
% out = applyRadialDistortion( img, k [, c ] )
%
% Written according to section 7.4 of Multiple View Geometry, 2nd edition
% by Hartley and Zisserman
%
% Inputs:
% img - a 2D array or 3D array where the third dimension is color
% ks - the radial distortion coefficients
% L(r) = 1 + k(1) * r + k(2) * r^2 + ...
% c - the center of the image [ center_x center_y ]
%
% Outputs:
% out - the image with radial distortion applied
%
% Written by Nicholas Dwork - Copyright 2019
%
% https://github.com/ndwork/dworkLib.git
%
% This software is offered under the GNU General Public License 3.0. It
% is offered without any warranty expressed or implied, including the
% implied warranties of merchantability or fitness for a particular
% purpose.
p = inputParser;
p.addParameter( 'c', [], @(x) isnumeric(x) && numel(x) == 2 );
p.parse( varargin{:} );
c = p.Results.c;
if numel( c ) == 0, c = zeros(2,1); end
sImg = size(img);
coords = size2imgCoordinates( sImg(1:2) );
[xs,ys] = meshgrid( coords{2}, coords{1} );
distortedPts = applyRadialDistortion2Pts( [ xs(:) ys(:) ], ks, c );
xTildes = distortedPts( :, 1 );
yTildes = distortedPts( :, 2 );
out = zeros( sImg );
for colorIndx = 1 : size( img, 3 )
colorChannel = img(:,:,colorIndx);
tmp = interp2( xs, ys, colorChannel, xTildes, yTildes, 'linear', 0 );
out(:,:,colorIndx) = reshape( tmp, sImg(1:2) );
end
end