Classifying new image after PCA in SVM
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Warid Islam
il 16 Giu 2020
Commentato: Warid Islam
il 29 Giu 2020
I have 32 breast tissue images where 16 are benign and 16 are malignant. I have applied PCA for dimension reduction. I have applied SVM for classification. Problem arises when I try to find the label of a new input image. I am getting an error message. Please find my code and error message below.
clc;clear;close all
%% Getting Image
for k = 1 : 32
jpgFileName = strcat('B', num2str(k), '.jpg');
if isfile(jpgFileName)
i = imread(jpgFileName);
else
fprintf('File %s does not exist.\n', jpgFileName);
end
% if image is rgb
try
i=rgb2gray(i);
end
%% Crop The Breast
z=im2bw(i,0.1);
figure(2)
imshow(z);title('Original B&W')
info=regionprops(z);
a=cat(1,info.Area);
[m,l]=max(a);
X=info(l).Centroid;
bw2=bwselect(z,X(1),X(2),8);
i=immultiply(i,bw2);
figure(3)
imshow(i);
title('Getting the Breast and Muscle')
%% Deleting Black Ground
% We will delete the black corners
% So that we can select the muscle
% using bwselect
% convert to B&W first time
[x,y]=size(z);
tst1=zeros(x,y);
% detect empty rows
r1=[];
m=1;
for j=1:x
if z(j,:)==tst1(j,:)
r1(m)=j;
m=m+1;
end
end
% detect empty columns
r2=[];
m=1;
for j=1:y
if z(:,j)==tst1(:,j)
r2(m)=j;
m=m+1;
end
end
% Deleting
i(:,r2)=[];
i(r1,:)=[];
figure(4)
imshow(i);title('after deleting background');
%% Deleting the Muscle
if i(1,1)~=0
c=3;
r=3;
else
r=3;
c=size(i,2)-3;
end
z2=im2bw(i,0.5);
bw3=bwselect(z2,c,r,8);
bw3=~bw3;
ratio=min(sum(bw3)/sum(z2));
if ratio>=1
i=immultiply(i,bw3);
else
z2=im2bw(i,0.75);
bw3=bwselect(z2,c,r,8);
ratio2=min(sum(bw3)/sum(z2));
if round(ratio2)==0
lvl=graythresh(i);
z2=im2bw(i,1.75*lvl);
bw3=bwselect(z2,c,r,8);
bw3=~bw3;
i=immultiply(i,bw3);
else
bw3=~bw3;
i=immultiply(i,bw3);
end
end
figure(5)
imshow(i)
title('Getting only the Breast')
%% Adaptive Median Filter
% clc;
% clear;
% close all;
% a=imread('M1.jpg');
% b=rgb2gray(i);
J = imnoise(i,'salt & pepper', 0.02);
NoisyImage=J;
[R C P]=size(NoisyImage);
OutImage=zeros(R,C);
figure;
% imshow(J);
Zmin=[];
Zmax=[];
Zmed=[];
for i=1:R
for j=1:C
if (i==1 & j==1)
% for right top corner[8,7,6]
elseif (i==1 & j==C)
% for bottom left corner[2,3,4]
elseif (i==R & j==1)
% for bottom right corner[8,1,2]
elseif (i==R & j==C)
%for top edge[8,7,6,5,4]
elseif (i==1)
% for right edge[2,1,8,7,6]
elseif (i==R)
% // for bottom edge[8,1,2,3,4]
elseif (j==C)
%// for left edge[2,3,4,5,6]
elseif (j==1)
else
SR1 = NoisyImage((i-1),(j-1));
SR2 = NoisyImage((i-1),(j));
SR3 = NoisyImage((i-1),(j+1));
SR4 = NoisyImage((i),(j-1));
SR5 = NoisyImage(i,j);
SR6 = NoisyImage((i),(j+1));
SR7 = NoisyImage((i+1),(j-1));
SR8 = NoisyImage((i+1),(j));
SR9 = NoisyImage((i+1)),((j+1));
TempPixel=[SR1,SR2,SR3,SR4,SR5,SR6,SR7,SR8,SR9];
Zxy=NoisyImage(i,j);
Zmin=min(TempPixel);
Zmax=max(TempPixel);
Zmed=median(TempPixel);
A1 = Zmed - Zmin;
A2 = Zmed - Zmax;
if A1 > 0 && A2 < 0
% go to level B
B1 = Zxy - Zmin;
B2 = Zxy - Zmax;
if B1 > 0 && B2 < 0
OutImage(i,j)= Zxy;
else
OutImage(i,j)= Zmed;
end
else
if ((R > 4 && R < R-5) && (C > 4 && C < C-5))
S1 = NoisyImage((i-1),(j-1));
S2 = NoisyImage((i-2),(j-2));
S3 = NoisyImage((i-1),(j));
S4 = NoisyImage((i-2),(j));
S5 = NoisyImage((i-1),(j+1));
S6 = NoisyImage((i-2),(j+2));
S7 = NoisyImage((i),(j-1));
S8 = NoisyImage((i),(j-2));
S9 = NoisyImage(i,j);
S10 = NoisyImage((i),(j+1));
S11 = NoisyImage((i),(j+2));
S12 = NoisyImage((i+1),(j-1));
S13 = NoisyImage((i+2),(j-2));
S14 = NoisyImage((i+1),(j));
S15 = NoisyImage((i+2),(j));
S16 = NoisyImage((i+1)),((j+1));
S17 = NoisyImage((i+2)),((j+2));
TempPixel2=[S1,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S12,S13,S14,S15,S16,S17];
Zmed2=median(TempPixel2);
OutImage(i,j)= Zmed2;
else
OutImage(i,j)= Zmed;
end
end
end
end
end
imshow(OutImage,[]);
title('Adaptive Median Filter')
disp('exit');
%%GLCM Feature Extraction
% Y=rgb2gray(OutImage);
Y=double(OutImage);
% statsArray=[];
% Y=Y{k};
glcm2=graycomatrix(Y);
stats = GLCM_Features1(glcm2,0);
ExtractedFeaturesm16=stats;
% save('ExtractedFeaturesm16.mat')
statsTable = struct2table(stats);
% statsArray = table2array(statsTable);
statsArray(k,:) = table2array(statsTable);
% statsArray'
end
% coeff = pca(statsArray);
[coeff,score,latent,~,explained] = pca(statsArray);
covarianceMatrix = cov(statsArray);
[V,D] = eig(covarianceMatrix);
coeff
V
dataInPrincipalComponentSpace = statsArray*coeff
score2=score(:,1:2);
% coeff2 = coeff(:,1:2);
corrcoef(dataInPrincipalComponentSpace)
var(dataInPrincipalComponentSpace)'
latent
sort(diag(D),'descend')
inp=input('Enter Image:');
i=imread(inp);
try
i=rgb2gray(i);
end
z=im2bw(i,0.1);
figure(2)
imshow(z);title('Original B&W')
info=regionprops(z);
a=cat(1,info.Area);
[m,l]=max(a);
X=info(l).Centroid;
bw2=bwselect(z,X(1),X(2),8);
i=immultiply(i,bw2);
figure(3)
imshow(i);
title('Getting the Breast and Muscle')
%% Deleting Black Ground
% We will delete the black corners
% So that we can select the muscle
% using bwselect
% convert to B&W first time
[x,y]=size(z);
tst1=zeros(x,y);
% detect empty rows
r1=[];
m=1;
for j=1:x
if z(j,:)==tst1(j,:)
r1(m)=j;
m=m+1;
end
end
% detect empty columns
r2=[];
m=1;
for j=1:y
if z(:,j)==tst1(:,j)
r2(m)=j;
m=m+1;
end
end
% Deleting
i(:,r2)=[];
i(r1,:)=[];
figure(4)
imshow(i);title('after deleting background');
%% Deleting the Muscle
if i(1,1)~=0
c=3;
r=3;
else
r=3;
c=size(i,2)-3;
end
z2=im2bw(i,0.5);
bw3=bwselect(z2,c,r,8);
bw3=~bw3;
ratio=min(sum(bw3)/sum(z2));
if ratio>=1
i=immultiply(i,bw3);
else
z2=im2bw(i,0.75);
bw3=bwselect(z2,c,r,8);
ratio2=min(sum(bw3)/sum(z2));
if round(ratio2)==0
lvl=graythresh(i);
z2=im2bw(i,1.75*lvl);
bw3=bwselect(z2,c,r,8);
bw3=~bw3;
i=immultiply(i,bw3);
else
bw3=~bw3;
i=immultiply(i,bw3);
end
end
figure(5)
imshow(i)
title('Getting only the Breast')
J = imnoise(i,'salt & pepper', 0.02);
NoisyImage=J;
[R C P]=size(NoisyImage);
OutImage=zeros(R,C);
figure;
% imshow(J);
Zmin=[];
Zmax=[];
Zmed=[];
for i=1:R
for j=1:C
if (i==1 & j==1)
% for right top corner[8,7,6]
elseif (i==1 & j==C)
% for bottom left corner[2,3,4]
elseif (i==R & j==1)
% for bottom right corner[8,1,2]
elseif (i==R & j==C)
%for top edge[8,7,6,5,4]
elseif (i==1)
% for right edge[2,1,8,7,6]
elseif (i==R)
% // for bottom edge[8,1,2,3,4]
elseif (j==C)
%// for left edge[2,3,4,5,6]
elseif (j==1)
else
SR1 = NoisyImage((i-1),(j-1));
SR2 = NoisyImage((i-1),(j));
SR3 = NoisyImage((i-1),(j+1));
SR4 = NoisyImage((i),(j-1));
SR5 = NoisyImage(i,j);
SR6 = NoisyImage((i),(j+1));
SR7 = NoisyImage((i+1),(j-1));
SR8 = NoisyImage((i+1),(j));
SR9 = NoisyImage((i+1)),((j+1));
TempPixel=[SR1,SR2,SR3,SR4,SR5,SR6,SR7,SR8,SR9];
Zxy=NoisyImage(i,j);
Zmin=min(TempPixel);
Zmax=max(TempPixel);
Zmed=median(TempPixel);
A1 = Zmed - Zmin;
A2 = Zmed - Zmax;
if A1 > 0 && A2 < 0
% go to level B
B1 = Zxy - Zmin;
B2 = Zxy - Zmax;
if B1 > 0 && B2 < 0
OutImage(i,j)= Zxy;
else
OutImage(i,j)= Zmed;
end
else
if ((R > 4 && R < R-5) && (C > 4 && C < C-5))
S1 = NoisyImage((i-1),(j-1));
S2 = NoisyImage((i-2),(j-2));
S3 = NoisyImage((i-1),(j));
S4 = NoisyImage((i-2),(j));
S5 = NoisyImage((i-1),(j+1));
S6 = NoisyImage((i-2),(j+2));
S7 = NoisyImage((i),(j-1));
S8 = NoisyImage((i),(j-2));
S9 = NoisyImage(i,j);
S10 = NoisyImage((i),(j+1));
S11 = NoisyImage((i),(j+2));
S12 = NoisyImage((i+1),(j-1));
S13 = NoisyImage((i+2),(j-2));
S14 = NoisyImage((i+1),(j));
S15 = NoisyImage((i+2),(j));
S16 = NoisyImage((i+1)),((j+1));
S17 = NoisyImage((i+2)),((j+2));
TempPixel2=[S1,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S12,S13,S14,S15,S16,S17];
Zmed2=median(TempPixel2);
OutImage(i,j)= Zmed2;
else
OutImage(i,j)= Zmed;
end
end
end
end
end
imshow(OutImage,[]);
title('Adaptive Median Filter')
disp('exit');
%%GLCM Feature Extraction
% Y=rgb2gray(OutImage);
Y=double(OutImage);
% statsArray=[];
% Y=Y{k};
glcm2=graycomatrix(Y);
stats = GLCM_Features1(glcm2,0);
ExtractedFeaturesm16=stats;
% save('ExtractedFeaturesm16.mat')
statsTable = struct2table(stats);
% statsArray = table2array(statsTable);
statsArray1 = table2array(statsTable);
% statsArray'
% [coeff3,score3,latent,~,explained] = pca(statsArray1);
% coeff4 = coeff(:,1:2);
[coeff3,score3,latent,~,explained] = pca(statsArray1);
TrainingSet=[score2(1,:);score2(2,:);score2(3,:);score2(4,:);score2(5,:);score2(6,:);score2(7,:);score2(8,:);
score2(9,:);score2(10,:);score2(11,:);score2(12,:);score2(13,:);score2(14,:);score2(15,:);score2(16,:);
score2(17,:);score2(18,:);score2(19,:);score2(20,:);score2(21,:);score2(22,:);score2(23,:);score2(24,:);
score2(25,:);score2(26,:);score2(27,:);score2(28,:);score2(29,:);score2(30,:);score2(31,:);score2(32,:)]
GroupTrain={'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2',}
TestSet=score3;
SVMModels=cell(2,1);
S=GroupTrain;
classes =unique(S);
rng(1);
% test = TestSet;
% result = multisvm(TrainingSet,classes,test);
for j=1:numel(classes)
indx=strcmp(S',classes(j));
SVMModels{j}=fitcsvm(score2,indx,'ClassNames',[false true],'Standardize',true,'KernelFunction','rbf','BoxConstraint',1);
end
xGrid=TestSet;
for j= 1:numel(classes)
[~,score]=predict(SVMModels{j},xGrid);
Scores(:,j)=score(:,2);
end
[~,maxScore]=max(Scores,[],2);
result=maxScore;
figure,imshow(i)
if result==1
msgbox('Benign')
elseif result==2
msgbox('Malignant')
end
The error message is below:
Error using classreg.learning.classif.ClassificationModel/predictEmptyX (line 203)
X data must have 2 columns.
Error in classreg.learning.classif.ClassificationModel/predict (line 406)
[labels,scores,cost] = predictEmptyX(this,X);
Error in classreg.learning.classif.CompactClassificationSVM/predict (line 433)
predict@classreg.learning.classif.ClassificationModel(this,X,varargin{:});
0 Commenti
Risposta accettata
Mahesh Taparia
il 19 Giu 2020
Hi
From your code and the error message, it seems the variable xGrid did not have the 2 columns.
[~,score]=predict(SVMModels{j},xGrid);
Check the dimension of xGrid, there may be dimensional mismatch/ not properly arranges/you might have missed the data.
5 Commenti
Mahesh Taparia
il 29 Giu 2020
Hi
Atleast take the number of data to be 2 or more. For example, consider below:
a=randn(2,20);
[coeff,score,latent]=pca(a,'NumComponents',10);
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Dimensionality Reduction and Feature Extraction in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!