Iam getting error for svm predection section please help me

1 次查看(过去 30 天)
clc
close all;
close all;
cd Database
DF=[]
for i=1:45
i
str1=int2str(i);
str2=strcat(str1,'.jpg');
nor=imread(str2);
%color
cmap=rgb2hsv(nor);
H=cmap(:,:,1);
S=cmap(:,:,2);
V=cmap(:,:,3);
Hmean=mean(mean(H));
Hst=std2(H);
Smean=mean(mean(S));
Sst=std2(S);
Vmean=mean(mean(V));
Vst=std2(V);
Hsk=sum(skewness(H));
Ssk=sum(skewness(S));
Vsk=sum(skewness(V));
Hmin=min(imhist(H));
Hmax=max(imhist(H));
Smin=min(imhist(S));
Smax=max(imhist(S));
Vmin=min(imhist(V));
Vmax=max(imhist(V));
%texture
I=rgb2gray(nor);
glcm=graycomatrix(I,'offset',[2 0;0 2]);
stats1=graycoprops(glcm,{'contrast','homogeneity'});
stats2=graycoprops(glcm,{'correlation','energy'});
t1=stats1.Contrast;
t2=stats1.Homogeneity;
t3=stats2.Correlation;
t4=stats2.Energy;
%Shape
FEAT=horzcat(1,[Hmean Hst Smean Sst Vmean Vst Hsk Vsk Ssk Hmin Hmax Smin Smax Vmin Vmax t1 t2 t3 t4]);
DF=[DF;FEAT];
end
cd ..
[fname,path]=uigetfile('.jpg','Provide currency for testing');
filename=strcat(path,fname);
nor=imread(filename);
%color
cmap=rgb2hsv(nor);
H=cmap(:,:,1);
S=cmap(:,:,2);
V=cmap(:,:,3);
Hmean=mean(mean(H));
Hst=std2(H);
Smean=mean(mean(S));
Sst=std2(S);
Vmean=mean(mean(V));
Vst=std2(V);
Hsk=sum(skewness(H));
Ssk=sum(skewness(S));
Vsk=sum(skewness(V));
Hmin=min(imhist(H));
Hmax=max(imhist(H));
Smin=min(imhist(S));
Smax=max(imhist(S));
Vmin=min(imhist(V));
Vmax=max(imhist(V));
%texture
I=rgb2gray(nor);
glcm=graycomatrix(I,'offset',[2 0;0 2]);
stats1=graycoprops(glcm,{'contrast','homogeneity'});
stats2=graycoprops(glcm,{'correlation','energy'});
t1=stats1.Contrast;
t2=stats1.Homogeneity;
t3=stats2.Correlation;
t4=stats2.Energy;
%Shape
QF=horzcat(1,[Hmean Hst Smean Sst Vmean Vst Hsk Vsk Ssk Hmin Hmax Smin Smax Vmin Vmax t1 t2 t3 t4]);
%Multi svm
Trainigset=[DF(1,:);DF(2,:);DF(3,:);DF(4,:);DF(5,:);DF(6,:);DF(7,:);DF(8,:);DF(9,:);DF(10,:);DF(11,:);DF(12,:);DF(13,:);DF(14,:);DF(15,:);DF(16,:);DF(17,:);DF(18,:);DF(19,:);DF(20,:);DF(21,:);DF(22,:);DF(23,:);DF(24,:);DF(25,:);DF(26,:);DF(27,:);DF(28,:);DF(29,:);DF(30,:);DF(31,:);DF(32,:);DF(33,:);DF(34,:);DF(35,:);DF(36,:);DF(37,:);DF(38,:);DF(39,:);DF(40,:);DF(41,:);DF(42,:);DF(43,:);DF(44,:);DF(45,:)];
GroupTrain={'1' '1' '1' '1' '1' '1' '1' '1' '1' '2' '2' '2' '2' '2' '2' '2' '2' '2' '3' '3' '3' '3' '3' '3' '3' '3' '3' '4' '4' '4' '4' '4' '4' '4' '4' '4' '5' '5' '5' '5' '5' '5' '5' '5' '5' };
TestSet=QF;
SVMModels=cell(5,1);
y=GroupTrain
classes=unique(y);
rng(1);
for j=1:numel(classes)
indx=strcmp(y',classes(j));
SVMMOdels{j}=fitcsvm(DF,indx,'ClassNames',[false true],'Standardize',true,'KernelFunction','rbf','BoxConstraint',1);
end
xGrid=QF;
for j=1:numel(classes)
[~,score]=predict(SVMModels{j},xGrid)
Scores(:,j)=score(:,2);
end
[~,maxScore]=max(score,[],2);
result=maxScore;
figure.imshow(nor)
title('input')
if result==1
msgbox('10')
elseif result==2
msgbox('20')
elseif result==3
msgbox('50')
elseif result==4
msgbox('100')
elseif result==5
msgbox('500')
end

回答(1 个)

Omega
Omega 2024-10-22
Hi Savita,
The error you're encountering is likely due to a typo in your code. You have defined your SVM models using "SVMMOdels" instead of "SVMModels". Therefore, when you try to use "SVMModels" in the prediction section, it's empty, causing the error.
Here's a quick fix: Change "SVMMOdels" to "SVMModels" in the training loop. Here's the corrected section of your code:
for j = 1:numel(classes)
indx = strcmp(y', classes(j));
SVMModels{j} = fitcsvm(DF, indx, 'ClassNames', [false true], 'Standardize', true, 'KernelFunction', 'rbf', 'BoxConstraint', 1);
end
Make sure you update the loop where you're creating the SVM models. After this change, your "predict" function should work correctly.

类别

Help CenterFile Exchange 中查找有关 Feature Detection and Extraction 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by