file_path = 'F:\\test\';% 图像文件夹路径
img_path_list = dir(strcat(file_path,'*.png'));%获取该文件夹中所有.jpg格式的图像
img_num = length(img_path_list);%获取图像总数
if img_num > 0 %有满足条件的图像
for pn = 1:img_num %逐一读取图像
image_name = img_path_list(pn).name;% 图像名
img_origin = imread(strcat(file_path,image_name));%读取图像
fprintf('%d %s\n',pn,strcat(file_path,image_name));% 显示正在处理的图像名
S = distortion002(imread(strcat(file_path,image_name)));
imwrite(S,image_name);
%%此处添加具体的图像处理程序
end
end
function I = distortion002(Idistorted)
%clear;
A =[639 0 320.5;
0 399 200.5;
0 0 1];
fx = A(1,1);
fy = A(2,2);
cx = A(1,3);
cy = A(2,3);
K = A;
%Idistorted = imread('4946978_left.png');
%Idistorted = rgb2gray(Idistorted);
Idistorted = im2double(Idistorted);
I = zeros(size(Idistorted));
[i ,j] = find(~isnan(I));
% Xp = the xyz vals of points on the z plane
Xp = (K)\[j i ones(length(i),1)]';
% Now we calculate how those points distort i.e forward map them through the distortion
%r2 = Xp(1,:).^2+Xp(2,:).^2;
x = Xp(1,:);
y = Xp(2,:);
theta = deg2rad(12.5);%X轴旋转角度
focal = 446 ; %相机内参,焦距
Ph = 400 ; %相机内参,画幅height
alpha = atan(2*focal/Ph);
beta = alpha - theta;
h2 = sin(alpha)*Ph/sin(beta);
deltaS = sin(theta)*Ph/sin(beta);
S = (Ph/2)/cos(alpha);
aa = (S+deltaS)/S;
x1=zeros(400,1);
for m=1:400
x1(m,1)=(m-1)/399;
end
t=aa-1;
for m= 1 : 400 %列1-21,x - → 竖线
for n= 1 : 640 %hang 1-21,x + ←
x(400*(n-1)+m)=x(400*(n-1)+m)/(1+t*x1(m)) ;
end
end
bb=h2/Ph;
y=y/bb;%y方向缩放
%x = x.*(1+k1*r2 + k2*r2.^2 + k3*r2.^3) + 2*p1.*x.*y + p2*(r2 + 2*x.^2);
%y = y.*(1+k1*r2 + k2*r2.^2 + k3*r2.^3) + 2*p2.*x.*y + p1*(r2 + 2*y.^2);
% u and v are now the distorted cooridnates
u = reshape(fx*x + cx,size(I));
v = reshape(fy*y + cy,size(I));
% Now we perform a backward mapping in order to undistort the warped image coordinates
I = interp2(Idistorted, u, v);
subplot(121); imshow(Idistorted);
subplot(122); imshow(I);
%imwrite(I,'I001.png');
end