Index in position 1 exceeds array bounds (must not exceed 1).error in SVM

1 view (last 30 days)
when i run this code ,it shows like Index in position 1 exceeds array bounds (must not exceed 1).error in line 208
TrainingSet=[ 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,:);DF(46,:);DF(47,:);DF(48,:);DF(49,:);DF(50,:) ]
how to slove that ,please help through code.
clc
clear all
close all
cd segmented_images
DF=[]
for i=1:50
i
str1=int2str(i)
str2=strcat(str1,'.jpg');
rgbImage=imread(str2);
%%[filename, pathname] = uigetfile({'*.*';'*.bmp';'*.jpg';'*.gif'}, 'Pick a Leaf Image File');
%%rgbImage = imread([pathname,filename]);
rgbImage = imresize(rgbImage,[256,256]);
bplane= rgbImage(:,:,3) - 0.5*( rgbImage(:,:,1)) - 0.5*( rgbImage(:,:,2));
%figure
%imshow(bplane)
%Extract out purple cells
%figure
BW= bplane >29;
%imshow(BW)
%%Remove noise 100 pixels or less
BW2=bwareaopen(BW,350);
%subplot(2,4,6);imshow(BW2);
%title('WBC Dilated Image');
%%calculate area of regions
%cellStats = regionprops(BW2, 'all');
%cellAreas = [cellStats(:).Area];
%%Superimpose onto original image
%figure,imshow(image),hold on
%himage=imshow(BW2);
%set(himage, 'AlphaData', 0.5);
CC= bwconncomp(BW2);
count = CC.NumObjects;
stats=regionprops(CC,'Area','Circularity','Centroid','Eccentricity','EquivDiameter','Extent','FilledArea','Perimeter','MajorAxisLength','MinorAxisLength','Image','Solidity');
%Centroid=reshape([stats.Centroid],2,CC.NumObjects)
majors=[stats.MajorAxisLength];
minors=[stats.MinorAxisLength];
Area=[stats.Area]
Circularity =[stats.Circularity]
Perimeter=[stats.Perimeter]
diameters=[stats.EquivDiameter]
radii=[diameters./2]
%image compression using DCT
% Convert to grayscale if image is RGB
if ndims(rgbImage) == 3
img = rgb2gray( rgbImage);
end
%figure, imshow(img); title('Gray Scale Image');
%img = rgb2gray(seg_img)
J=dct2(img);
%imshow(log(abs(J)),[]);
%colormap parula
%colorbar
J(abs(J)<10) =0;
K= idct2(J);
K=rescale(K);
%subplot(2,4,5);imshow(K);
%montage({I,K})
%title('Compressed Image');
% Create the Gray Level Cooccurance Matrices (GLCMs)
glcms = graycomatrix(K);
% Derive Statistics from GLCM
stats = graycoprops(glcms,'Contrast Correlation Energy Homogeneity');
Contrast = stats.Contrast
Correlation = stats.Correlation
Energy = stats.Energy
Homogeneity = stats.Homogeneity
Mean = mean2(rgbImage)
Standard_Deviation = std2(rgbImage)
Entropy = entropy(rgbImage)
%RMS = mean2(rms(rgbImage));
%Skewness = skewness(rgbImage)
Variance = mean2(var(double(rgbImage)))
a = sum(double(rgbImage(:)));
Smoothness = 1-(1/(1+a))
Kurtosis = kurtosis(double(rgbImage(:)))
Skewness = skewness(double(rgbImage(:)))
% Inverse Difference Movement
m = size(rgbImage,1);
n = size(rgbImage,2);
in_diff = 0;
for i = 1:m
for j = 1:n
temp = rgbImage(i,j)./(1+(i-j).^2);
in_diff = in_diff+temp;
end
end
IDM = double(in_diff)
WBC_COUNT=count
FEAT = horzcat(1,[Contrast Correlation Energy Homogeneity Mean Standard_Deviation Entropy Variance Smoothness Kurtosis Skewness IDM diameters radii Area Circularity Perimeter]);
DF=[DF,FEAT];
end
cd ..
inp=input('enter image:')
rgbImage=imread(inp);
%[filename, pathname] = uigetfile({'*.*';'*.bmp';'*.jpg';'*.gif'}, 'Pick a wbc Image File');
%rgbImage = imread([pathname,filename]);
%rgbImage = imresize(rgbImage,[256,256]);
% [rows columns numberOfColorBands] = size(rgbImage);
% Enlarge figure to full screen.
%set(gcf, 'Position', get(0,'Screensize'));
bplane= rgbImage(:,:,3) - 0.5*( rgbImage(:,:,1)) - 0.5*( rgbImage(:,:,2));
%figure
%imshow(bplane)
%Extract out purple cells
%figure
BW= bplane >29;
%imshow(BW)
%%Remove noise 100 pixels or less
BW2=bwareaopen(BW,350);
%subplot(2,4,6);imshow(BW2);
%title('WBC Dilated Image');
%%calculate area of regions
%cellStats = regionprops(BW2, 'all');
%cellAreas = [cellStats(:).Area];
%%Superimpose onto original image
%figure,imshow(image),hold on
%himage=imshow(BW2);
%set(himage, 'AlphaData', 0.5);
CC= bwconncomp(BW2);
count = CC.NumObjects;
stats=regionprops(CC,'Area','Circularity','Centroid','Eccentricity','EquivDiameter','Extent','FilledArea','Perimeter','MajorAxisLength','MinorAxisLength','Image','Solidity');
%Centroid=reshape([stats.Centroid],2,CC.NumObjects)
majors=[stats.MajorAxisLength];
minors=[stats.MinorAxisLength];
Area=[stats.Area]
Circularity =[stats.Circularity]
Perimeter=[stats.Perimeter]
diameters=[stats.EquivDiameter]
radii=[diameters./2]
%image compression using DCT
% Convert to grayscale if image is RGB
if ndims(rgbImage) == 3
img = rgb2gray( rgbImage);
end
%figure, imshow(img); title('Gray Scale Image');
%img = rgb2gray(seg_img)
J=dct2(img);
%imshow(log(abs(J)),[]);
%colormap parula
%colorbar
J(abs(J)<10) =0;
K= idct2(J);
K=rescale(K);
%subplot(2,4,5);imshow(K);
%montage({I,K})
%title('Compressed Image');
% Create the Gray Level Cooccurance Matrices (GLCMs)
glcms = graycomatrix(K);
% Derive Statistics from GLCM
stats = graycoprops(glcms,'Contrast Correlation Energy Homogeneity');
Contrast = stats.Contrast
Correlation = stats.Correlation
Energy = stats.Energy
Homogeneity = stats.Homogeneity
Mean = mean2(rgbImage)
Standard_Deviation = std2(rgbImage)
Entropy = entropy(rgbImage)
%RMS = mean2(rms(rgbImage));
%Skewness = skewness(img)
Variance = mean2(var(double(rgbImage)))
a = sum(double(rgbImage(:)));
Smoothness = 1-(1/(1+a))
Kurtosis = kurtosis(double(rgbImage(:)))
Skewness = skewness(double(rgbImage(:)))
% Inverse Difference Movement
m = size(rgbImage,1);
n = size(rgbImage,2);
in_diff = 0;
for i = 1:m
for j = 1:n
temp = rgbImage(i,j)./(1+(i-j).^2);
in_diff = in_diff+temp;
end
end
IDM = double(in_diff)
WBC_COUNT=count
QF = horzcat(1,[Contrast Correlation Energy Homogeneity Mean Standard_Deviation Entropy Variance Smoothness Kurtosis Skewness IDM diameters radii Area Circularity Perimeter]);
%multi svm
TrainingSet=[ 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,:);DF(46,:);DF(47,:);DF(48,:);DF(49,:);DF(50,:) ]
GroupTrain={'1' '1' '1' '1' '1' '1' '1' '1' '1' '1' '2' '2' '2' '2' '2' '2' '2' '2' '2' '2'...
'3' '3' '3' '3' '3' '3' '3' '3' '3' '3' '4' '4' '4' '4' '4' '4' '4' '4' '4' '4' '5' '5' '5' '5' '5' '5' '5' '5' '5' '5'}
TestSet=QF
SVMModels=cell(5,1);
Y=GroupTrain
classes=unique(Y);
rng(1);%for reproducibility
for j=1:numbe1(classes)
indx=strcmp(Y',classes(j)); %create binary classes for each classifier
SVMModel{i}=fitcsvm(DF,indx,'ClassNames',[false true],'Standardize',true,...
'KernelFunction','rbf','BoxContraint',1);
end
XGrid=QF;
for j=1:nume1(classes)
[~,Score] = predict(SVMMOdels{j},XGrid);
Scores(:,j)=Score(:,2);% second column contains positive class score
end
[~,maxScore]=max(Scores,[],2);
result=maxScore;
figure,imshow(rgbImage)
title ('input image')
if result==1
msgbox('Neutrophil')
elseif result==2
msgbox('Eosinophil')
elseif result==3
msgbox('Monocyte')
elseif result==4
msgbox('Lymphocyte')
elseif result==5
msgbox('Basophil')
end
  3 Comments
Scott MacKenzie
Scott MacKenzie on 8 May 2021
Actually, what I meant was using the code button when creating your question to change a block of text -- the code you inserted -- to MATLAB code:
Then, it's easier for people to read your question and they can easily copy your code into their own MATLAB editor window and run it.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 8 May 2021
The error appears to be with how you are indexing DF. The error message is telling you the index in position 1 (rows) exceeds the size of the array. It then tells you the maximum allowable number is 1. Clearly you expect there to be many more rows than 1, so the code that creates DF is not doing what you expect.
  1 Comment
BC
BC on 8 May 2021
Edited: BC on 8 May 2021
To add to this all I can see with DF before it's indexed is:
DF=[]
...
DF=[DF,FEAT];
So it seems like DF is just a blank value concatenated with FEAT.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!