再用matlab 来试试转换
思路:
先把图像按下图分割,中间三块分别是:左、前、右(块),两边合成 :后(块)
上面5块合成:上(块),下面合成:下(块),其中上下两边块合成 1块,然后再这4块转换成三角形,再把4个三角拼成一个正方形 成顶、底(块)
读入图像:
%全景图转换为天空盒图
clear;close all;clc;
name='sh.jpg';
im=imread(name);
im=imresize(im,[1000 2000]);%缩放成一个标准大小
简单分割左、前、右、后:
%左
l=w/8+1;r=w*(1/4+1/8);t=h/4;b=h*3/4;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
le=im(t:b,l:r,:);
le=uint8(le);
figure(2);imshow(le);title('左图');
imwrite(le,'左.jpg');
%前
l=w*(1/4+1/8)+1;r=w*(2/4+1/8);t=h/4;b=h*3/4;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
fr=im(t:b,l:r,:);
fr=uint8(fr);
figure(3);imshow(fr);title('前图');
imwrite(fr,'前.jpg');
%右
l=w*(2/4+1/8)+1;r=w*(3/4+1/8);t=h/4;b=h*3/4;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
ri=im(t:b,l:r,:);
ri=uint8(ri);
figure(4);imshow(ri);title('右图');
imwrite(ri,'右.jpg');
%后图两半拼成
l=w*(3/4+1/8)+1;r=w;t=h/4;b=h*3/4;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
ba1=im(t:b,l:r,:);
l=1;r=w/8;t=h/4;b=h*3/4;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
ba2=im(t:b,l:r,:);
ba=[ba1,ba2];
ba=uint8(ba);
figure(5);imshow(ba);title('后图');
imwrite(ba,'后.jpg');
%上
l=w/8+1;r=w;t=1;b=h/4;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
le1=im(t:b,l:r,:);
l=1;r=w/8;t=1;b=h/4;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
le2=im(t:b,l:r,:);
le=[le1,le2];
to=Top_bottom(le);
figure(2);imshow(to);title('顶图');
imwrite(to,'顶.jpg');
%下
l=w/8+1;r=w;t=1+h*3/4;b=h;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
le1=im(t:b,l:r,:);
l=1;r=w/8;t=1+h*3/4;b=h;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
le2=im(t:b,l:r,:);
le=[le1,le2];
to=Top_bottom(le);
figure(3);imshow(to);title('底图');
imwrite(to,'底.jpg');
怪怪的两个图
用到的函数:
%合成顶图和底图
function to=Top_bottom(im)
im=double(im);
%figure(1),imshow(im);title('原图');
[ h, w, v ] = size(im);
%上左
%1/4
l=1;r=w/4;t=1;b=h;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
le=im(t:b,l:r,:);
%figure(2);imshow(uint8(le));title('左图');
le1=Four2Three(le);%四边形图像转换为三角形
%figure(3);imshow(uint8(le1));title('三角左图');
disp('完成当前4分之1');
%2/4
l=1+w/4;r=w*2/4;t=1;b=h;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
le=im(t:b,l:r,:);
%figure(2);imshow(uint8(le));title('左图');
le2=Four2Three(le);
%figure(4);imshow(uint8(le2));title('三角左图');
disp('完成当前4分之2');
%3/4
l=1+w*2/4;r=w*3/4;t=1;b=h;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
le=im(t:b,l:r,:);
%figure(2);imshow(uint8(le));title('左图');
le3=Four2Three(le);
%figure(5);imshow(uint8(le3));title('三角左图');
disp('完成当前4分之3');
%4/4
l=1+w*3/4;r=w;t=1;b=h;
l=fix(l);r=fix(r);t=fix(t);b=fix(b);
le=im(t:b,l:r,:);
%figure(2);imshow(uint8(le));title('左图');
le4=Four2Three(le);
%figure(6);imshow(uint8(le4));title('三角左图');
disp('完成当前4分之4');
%合成
le1=imrotate(le1, 270);
le3=imrotate(le3, 90);
le4=imrotate(le4, 180);
to1=[le1,le3];
%figure(7);imshow(uint8(to1));title('三角左图');
to2=[le4;le2];
%figure(8);imshow(uint8(to2));title('三角左图');
to=imadd(to1,to2);
to=uint8(to);
%figure(9);imshow(to);title('三角左图');
%四边形转换为三角形,竖直线一头动(下端不动,上端移到中点)
function I=Four2Three(im)
%左半向右压缩
im1=Four2ThreeBai(im);
%figure(3);imshow(uint8(im1));title('三角左图');
%右半向左压缩(水平镜像)
im2=flipdim(im,2);%原图像的水平镜像(mirror)
im2=Four2ThreeBai(im2);
im2=flipdim(im2,2);%原图像的水平镜像
%figure(4);imshow(uint8(im2));title('三角左图');
I=[im1,im2];
function im1=Four2ThreeBai(im) %半边
[ h, w, v ] = size(im);
im1=zeros(h,fix(w/2),v);
for i=1:h
for j=1:fix(w/2)
%向右压缩
x=(w/2-i)*(h-j)/h+j;
x=fix(x);%+2 消除白线
if x>0 & x<=fix(w/2)
im1(i,x,:)=im(i,j,:);
else
t=x-w/2
end
end
end
显然这次转换又失败了,还得继续努力!