注意: loadsamples的第二個參數(shù)是2,表明需要進行縮放,但是測試時,并沒有進行縮放,參數(shù)為1. To do list:本函數(shù)使用的真實特征點的包圍盒截取的圖像,在實際作用時,肯定不能去真實特征點截取圖像了。我們應(yīng)該改為人臉檢測框代替這個真實特征點的包圍盒,如果檢測不到,再用真實特征點的包圍盒替代.
functionData = loadsamples(imgpathlistfile, exc_setlabel)%LOADSAMPLES Summary of this function goes here% Function: load samples from dbname database% Detailed explanation goes here% Input: % dbname: the name of one database% exc_setlabel: excluded set label% Output:% Data: loaded data from the database% 基本步驟:% 1. 載入圖片,取ground_truth shape的包圍盒,然后放大一倍,截取圖像。% 同時相應(yīng)地變換shape.% 2. 為了防止圖片過大,我們把圖像控制在150*150內(nèi).% 3. 用matlab自帶的人臉檢測求解或者直接拿bbox代替bbox_facedet% 4. 后面還為了防止train文件下夾雜了test的圖片,做了排除處理.%imgpathlist = textread(imgpathlistfile, '%s', 'delimiter', '\n');Data = cell(length(imgpathlist), 1);setnames = {'train''test'};% Create a cascade detector object.% faceDetector = vision.CascadeObjectDetector();% bboxes_facedet = zeros(length(imgpathlist), 4);% bboxes_gt = zeros(length(imgpathlist), 4);% isdetected = zeros(length(imgpathlist), 1);parfori = 1:length(imgpathlist)img = im2uint8(imread(imgpathlist{i}));Data{i}.width_orig = size(img, 2);Data{i}.height_orig = size(img, 1);% Data{i}.img = img% shapepath = strrep(imgpathlist{i}, 'png', 'pts');%這一句和下一句是一樣的用途shapepath = strcat(imgpathlist{i}(1:end-3), 'pts');Data{i}.shape_gt = double(loadshape(shapepath)); % Data{i}.shape_gt = Data{i}.shape_gt(params.ind_usedpts, :);% bbox = bounding_boxes_allsamples{i}.bb_detector; %Data{i}.bbox_gt = getbbox(Data{i}.shape_gt); % [bbox(1) bbox(2) bbox(3)-bbox(1) bbox(4)-bbox(2)]; %shape的包圍盒% cut original image to a region which is a bit larger than the face% bounding boxregion = enlargingbbox(Data{i}.bbox_gt, 2.0); %將true_shape的包圍盒放大一倍,形成更大的包圍盒,以此裁剪人臉,也有通過人臉檢測框放大的region(2) = double(max(region(2), 1)); %防止放大后的region超過圖像,這樣一旦超過坐標(biāo)會變成負(fù)數(shù)。因此取了和1的最大值.region(1) = double(max(region(1), 1));bottom_y = double(min(region(2) + region(4) - 1, Data{i}.height_orig)); %同理防止過高,過寬right_x = double(min(region(1) + region(3) - 1, Data{i}.width_orig));img_region = img(region(2):bottom_y, region(1):right_x, :);Data{i}.shape_gt = bsxfun(@minus, Data{i}.shape_gt, double([region(1) region(2)])); %68*2的矩陣減去一個向量% to save memory cost during trainingif exc_setlabel == 2ratio = min(1, sqrt(single(150 * 150) / single(size(img_region, 1) * size(img_region, 2)))); %如果圖像小于150*150,則不用縮放,否則縮放到150*150以內(nèi)img_region = imresize(img_region, ratio);Data{i}.shape_gt = Data{i}.shape_gt .* ratio;end Data{i}.ori_img=img;Data{i}.lefttop=[region(1) region(2)]; %自己補充的Data{i}.bbox_gt = getbbox(Data{i}.shape_gt);Data{i}.bbox_facedet = getbbox(Data{i}.shape_gt); %應(yīng)該改為人臉檢測框% perform face detection using matlab face detector%{bbox = step(faceDetector, img_region);ifisempty(bbox)% if face detection is failed isdetected(i) = 1;Data{i}.bbox_facedet = getbbox(Data{i}.shape_gt);elseint_ratios = zeros(1, size(bbox, 1));for b = 1:size(bbox, 1)area = rectint(Data{i}.bbox_gt, bbox(b, :));int_ratios(b) = (area)/(bbox(b, 3)*bbox(b, 4) + Data{i}.bbox_gt(3)*Data{i}.bbox_gt(4) - area); end[max_ratio, max_ind] = max(int_ratios);if max_ratio < 0.4% detection failisdetected(i) = 0;elseData{i}.bbox_facedet = bbox(max_ind, 1:4);isdetected(i) = 1;% imgOut = insertObjectAnnotation(img_region,'rectangle',Data{i}.bbox_facedet,'Face');% imshow(imgOut);endend%}% recalculate the location of groundtruth shape and bounding box% Data{i}.shape_gt = bsxfun(@minus, Data{i}.shape_gt, double([region(1) region(2)]));% Data{i}.bbox_gt = getbbox(Data{i}.shape_gt);ifsize(img_region, 3) == 1Data{i}.img_gray = img_region;else% hsv = rgb2hsv(img_region);Data{i}.img_gray = rgb2gray(img_region);end Data{i}.width = size(img_region, 2);Data{i}.height = size(img_region, 1);
endind_valid = ones(1, length(imgpathlist));
parfori = 1:length(imgpathlist)if ~isempty(exc_setlabel)ind = strfind(imgpathlist{i}, setnames{exc_setlabel}); %strfind是找imgpathlist{i}中含有setnames{exc_setlabel}='test'的地址if ~isempty(ind) % | ~isdetected(i)ind_valid(i) = 0;endendend% learn the linear transformation from detected bboxes to groundtruth bboxes% bboxes = [bboxes_gt bboxes_facedet];% bboxes = bboxes(ind_valid == 1, :);Data = Data(ind_valid == 1); %找到含有test的地址,并排除出去,保證Data都是train_dataendfunctionshape = loadshape(path)% function: load shape from pts file
file = fopen(path);if ~isempty(strfind(path, 'COFW'))shape = textscan(file, '%d16 %d16 %d8', 'HeaderLines', 3, 'CollectOutput', 3);
elseshape = textscan(file, '%d16 %d16', 'HeaderLines', 3, 'CollectOutput', 2);
end
fclose(file);shape = shape{1};
endfunctionregion = enlargingbbox(bbox, scale)region(1) = floor(bbox(1) - (scale - 1)/2*bbox(3));
region(2) = floor(bbox(2) - (scale - 1)/2*bbox(4));region(3) = floor(scale*bbox(3));
region(4) = floor(scale*bbox(4));% region.right_x = floor(region.left_x + region.width - 1);% region.bottom_y = floor(region.top_y + region.height - 1);end