【发布时间】:2015-04-03 13:24:00
【问题描述】:
我正在使用 svmtrain 函数对图像进行分类。我遇到了这样的错误。
Error using svmtrain (line 253)
Y and TRAINING must have the same number of rows.
Error in svm5 (line 80)
SVMStruct = svmtrain(Training_Set , train_label, 'kernel_function', 'linear');
Training_Set 包含图像集,而 train_lable 是用于识别输入图像的类。 参考完整代码
clc
clear all
% Load Datasets
Dataset = 'D:\majorproject\image\traindata\';
Testset = 'D:\majorproject\image\testset\';
% we need to process the images first.
% Convert your images into grayscale
% Resize the images
width=100; height=100;
DataSet = cell([], 1);
for i=1:length(dir(fullfile(Dataset,'*.jpg')))
% Training set process
k = dir(fullfile(Dataset,'*.jpg'));
k = {k(~[k.isdir]).name};
for j=1:length(k)
tempImage = imread(horzcat(Dataset,filesep,k{j}));
imgInfo = imfinfo(horzcat(Dataset,filesep,k{j}));
% Image transformation
if strcmp(imgInfo.ColorType,'grayscale')
% array of images
DataSet{j} = double(imresize(tempImage,[width height]));
else
% array of images
DataSet{j} = double(imresize((tempImage),[width height]));
end
end
end
TestSet = cell([], 1);
for i=1:length(dir(fullfile(Testset,'*.jpg')))
% Training set process
k = dir(fullfile(Testset,'*.jpg'));
k = {k(~[k.isdir]).name};
for j=1:length(k)
tempImage = imread(horzcat(Testset,filesep,k{j}));
imgInfo = imfinfo(horzcat(Testset,filesep,k{j}));
% Image transformation
if strcmp(imgInfo.ColorType,'grayscale')
% array of images
TestSet{j} = double(imresize(tempImage,[width height]));
else
% array of images
TestSet{j} = double(imresize(tempImage,[width height]));
end
end
end
% Prepare class label for first run of svm
% I have arranged labels 1 & 2 as per my convenience.
% It is always better to label your images numerically
% Note that for every image in our Dataset we need to provide one label.
% we have 10 images and we divided it into two label groups here.
train_label = zeros(size(10,1),1);
train_label(1:4,1) = 1; % 1 = naa
train_label(5:10,1) = 2; % 2 = ta
% Prepare numeric matrix for svmtrain
Training_Set=[];
for i=1:length(DataSet)
b = imresize(DataSet{i},[100 100]);
Training_Set_tmp= b(:);
%Training_Set_tmp = reshape(DataSet{i},1, 100*100);
Training_Set=[Training_Set;Training_Set_tmp];
end
Test_Set=[];
for j=1:length(TestSet)
b = imresize(TestSet{j},[100 100]);
Test_set_tmp= b(:);
%Test_set_tmp = reshape(TestSet{j},1, 100*100);
Test_Set=[Test_Set;Test_set_tmp];
end
% Perform first run of svm
SVMStruct = svmtrain(Training_Set, train_label, 'kernel_function', 'linear');
Group = svmclassify(SVMStruct, Test_Set);
请指导我克服这一点。 谢谢。
【问题讨论】:
-
您能否提供有关 Training_set 的更多信息?如果它是 PxPx3 RBG 图像的集合,我真的不认为这是您想要使用 svmtrain 的方式。在大多数情况下,您会从使用 PCA 之类的降维中受益,然后训练集将是 NxM,其中 N 是图像的数量,M 是减少的特征数量,因此 train_label 应该是 Nx1 或类似的东西。跨度>
-
Training_Set=[];对于 i=1:length(DataSet) b = imresize(DataSet{i},[100 100]); Training_Set_tmp= b(:); %Training_Set_tmp = reshape(DataSet{i},1, 100*100); Training_Set=[Training_Set;Training_Set_tmp];这就是我获取训练数据的方式
-
检查
size(Training_Set,1)是否与length(train_label)相同。 -
您能否将评论中的代码放入问题中以获得完整的示例,显示问题的可运行代码?
-
@user3859472 您必须编辑您的问题并将该代码添加到其中(请正确格式化)。评论没用。
标签: matlab image-processing svm