Array_forward_bucket=cell(80,140);
Array_backward_bucket=cell(80,140);
Array_forward_bird=cell(80,140);
Array_backward_bird=cell(80,140);
st.row=0;st.col=0;
for m=1:80
for n=1:140
Array_forward_bucket{m,n}=st;Array_backward_bucket{m,n}=st;Array_forward_bird{m,n}=st;Array_backward_bird{m,n}=st;
end
end
img_origin1 = imread('图像001.bmp');
img_origin = img_origin1(80:-1:1,140:-1:1);
img_origin1 = img_origin;
k1 = -0.00001374; % 形变参数,根据实际情况调整
k2 = -0.00001057;
img_size = size( img_origin );
img_undist = zeros( img_size );
img_undist = uint8( img_undist );
for l1 = 1:img_size(1) % 垂直方向
y = l1 - img_size(1)/2;
for l2 = 1:img_size(2) % 水平方向
x = l2 - img_size(2)/2;
x1 = round( x * ( 1 + k1 * x * x + k2 * y * y ) );
y1 = round( y * ( 1 + k1 * x * x + k2 * y * y ) );
y1 = y1 + img_size(1)/2;
x1 = x1 + img_size(2)/2;
img_undist(l1,l2) = img_origin(y1, x1);
Array_forward_bucket{l1,l2}.row=y1;
Array_forward_bucket{l1,l2}.col=x1;
Array_backward_bucket{y1,x1}.row= l1;
Array_backward_bucket{y1,x1}.col= l2;
end
end
figure(1);
subplot(121); imshow(img_origin);
subplot(122); imshow(img_undist);
imwrite(img_undist,'line1_tong.bmp');
A=imread('line1_tong.bmp');
h=0.330;%垂直高度
alpha=153.0*pi/180;%垂直视角
beta=123*pi/180;%水平视角
% theta=pi/2-atan(0.5/6);%垂直俯角
theta=atan(81.2*pi/180);%垂直俯角
gama=0;%水平倾角
figure;
imshow(A);
[m n]=size(A);
B=A;
after_row=80;
after_col=140;
C=zeros(after_row,after_col);
zoom2=27.9;
zoom1=48.8;%48.8
rowadd = 8;coladd=-0;
for j=1:after_row
for i=1:after_col
x=(-(j-1)+after_row+rowadd)/zoom1;
y=((i-1)-after_col/2+coladd)/zoom2;
u_inv=(x*m-m*h*tan(theta))/(2*x*tan(theta)*tan(alpha)+2*h*tan(alpha));
tan_puOg=tan(alpha)*2*u_inv/m;
tan_puOp=y/sqrt(h^2+x^2);
tan_pvOg=tan_puOp*sqrt(1+tan_puOg^2);
v_inv=tan_pvOg*n/tan(beta)/2;
Xt=-u_inv+m/2;
Yt=v_inv+n/2; sx=floor(Xt);
sy=floor(Yt);
a=Xt-sx;b=Yt-sy; %小数部分 %下面是插值算法,利用最近邻插值法
if((b<0.5))
Yt=sy;
else
Yt=sy+1;
end
if((a<0.5))
Xt=sx;
else
Xt=sx+1;
end
if Xt<0.5 || Xt>m || Yt<0.5 || Yt>n
continue;
end
C(j,i)=img_origin1(round(Xt),round(Yt));
Array_forward_bird{j,i}.row= round(Xt);
Array_forward_bird{j,i}.col= round(Yt);
end
end
figure(3);
C=uint8(C);
subplot(121);
imshow(C);
for u=1:after_row
for v=1:after_col
u_inv=m/2-u;
v_inv=v-n/2;
x=h*(m*tan(theta)+2*u_inv*tan(alpha))/(m-2*u_inv*tan(theta)*tan(alpha));
mmm= 3.55;%3.2;
y=(mmm*v_inv*tan(beta)*(x*tan(theta)+h))/(n*(tan(theta)^2+1));
Xt=after_row-x*zoom1+rowadd;
Yt=y*zoom2+after_col/2-coladd + 0.5;
sx=floor(Xt);
sy=floor(Yt);
a=Xt-sx;b=Yt-sy; %小数部分 %下面是插值算法,利用最近邻插值法
if((b<0.5))
Yt=sy;
else
Yt=sy+1;
end
if((a<0.5))
Xt=sx;
else
Xt=sx+1;
end
if Xt<0.5 || Xt>m || Yt<0.5 || Yt>n
continue;
end
D(u,v)=C(round(Xt),round(Yt));
Array_backward_bird{u,v}.row= round(Xt);
Array_backward_bird{u,v}.col= round(Yt);
end
end
D=uint8(D);figure(6);
subplot(121);imshow(img_undist);subplot(122);imshow(D);
fidout=fopen('Array_backward_bird_col.txt','w');
for s=1:80
for t=1:140
if Array_backward_bird{s,t}.col<1
fprintf(fidout,'%d,',Array_backward_bird{s,t}.col);
else
fprintf(fidout,'%d,',Array_backward_bird{s,t}.col-1);
end
end
fprintf(fidout,'\n');
end
fclose(fidout);
fidout=fopen('Array_backward_bird_row.txt','w');
for s=1:80
for t=1:140
if Array_backward_bird{s,t}.row<1
fprintf(fidout,'%d,',Array_backward_bird{s,t}.row);
else
fprintf(fidout,'%d,',Array_backward_bird{s,t}.row-1);
end
end
fprintf(fidout,'\n');
end
fclose(fidout);
fidout=fopen('Array_forward_bird_row.txt','w');
for s=1:80
for t=1:140
if Array_forward_bird{s,t}.row<1
fprintf(fidout,'%d,',Array_forward_bird{s,t}.row);
else
fprintf(fidout,'%d,',Array_forward_bird{s,t}.row-1);
end
end
fprintf(fidout,'\n');
end
fclose(fidout);
fidout=fopen('Array_forward_bird_col.txt','w');
for s=1:80
for t=1:140
if Array_forward_bird{s,t}.col<1
fprintf(fidout,'%d,',Array_forward_bird{s,t}.col);
else
fprintf(fidout,'%d,',Array_forward_bird{s,t}.col-1);
end
end
fprintf(fidout,'\n');
end
fclose(fidout);
|