-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinitUndistortRectifyMapOpenCV.m
70 lines (63 loc) · 2.51 KB
/
initUndistortRectifyMapOpenCV.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
67
68
69
70
function [mapX,mapY,undistortPts, distortPts] = initUndistortRectifyMapOpenCV(K, opencvCoeffs,newCameraMatrixK,newImageSize)
% Brief: 由opencv鱼眼畸变系数得到映射和坐标点对应,功能等同于opencv的initUndistortRectifyMap函数
% Details:
% None
%
% Syntax:
% [mapX,mapY,undistortPts, distortPts] = initUndistortRectifyMapOpenCV(w, h, K, opencvCoeffs)
%
% Inputs:
% K - [3,3] size,[double] type,fisheye camera intrinsic,
% [fx,0,cx;
% 0,fy,cy;
% 0,0,1] format
% opencvCoeffs - [1,4] size,[double] type,opencv fisheye coeffs
% newCameraMatrixK - [3,3] size,[double] type,new fisheye camera intrinsic,
% [fx,0,cx;
% 0,fy,cy;
% 0,0,1] format
% newImageSize - [1,2],[double] type,new image [height,width]
%
% Outputs:
% mapX - [h,w] size,[double] type,Description
% mapY - [h,w] size,[double] type,Description
% undistortPts - [h*w,2] size,[double] type,Description
% distortPts - [h*w,2] size,[double] type,Description
%
%
% See also: None
% Author: cuixingxing
% Email: [email protected]
% Created: 27-Sep-2022 07:42:05
% Implementation In Matlab R2022a
% Copyright © 2022 TheMatrix.All Rights Reserved.
%
arguments
K (3,3) {mustBeNumeric}
opencvCoeffs (1,4) {mustBeNumeric}
newCameraMatrixK (3,3) {mustBeNumeric}
newImageSize (1,2) {mustBeNumeric}
end
% coeff convert to matlab
centerY = newImageSize(1)/2; % ensure distortion lie in center of image
centerX = newImageSize(2)/2; % ensure distortion lie in center of image
offsetX = K(1,3)-centerX;
offsetY = K(2,3)-centerY;
[undistortX,undistortY] = meshgrid(1+offsetX:newImageSize(2)+offsetX,1+offsetY:newImageSize(1)+offsetY);
undistortPts = [undistortX(:),undistortY(:)];
undistortPtsHomo = [undistortPts';
ones(1,prod(newImageSize))]; % 3*cols size
undistortCameraPts = newCameraMatrixK\undistortPtsHomo; % 3*cols size
undistortCameraPts = undistortCameraPts./undistortCameraPts(end,:);% 3*cols size
r = vecnorm(undistortCameraPts(1:2,:),2,1); % 1*cols size
theta = atan(r);
r_d = theta.*(1+opencvCoeffs(1)*theta.^2+opencvCoeffs(2)*theta.^4+...
opencvCoeffs(3)*theta.^6+opencvCoeffs(4)*theta.^8); % r_d非theta_d
r(r<=10^(-8))=1;
scale =r_d./r;
u = K(1,1)*undistortCameraPts(1,:).*scale+ K(1,3);
v = K(2,2)*undistortCameraPts(2,:).*scale + K(2,3);
distortPts = [u',v'];% rows*2
mapX = reshape(distortPts(:,1),newImageSize(1),newImageSize(2));
mapY = reshape(distortPts(:,2),newImageSize(1),newImageSize(2));
end