clc;
clear;
nin = 10;
ratio = 1.2;
fprintf('TrainSampleMatData production BEGIN...\n');
posTrainSampleImageDirectory = 'C:\Users\Administrator\Desktop\hog+svm_行人检测matlab程序\hog+svm 行人检测matlab程序\posdata';
negTrainSampleImageDirectory = 'C:\Users\Administrator\Desktop\hog+svm_行人检测matlab程序\hog+svm 行人检测matlab程序\negdata';
pathPosName = [posTrainSampleImageDirectory, '/*.png']; % '/*.png'是什么意思呢!有什么用?
readPosList = dir(pathPosName);
[mpos, n] = size(readPosList);
pathNegName1 = [negTrainSampleImageDirectory, '/*.jpg']; % 这一句和下一句又是什么意思呢!
pathNegName2 = [negTrainSampleImageDirectory, '/*.png'];
readNegList1 = dir(pathNegName1);
readNegList2 = dir(pathNegName2);
readNegList = [readNegList1; readNegList2];
[mneg, n] = size(readNegList);
hog_train_data = zeros(mpos+nin*mneg, 3780); %下边四句没怎么看懂!为什么neg的数量要乘以nin?
hog_train_label = zeros(mpos+nin*mneg,1);
hog_train_label(1:mpos,:) = 1;
hog_train_label(mpos+1:mpos+nin*mneg,:) = -1;
fprintf('total PosTrainSampleImages number: %d\n', mpos);
%训练正样本
for i=1:mpos
fprintf('generating NO.%d Pos image HOG feature...', i);
imageName = readPosList(i,1).name; %.name是什么啊?
pathPosName = [posTrainSampleImageDirectory, '/', imageName];
image = imread(pathPosName);
% image = image(17:64,17:128,:); %这句是什么意思呢?
hog_train_data(i,:) = hogcalculator(image);
fprintf(' finished\n');
end
maxScaleNum=8;
fprintf('total NegTrainSampleImages number: %d\n', mneg);
%训练负样本
w = 64;
h = 128;
k = 0;
for i=1:mneg
imageName = readNegList(i,1).name;
pathNegName = [negTrainSampleImageDirectory, '/', imageName];
image = imread(pathNegName);
[imgHeight,imgWidth, imgnChannels] = size(image);
newMaxScaleNum = min(floor(log(imgHeight/h)/(log(ratio))),floor(log(imgWidth/w)/log(ratio))); %这是在做什么呢?
if newMaxScaleNum > maxScaleNum
newMaxScaleNum = maxScaleNum;
end
scale_num = 1+(newMaxScaleNum-1)*rand();
image = imresize(image,1/(ratio^scale_num));
[imgHeight,imgWidth, imgnChannels] = size(image);
xmax = imgWidth - w + 1;
ymax = imgHeight - h + 1;
for j = 1:nin
k = k+1;
fprintf('generating NO.%d sample HOG feature in NO.%d Neg image...', j, i);
x = floor(rand(1)*xmax); %这几句是在干什么啊?
y = floor(rand(1)*ymax);
if x == 0
x = x+1;
end
if y == 0
y = y+1;
end
sampleImage = image(y:(y+h-1),x:(x+w-1),:); %sampleImage = image(y:(y+h-1),x:(x+w-1),:);是什么意思呢?为什么要这么做呢
hog_train_data(mpos+k,:) = hogcalculator(image);
fprintf(' finished\n');
end
end
save HOG_TRAIN_DATA.mat hog_train_data;
save HOG_TRAIN_LABEL.mat hog_train_label;
clear;
fprintf('TrainSampleMatData production END...\n');