SDM For Face Alignment 流程介绍及Matlab代码实现之预处理篇
SDM全稱為 Supervised Descent Method,是一種機(jī)器學(xué)習(xí)的方法,可以被用來做Face Alignment.
下面我們將通過matlab代碼來梳理整個(gè)實(shí)現(xiàn)的過程。
預(yù)處理階段
Input: ../data/lfpw/trainset (811張圖片)
Output: mean_shape 811張圖片的特征點(diǎn)的平均值
我們從網(wǎng)上download下訓(xùn)練數(shù)據(jù)集,包括image和ground-truth points, 我們希望可以得到所有圖片的平均特征點(diǎn),但是由于每張圖片的尺寸各異,圖片里的人臉也是各不相同,因此,只是簡簡單單將ground-truth points平均一下是沒有意義的,所以必須把他們統(tǒng)一到一個(gè)尺寸下。
我們可以提取人臉,將其放縮到400*400的尺寸下。然后通過取變換后的特征點(diǎn)的平均值來作為平均特征點(diǎn)。那么如何進(jìn)行呢?方法如下:
先正則化第一張圖片
1.取第一張圖片ground-truth points的包圍盒(即包含特征點(diǎn)的最小矩形)。
2.將包圍盒的左上角向坐標(biāo)系左上角平移包圍盒一半的寬和高,作為新的包圍盒的左上角,寬和高分別取原來的2倍。這樣裁剪出的人臉就基本上是人的正臉了,同時(shí)相應(yīng)的變換特征點(diǎn)的位置。
3.放縮上面新得到的圖片到400*400,同時(shí)相應(yīng)的變換特征點(diǎn)的位置。
這樣第一張圖片的400*400的正臉以及相應(yīng)的特征點(diǎn)就取得了。
如下圖:
再正則其他圖片
我們通過普氏分析將其他圖片的特征點(diǎn)與第一張正則化的特征點(diǎn)對(duì)齊,獲得統(tǒng)一尺寸下的特征點(diǎn),這樣就可求解平均值了。
bounding_box.m代碼,用來裁剪正臉:
normalize_first_shape.m代碼:處理第一張圖片
function [shape] = normalize_first_shape( Data, options )shape = Data.shape; image = Data.img;%補(bǔ)充項(xiàng) image=imread(image); %% calculating bounding box %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [cropmin,cropmax,offset,minshape,marginW,marginH] = bounding_box ( shape,image );%%輸出offset不為0的圖片的位置 %{if offset~=[0 0] disp('我們要找的頭像偏左或偏上的圖片已找到,地址為:'); disp(Data.img); pause; end %}%% calculate scale factor %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% W_H = cropmax - cropmin; wh1 = W_H(1); wh2 = W_H(2);CanvasSize = options.canvasSize;%標(biāo)準(zhǔn)的正臉大小scf = CanvasSize(1)/wh1; if(scf*wh2 > CanvasSize(2))scf = CanvasSize(2)/wh2; end%% croping image (for debug only) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% debug =0;if debug img = imread(Data.img);cropImage = img(cropmin(2):cropmax(2), cropmin(1):cropmax(1));scaleImage = imresize(cropImage, scf); end%% scale shape and image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%shape = shape - repmat((minshape - [marginW marginH] + offset) ..., size(shape, 1), 1); shape = shape*scf;if debug% Displaying image and feature points.figure(1);imshow(image);figure(3);imshow(scaleImage);hold on;plot(shape(:, 1), shape(:, 2), 'g*');pause; endendnormalize_rest_shape.m:依據(jù)正則化的第一張圖片特征點(diǎn)來正則化其他圖片的特征點(diǎn)。
function [shape,img] = normalize_rest_shape ( ref, data, options )cvw = options.canvasSize(1); cvh = options.canvasSize(2);base = ref.shape;shape = data.shape;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Use procrustes analysis to align shape. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [d, z, tform] = procrustes(base, shape, 'Reflection',false);%% normaling shape %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% debug =0;if debugTrans = -1/tform.b*tform.c*tform.T';Trans = Trans(1, :);transM = [1/tform.b*tform.T Trans'];cvXY = [1 cvw 1 cvw;1 1 cvh cvh];img = im2double(rgb2gray(imread(data.img)));normImg = quad2Box(img, cvXY, transM);figure(2);imshow(normImg);hold on;plot(z(:, 1), z(:, 2), 'r.');pause;endshape = z;end然后求解平均特征點(diǎn)即可。
總結(jié)
以上是生活随笔為你收集整理的SDM For Face Alignment 流程介绍及Matlab代码实现之预处理篇的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hive 如何将数组转成字符串_教你如何
- 下一篇: 【C语言进阶深度学习记录】三十八 C/