我是图像处理的初学者,我正在使用MATLAB从图像中提取HOG特征来训练SVM分类器。训练图像的大小为480 * 640像素,我使用内置MATLAB extractHOGFeatures功能的默认设置获得167796个功能。但是,当我测试模型时,它给我的功能较少(仅216个功能!),因为他们知道测试图像具有相同的训练图像大小。我在MATLAB中遇到这个错误“TEST和训练数据中的列数必须相等”。
您是否有任何线索如何解决此问题并获得与训练和测试集相同大小的特征向量?
这是代码,
[fpos,fneg] = featuress(pathPos, pathNeg);
%train SVM
HOG_featV = loadingV(fpos,fneg); % loading and labeling each training example
%% Detection
tSize = [24 32];
testImPath = '.\face_detection\dataset\bikes_and_persons2\';
imlist = dir([testImPath '*.bmp']);
for j = 1:length(imlist)
disp ('inside for loop');
img = imread([testImPath imlist(j).name]);
axis equal; axis tight; axis off;
imshow(img); hold on;
detect(img,model,tSize);
%% training
function [fpos, fneg] = featuress(pathPos,pathNeg)
% extract features for positive examples
imlist = dir([pathPos '*.bmp']);
for i = 1:length(imlist)
im = imread([pathPos imlist(i).name]);
fpos{i} = extractHOGFeatures(double(im));
end
% extract features for negative examples
imlist = dir([pathNeg '*.bmp']);
for i = 1:length(imlist)
im = imread([pathNeg imlist(i).name]);
fneg{i} = extractHOGFeatures(double(im));
end
end
%% testing function
function detect(im,model,wSize)
topLeftRow = 1;
topLeftCol = 1;
[bottomRightCol bottomRightRow d] = size(im);
fcount = 1;
for y = topLeftCol:bottomRightCol-wSize(2)
for x = topLeftRow:bottomRightRow-wSize(1)
p1 = [x,y];
p2 = [x+(wSize(1)-1), y+(wSize(2)-1)];
po = [p1; p2];
img = imcut(po,im);
featureVector{fcount} = extractHOGFeatures(double(img));
boxPoint{fcount} = [x,y];
fcount = fcount+1;
x = x+1;
end
end
lebel = ones(length(featureVector),1);
P = cell2mat(featureVector');
% each row of P' correspond to a window
[ predictions] = svmclassify(model, P); % classifying each window
[a, indx]= max(predictions);
bBox = cell2mat(boxPoint(indx));
rectangle('Position',[bBox(1),bBox(2),24,32],'LineWidth',1, 'EdgeColor','r');
end
提前致谢。
答案 0 :(得分:0)
P
的大小是多少?是167796 x 216吗?如果是这样,那么当您致电featureVector
时,不应转置cell2mat
。或者您应该在使用之前转置P
。您还可以将featureVector
设为矩阵而不是单元格数组。既然您知道HOG矢量的长度是167796并且您知道有多少图像,则可以预先预先分配它,并填写行。