数据集
https://www.microsoft.com/en-us/download/details.aspx?id=52358
示例
原图
深度图
DIIBR图像
代码实现:
clc;
% close all;
time1=clock;
addpath(genpath('D:\chengxu_matlab_project'));%调用不同文件夹里的子函数
%% 42 130 44 120
%读取CFG路径
CFG='D:\vitual_viewpoint\CFG\3d-ballet.cfg';
% CFG='D:\vitual_viewpoint\Vitual\3d-dancer - 副本.cfg';
[Width,Height,Frames,D_min,D_max,C_ParameterFile,pbnum_L,pbnum_V,pbnum_R,L_route,R_route,L_depth_route,R_depth_route,Output_Virtual]=extract_cfg(CFG);
col=Width;%1024
row=Height;%768
%读取相机参数 获取矩阵
[LMat_In_ref,LMat_Spin,LMat_Pan,LMat_Ex_ref,RMat_In_ref,RMat_Spin,RMat_Pan,RMat_Ex_ref,VMat_In_ref,VMat_Spin,VMat_Pan,VMat_Ex_ref]=Read_matrix(C_ParameterFile,pbnum_L,pbnum_V,pbnum_R);
%基线距离
Tvl=sqrt((VMat_Ex_ref(1,4)-LMat_Ex_ref(1,4))^2+(VMat_Ex_ref(2,4)-LMat_Ex_ref(2,4))^2+(VMat_Ex_ref(2,4)-LMat_Ex_ref(2,4))^2);
Tvr=sqrt((VMat_Ex_ref(1,4)-RMat_Ex_ref(1,4))^2+(VMat_Ex_ref(2,4)-RMat_Ex_ref(2,4))^2+(VMat_Ex_ref(2,4)-RMat_Ex_ref(2,4))^2);
%%
%公式推导
%参考视点:
kr_Mat=LMat_In_ref*LMat_Spin; %K*R
kt_MatKT=LMat_In_ref*LMat_Pan; %K*T
KR_V=VMat_In_ref*VMat_Spin;
KT_V=VMat_In_ref*VMat_Pan;
%%
%虚拟视点博客处理参考视点矩阵
exMat_L=zeros(3,4);
m_RotMatrix=zeros(4,4);
inMat_L=zeros(3,3);
inMat_L=LMat_In_ref;
exMat_L=LMat_Spin;
for i=1:3
exMat_L(i,4)=LMat_Pan(i,1);
end
for i=1:3
for j=1:4
m_RotMatrix(i,j)=0;
for k=1:3
m_RotMatrix(i,j)=m_RotMatrix(i,j)+inMat_L(i,k)*exMat_L(k,j);
end
end
end
m_RotMatrix(4,4)=1;
%处理虚拟视点矩阵
exMat_V=zeros(3,4);
m_RotMatrix_V=zeros(4,4);
inMat_V=zeros(3,3);
inMat_V=VMat_In_ref;
exMat_V=VMat_Spin;
for i=1:3
exMat_V(i,4)=VMat_Pan(i,1);
end
for i=1:3
for j=1:4
m_RotMatrix_V(i,j)=0;
for k=1:3
m_RotMatrix_V(i,j)=m_RotMatrix_V(i,j)+inMat_V(i,k)*exMat_V(k,j);
end
end
end
m_RotMatrix_V(4,4)=1;
%%
%初始化
page=3;
disp(['当处理的yuv视频帧数为',num2str(Frames),'帧时:']);
output_image=zeros(row,col,page);%输出合成图像初始化
output_depth_L=zeros(row,col,page);%输出深度图像初始化
flag_depth_L=zeros(row,col);%左空洞图
flag_depth_R=zeros(row,col);%右空洞图
flag_image=zeros(row,col);
output_image_L=zeros(row,col,page);%初始化左合成图
output_image_R=zeros(row,col,page);%初始化右合成图
output_depth_R=zeros(row,col,page);%初始化深度图
L_path_list=dir(strcat(L_route,'*.bmp'));
L_dep_path_list=dir(strcat(L_depth_route,'*.png'));
R_path_list=dir(strcat(R_route,'*.jpg'));
R_dep_path_list=dir(strcat(R_depth_route,'*.png'));
for f=1:Frames
img_F=L_path_list(f).name;
img=imread(strcat(L_route,img_F));
img_F=L_dep_path_list(f).name;
img_dep=imread(strcat(L_depth_route,img_F));
figure,imshow(img_dep);title('原深度图');
[output_image,flag_depth]=Transf_dep(row,col,img_dep,D_min,D_max,m_RotMatrix,m_RotMatrix_V,img);
figure,imshow(output_image);title('原深度图');
end
function [image,flag_depth]=Transf_dep(row,col,img_dep,Zmin,Zmax,m_RotMatrix,m_RotMatrix_V,img)
output_depth=zeros(row,col,1);
flag_depth=zeros(row,col);
img_1=img_dep(:,:,1);
image=zeros(row,col,3);
for i=1:row
for j=1:col
Z=1.0/((double(img_1(i,j))/255.0)*(1.0/Zmin-1.0/Zmax)+1.0/Zmax);
[X,Y]=projUVZtoXY(row,col,m_RotMatrix,j,i,Z);%第一个 X=-25.1382 Y=16.7334
[u,v,w]=projXYZtoUV(row,col,m_RotMatrix_V,X,Y,Z);%第一个 u=54.9588 v=19.9150
u=u+0.5;
v=v+0.5;
u=fix(u);
v=fix(v);
w=img_1(i,j);
% w = (255.0*(1 / w - 1.0/Zmax) / (1.0/Zmin-1.0/Zmax));
if(u>0&&u<=col-1e-6&&v>0&&v<=row-1e-6 )
output_depth(v,u,1)=w;
image(v,u,1)=img(i,j,1);
image(v,u,2)=img(i,j,2);
image(v,u,3)=img(i,j,3);
end
end
end
% 滤波
%深度图的变换图
output_depth=uint8(output_depth);
figure,imshow(output_depth);impixelinfo;
% 1
hole=made_hole_gray(output_depth);
%有滤波的深度图1
% [output_depth,hole]=smooth_hole(output_depth,hole);
% figure,imshow(output_depth);impixelinfo;
% figure,imshow(flag_depth);impixelinfo;
% caise 3*3原来 深度图的滤波大小为3*3 图像不会模糊。1
image=uint8(image);
image(:,:,1)=medfilt2(image(:,:,1),[3,3]);
image(:,:,2)=medfilt2(image(:,:,2),[3,3]);
image(:,:,3)=medfilt2(image(:,:,3),[3,3]);
end