图像畸变矫正算法实现 matlab版

 

真正的相机镜头不理想,并在图像中引入一些失真。 为了解释这些非理想性,有必要在透视投影的方程中添加失真模型。

一、原图如下:

二、实现的效果图

三、算法具体实现

function undistorted_img = undistortImage(img, K, D)
% Corrects an image for lens distortion.
% K为内参矩阵,用来归一化坐标的,给定的
% D为径向畸变参数,给定的

[height, width] = size(img);

fx = K(1,1);
fy = K(2,2);
cx = K(1,3);
cy = K(2,3);


%Finish image undistorted function
undistorted_img = uint8(zeros(height, width));

% 依次找到去畸变后的图像坐标(y,x)对应的畸变坐标
for y = 1:height
    for x = 1:width

        % 第一步,通过内参矩阵归一化畸变图像的坐标       
        x1 = (x-cx)/fx;  
        y1 = (y-cy)/fy;
        
        % 第二步,通过径向畸变模型得到归一化的畸变坐标
        r2 = x1^2+y1^2;
        x2  = x1*(1+D(1)*r2+D(2)*r2^2); 
        y2 = y1*(1+D(1)*r2+D(2)*r2^2);
        
        % 第三步,坐标映射回去,去归一化,得到畸变坐标。
        u_distorted = fx*x2+cx;  % 列
        v_distorted = fy*y2+cy;  % 行
        
        % 通过上面的步骤,我们找到了正常图像坐标(y,x)对应的畸变图像坐标(u_distorted, v_distorted),下一步就是赋值了,这里用的是最近邻插值法

        % 第四步,最近邻插值
        if (u_distorted >= 0 && v_distorted >= 0 && u_distorted < width && v_distorted < height) % 防止行列越界
            undistorted_img(y, x) = img(round(v_distorted), round(u_distorted)); % 通过round函数取最近的数字
        else
            undistorted_img(y, x) = 0;
        end
    end
end

end

注:

(1)内参矩阵K

(2)径向畸变模型参数D:

 

  • 33
    点赞
  • 253
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值