摄像机成像、畸变模型(三)

灰度图像去畸变
本文提供了一段MATLAB代码,用于校正灰度图像的径向畸变。该代码接收图像及相机校准参数作为输入,通过逆向投影、应用畸变系数,并采用双线性插值来重建无畸变图像。

图像去畸变matlab代码:

function rectifiedImage = undistortImage_grayscale(image, cameraCalibration)
%UNDISTORTIMAGE_GRAYSCALE Undistort a grayscale image for radial distortion.
%   RECTIFIEDIMAGE = UNDISTORTIMAGE_GRAYSCALE(IMAGE, CAMERACALIBRATION)
%   undistorts a grayscale image (MxN matrix) for radial distortion.
%
%   CAMERACALIBRATION must contain the following elements:
%     CAMERACALIBRATION.FOCAL_LENGTHS (1x2)
%     CAMERACALIBRATION.IMAGE_CENTER (1x2)
%     CAMERACALIBRATION.DISTORTION_COEFICIENTS (1x5)
%     CAMERACALIBRATION.IMAGE_WIDTH
%     CAMERACALIBRATION.IMAGE_HEIGHT

%   Author: excerpt from the Camera Calibration Toolbox for Matlab
%   (http://www.vision.caltech.edu/bouguetj/calib_doc/), modified by Damien
%   Teney

% Check the arguments
assert(size(image, 3) == 1); % Only one channel

% Cast input to doubles
image = double(image);

% Rename for clarity
f = cameraCalibration.FOCAL_LENGTHS;
c = cameraCalibration.IMAGE_CENTER;
k = cameraCalibration.DISTORTION_COEFICIENTS;
nc = cameraCalibration.IMAGE_WIDTH;
nr = cameraCalibration.IMAGE_HEIGHT;
%[nr nc] = size(image); % Equivalent

K = getK();
R = eye(3);

[mx my] = meshgrid(1:nc, 1:nr);
px = reshape(mx', nc * nr, 1);
py = reshape(my', nc * nr, 1);

rays = inv(K) * [(px - 1)' ; (py - 1)' ; ones(1,length(px))];

% Rotation
rays2 = R' * rays;
x = [rays2(1,:)./rays2(3,:);rays2(2,:)./rays2(3,:)];

% Add distortion
xd = applyDistortion(x, k);

% Reconvert in pixels
px2 = f(1) * xd(1,:) + c(1);
py2 = f(2) * xd(2,:) + c(2);

% Interpolate between the closest pixels:
px_0 = floor(px2);
py_0 = floor(py2);

good_points = find((px_0 >= 0) & ...
                   (px_0 <= (nc-2)) & ...
                   (py_0 >= 0) & ...
                   (py_0 <= (nr-2)));

px2 = px2(good_points);
py2 = py2(good_points);
px_0 = px_0(good_points);
py_0 = py_0(good_points);

alpha_x = px2 - px_0;
alpha_y = py2 - py_0;

a1 = (1 - alpha_y).*(1 - alpha_x);
a2 = (1 - alpha_y).*alpha_x;
a3 = alpha_y .* (1 - alpha_x);
a4 = alpha_y .* alpha_x;

ind_lu = px_0 * nr + py_0 + 1;
ind_ru = (px_0 + 1) * nr + py_0 + 1;
ind_ld = px_0 * nr + (py_0 + 1) + 1;
ind_rd = (px_0 + 1) * nr + (py_0 + 1) + 1;

ind_new = (px(good_points)-1)*nr + py(good_points);

rectifiedImage = 255 * ones(size(image));
rectifiedImage(ind_new) = a1 .* image(ind_lu) + ...
                          a2 .* image(ind_ru) + ...
                          a3 .* image(ind_ld) + ...
                          a4 .* image(ind_rd);
% Cast output to uint8
rectifiedImage = uint8(rectifiedImage);
end

%--------------------------------------------------------------------------

function xd = applyDistortion(x, k)

assert(numel(k) == 5);

% Add distortion
r2 = x(1,:).^2 + x(2,:).^2;
r4 = r2.^2;
r6 = r2.^3;

% Radial distortion
cdist = 1 + k(1) * r2 + k(2) * r4 + k(5) * r6;
xd1 = x .* (ones(2,1) * cdist);

% Tangential distortion
a1 = 2 .* x(1,:) .* x(2,:);
a2 = r2 + 2 * x(1,:).^2;
a3 = r2 + 2 * x(2,:).^2;

delta_x = [k(3) * a1 + k(4) * a2 ;
           k(3) * a3 + k(4) * a1];

xd = xd1 + delta_x;

end

From: http://www.montefiore.ulg.ac.be/~dteney/dml.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值