天天看點

matlab人臉重建,3D人臉重建: BFM結合表情模型

MATLAB代碼:

% addpath(genpath(pwd))

% --> model

% 載入原始的BFM模型

load('raw/01_MorphableModel.mat');

% 載入3dffa中 BFM資訊

load('3ddfa/model_info.mat');

% BFM 處理後 共有 53490個點

% trimIndex: 53215x1, trimIndex_f: 53215*3

% 點的索引, 次元是53251,對應BFM中的 53490個點

trimIndex_f = [trimIndex*3-2, 3*trimIndex-1, 3*trimIndex]';

trimIndex_f = trimIndex_f(:); % 次元 159645 * 1

model.shapeMU = shapeMU(trimIndex_f, :); % 平均形狀向量 159645 * 1

model.shapePC = shapePC(trimIndex_f, :); % 形狀PCA降維矩陣 159645 * 199

model.shapeEV = shapeEV; % 形狀正交空間的系數; 199*1

model.texMU = texMU(trimIndex_f, :); % 平均紋理向量159645 * 1

model.texPC = texPC(trimIndex_f, :); % 紋理PCA降維矩陣 159645 * 199

model.texEV = texEV; % 紋理正交空間的系數 199*1

model.tri = tri; % 三角格坐标, 次元 105840 * 3

model.kpt_ind = keypoints; % 68個特征點

% 存儲相關模型資訊

model_info.kpt_ind = keypoints;

model_info.trimIndex = trimIndex;

model_info.symlist = symlist; % 對稱點資訊 大小為 2*26720

model_info.symlist_tri = symlist_tri; % 對稱網格資訊, 大小 2*52937

model_info.segbin = segbin(trimIndex, :)'; % 獲得掩模

model_info.segbin_tri = segbin_tri'; % 獲得三角格掩模

% 2 載入表情模型

load('3ddfa/Model_Expression.mat');

model.expMU = mu_exp; % 平均表情向量 159645 * 1

model.expPC = w_exp; % 表情PCA降維矩陣 159645 * 29

model.expEV = sigma_exp; % 表情系數 29*1

% 159645 / 3 = 53215, 不夠 53490 個點, 缺少 275 點

% 3 加載嘴唇三角格資訊

load('3ddfa/Model_tri_mouth');

model.tri_mouth = tri_mouth; % 嘴唇坐标, 3*107

model_info.tri_mouth = tri_mouth;

% 4. 載入 臉部輪廓資訊

load('3ddfa/Model_face_contour_trimed.mat');

model_info.face_contour = face_contour; % 人臉輪廓點索引, 次元28

model_info.face_contour_line = face_contour_line; % 人臉輪廓線索引 512

model_info.face_contour_front = face_contour_front; % 正面人臉輪廓線索引 28

model_info.face_contour_front_line = face_contour_front_line; % 正面人臉輪廓線索引 512

% 5 載入鼻子空洞

load('3ddfa/Modelplus_nose_hole.mat');

model_info.nose_hole = nose_hole; % 鼻子索引 142

model_info.nose_hole_right = nose_hole_right; % 右側鼻子索引 71

model_info.nose_hole_left = nose_hole_left; % 左側鼻子索引 71

% 6 parallel for key points

load('3ddfa/Modelplus_parallel.mat');

model_info.parallel = parallel;

model_info.parallel_face_contour = parallel_face_contour;

% 7. PNCC

% 從 提前 計算好的 vertex_code.mat 得到PNCC特征

copyfile('3ddfa/vertex_code.mat', 'Out/pncc_code.mat');

% 載入UV坐标, 計算方法來自 3DMMasSTN論文

load('stn/BFM_UV.mat');

uv_coords = UV(trimIndex, :)'; % 2*53215

% 修改不好的頂點

bad_ind = [10032, 10155, 10280];

round1 = [10033, 10158 ];

round2 = [10534, 10157, 10661];

round3 = [10916, 10286];

uv_coords(:, bad_ind(1)) = mean(uv_coords(:, round1), 2);

uv_coords(:, bad_ind(2)) = mean(uv_coords(:, round2), 2);

uv_coords(:, bad_ind(3)) = mean(uv_coords(:, round3), 2);

model_info.uv_coords = uv_coords';

UV = model_info.uv_coords;

% 添加嘴唇三角網絡

tm_inner = model.tri_mouth;

tm_inner_add =[6420 6542 6664; %% add inner mouth triangles

6420 6294 6167;

6167 6297 6420;

6167 6297 6296;

6167 6296 6295;

6167 6295 6039;

6168 6295 6039];

ind_bad = 38;

all_ind = 1:size(tm_inner, 2);

tm_inner = tm_inner(:, setdiff(all_ind, bad_ind));

tm_inner = [tm_inner tm_inner_add'];

model_info.tri_mouth = tm_inner;

model.tri_mouth = tm_inner;

% save

clearvars -except model model_info UV

save 'Out/BFM.mat' model

save 'Out/BFM_info.mat' model_info

save 'Out/BFM_UV.mat' UV

% copyfile('3ddfa/pncc_code.mat', 'Out/pncc_code.mat')

對應檔案:  此處  密碼: b8la