sir, I have tried the below code for the lung cancer detection but at the last part it showing the error "Index exceeds matrix dimensions'' why this error coming and how to fix it
15 views (last 30 days)
Show older comments
clc;
clear all;
close all;
% read the input CT image
I=imread('image5c.png');
figure
imshow(I);
title('INPUT CT IMAGE');
Igra1=rgb2gray(I);
figure
imshow(Igra1);
title('GRAY IMAGE');
Ifil2=medfilt2(Igra1,[3,3]);
figure
imshow(Ifil2);
title('FILTERED GRAY IMAGE');
text(732,501,'Image courtesy of Corel(R)',...
'FontSize',7,'HorizontalAlignment','right')
hy = fspecial('sobel');
hx = hy';
Iy = imfilter(double(Ifil2), hy, 'replicate');
Ix = imfilter(double(Ifil2), hx, 'replicate');
gradmag = sqrt(Ix.^2 + Iy.^2);
figure
imshow(gradmag,[]), title('Gradient magnitude (gradmag)')
L = watershed(gradmag);
Lrgb = label2rgb(L);
figure, imshow(Lrgb), title('Watershed transform of gradient magnitude (Lrgb)')
se = strel('disk', 20);
Io = imopen(Ifil2, se);
figure
imshow(Io), title('Opening (Io)')
Ie = imerode(Ifil2, se);
Iobr = imreconstruct(Ie, Ifil2);
figure
imshow(Iobr), title('Opening-by-reconstruction (Iobr)')
Ioc = imclose(Io, se);
figure
imshow(Ioc), title('Opening-closing (Ioc)')
Iobrd = imdilate(Iobr, se);
Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr));
Iobrcbr = imcomplement(Iobrcbr);
figure
imshow(Iobrcbr), title('Opening-closing by reconstruction (Iobrcbr)')
fgm = imregionalmax(Iobrcbr);
figure
imshow(fgm), title('Regional maxima of opening-closing by reconstruction (fgm)')
I21 = Ifil2;
Ifil2(fgm) = 255;
figure
imshow(I21), title('Regional maxima superimposed on original image (I21)')
se2 = strel(ones(5,5));
fgm2 = imclose(fgm, se2);
fgm3 = imerode(fgm2, se2);
fgm4 = bwareaopen(fgm3, 20);
I3 = Ifil2;
I3(fgm4) = 255;
figure
imshow(I3)
title('Modified regional maxima superimposed on original image (fgm4)')
bw = imbinarize(Iobrcbr);
figure
imshow(bw), title('Thresholded opening-closing by reconstruction (bw)')
D = bwdist(bw);
DL = watershed(D);
bgm = DL == 0;
figure
imshow(bgm), title('Watershed ridge lines (bgm)')
gradmag2 = imimposemin(gradmag, bgm | fgm4);
L = watershed(gradmag2);
I4 = Ifil2;
I4(imdilate(L == 0, ones(3, 3)) | bgm | fgm4) = 255;
figure
imshow(I4)
title('Markers and object boundaries superimposed on original image (I4)')
Lrgb = label2rgb(L, 'jet', 'w', 'shuffle');
figure
imshow(Lrgb)
title('Colored watershed label matrix (Lrgb)')
figure
imshow(Ifil2)
hold on
himage = imshow(Lrgb);
himage.AlphaData = 0.3;
title('Lrgb superimposed transparently on original image')
x = double(fgm);
m = size(fgm,1);
n = size(fgm,2);
signal1 = fgm(:,:);
%Feat = getmswpfeat(signal,winsize,wininc,J,'matlab');
%Features = getmswpfeat(signal,winsize,wininc,J,'matlab');
[cA1,cH1,cV1,cD1] = dwt2(signal1,'db4');
[cA2,cH2,cV2,cD2] = dwt2(cA1,'db4');
[cA3,cH3,cV3,cD3] = dwt2(cA2,'db4');
DWT_feat = [cA3,cH3,cV3,cD3];
G = pca(DWT_feat);
g = graycomatrix(G);
stats = graycoprops(g,'Contrast Correlation Energy Homogeneity');
Contrast = stats.Contrast;
Correlation = stats.Correlation;
Energy = stats.Energy;
Homogeneity = stats.Homogeneity;
Mean = mean2(G);
Standard_Deviation = std2(G);
Entropy = entropy(G);
RMS = mean2(rms(G));
%Skewness = skewness(img)
Variance = mean2(var(double(G)));
a = sum(double(G(:)));
Smoothness = 1-(1/(1+a));
Kurtosis = kurtosis(double(G(:)));
Skewness = skewness(double(G(:)));
% Inverse Difference Movement
m = size(G,1);
n = size(G,2);
in_diff = 0;
for i = 1:m
for j = 1:n
temp = G(i,j)./(1+(i-j).^2);
in_diff = in_diff+temp;
end
end
IDM = double(in_diff);
feat = [Contrast,Correlation,Energy,Homogeneity, Mean, Standard_Deviation, Entropy, RMS, Variance, Smoothness, Kurtosis, Skewness, IDM];
%%SVM TRAINING AND CLASSIFICATION
database = xlsread('datacan.xls');
% Read Database in excel file
disp('data base of patients ');
disp(database);
Contrast = database(1:10,1);
Correlation = database(1:10,2);
Energy = database(1:10,3);
Homogeneity = database(1:10,4);
Mean = database(1:10,5);
Standard_Deviation = database(1:10,6);
Entropy = database(1:10,7);
RMS = database(1:10,8);
Variance = database(1:10,9);
Smoothness = database(1:10,10);
Kurtosis = database(1:10,11);
Skewness = database(1:10,12);
IDM = database(1:10,13);
xdata = ([Contrast,Correlation,Energy,Homogeneity, Mean, Standard_Deviation, Entropy, RMS, Variance, Smoothness, Kurtosis, Skewness, IDM]);
group = database(1:10,14);
svmStruct= svmtrain(xdata,group,'kernel_function','rbf','rbf_sigma',3,'ShowPlot',true);
% Classifying New input data
disease = svmclassify(svmStruct,[feat],'ShowPlot',true);
if (disease==1)
disp('Patient is having cancer');
h=msgbox('Patient is having cancer','RESULT','custom',I);
else
disp('Patient is not having cancer');
h=msgbox('Patient is not having cancer','RESULT','custom',I);
end
9 Comments
Answers (7)
Sagar V
on 29 Jan 2019
how train the datacan.xls file or .mat file please help me to perform for my dataset images
2 Comments
Walter Roberson
on 29 Jan 2019
The code line
svmStruct= svmtrain(xdata,group,'kernel_function','rbf','rbf_sigma',3,'ShowPlot',true);
does the training.
Note that svmtrain() has been replaced in newer MATLAB releases.
Sagar V
on 7 Feb 2019
how to create datacan.xls file or .mat file of an 50 images at a time and storing to
a file
LAKSHIPRIYA GOGOI
on 11 May 2020
Edited: LAKSHIPRIYA GOGOI
on 11 May 2020
Getting problem in this syntax
svmStruct= svmtrain(xdata,group,'kernel_function','rbf','rbf_sigma',3,'ShowPlot',true);
I am using Matlab2014a.. I will be pleased if I got the answer..
5 Comments
Walter Roberson
on 24 Jul 2021
Agent: we need to know which MATLAB version you are using, and whether you have the Statistics toolbox, and if not whether you have installed the third party SVM toolbox
Kunala Sahith
on 8 Dec 2021
can u give training code for svm for the above mentioned code R2021B VRSION
Vinithra P
on 5 Jul 2021
May I know, which part is the detection in this?
1 Comment
Walter Roberson
on 5 Jul 2021
All of it.
In particular, the part from
[cA1,cH1,cV1,cD1] = dwt2(signal1,'db4');
is creating features, through to
feat = [Contrast,Correlation,Energy,Homogeneity, Mean, Standard_Deviation, Entropy, RMS, Variance, Smoothness, Kurtosis, Skewness, IDM];
puts the completed feature vector together.
Then the part from
%%SVM TRAINING AND CLASSIFICATION
to
svmStruct= svmtrain(xdata,group,'kernel_function','rbf','rbf_sigma',3,'ShowPlot',true);
extracts features from a database and does svm training on it.
Then
disease = svmclassify(svmStruct,[feat],'ShowPlot',true);
processes the features extracted from the patient image through the trained SVM system, making a prediction about which group it belongs to.
khushboo singh
on 7 Nov 2021
THIS SYNTAX IS NOT WORKING IN R2021b
svmStruct= svmtrain(xdata,group,'kernel_function','rbf','rbf_sigma',3,'ShowPlot',true);
8 Comments
Walter Roberson
on 8 Dec 2021
svmStruct= fitcsvm(xdata,group,'KernelFunction',' KernelScale',3,'ShowPlot',true);
That line does not match your posted code. Your posted code says
svmStruct= fitcsvm(xdata,group,'KernelFunction','rbf',' KernelScale',3,'ShowPlot',true);
Notice that in the error message, there is no 'rbf' . Also, you introduced a space at the beginning of the KernelScale option.
Kunala Sahith
on 8 Dec 2021
Edited: Walter Roberson
on 8 Dec 2021
I=imread('000114.png');
figure
imshow(I);
title('INPUT CT IMAGE');
Igra1=rgb2gray(I);
figure
imshow(Igra1);
title('GRAY IMAGE');
Ifil2=medfilt2(Igra1,[3,3]);
figure
imshow(Ifil2);
title('FILTERED GRAY IMAGE');
text(732,501,'Image courtesy of Corel(R)',...
'FontSize',7,'HorizontalAlignment','right')
hy = fspecial('sobel');
hx = hy';
Iy = imfilter(double(Ifil2), hy, 'replicate');
Ix = imfilter(double(Ifil2), hx, 'replicate');
gradmag = sqrt(Ix.^2 + Iy.^2);
figure
imshow(gradmag,[]), title('Gradient magnitude (gradmag)')
L = watershed(gradmag);
Lrgb = label2rgb(L);
figure, imshow(Lrgb), title('Watershed transform of gradient magnitude (Lrgb)')
se = strel('disk', 20);
Io = imopen(Ifil2, se);
figure
imshow(Io), title('Opening (Io)')
Ie = imerode(Ifil2, se);
Iobr = imreconstruct(Ie, Ifil2);
figure
imshow(Iobr), title('Opening-by-reconstruction (Iobr)')
Ioc = imclose(Io, se);
figure
imshow(Ioc), title('Opening-closing (Ioc)')
Iobrd = imdilate(Iobr, se);
Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr));
Iobrcbr = imcomplement(Iobrcbr);
figure
imshow(Iobrcbr), title('Opening-closing by reconstruction (Iobrcbr)')
fgm = imregionalmax(Iobrcbr);
figure
imshow(fgm), title('Regional maxima of opening-closing by reconstruction (fgm)')
I21 = Ifil2;
Ifil2(fgm) = 255;
figure
imshow(I21), title('Regional maxima superimposed on original image (I21)')
se2 = strel(ones(5,5));
fgm2 = imclose(fgm, se2);
fgm3 = imerode(fgm2, se2);
fgm4 = bwareaopen(fgm3, 20);
I3 = Ifil2;
I3(fgm4) = 255;
figure
imshow(I3)
title('Modified regional maxima superimposed on original image (fgm4)')
bw = imbinarize(Iobrcbr);
figure
imshow(bw), title('Thresholded opening-closing by reconstruction (bw)')
D = bwdist(bw);
DL = watershed(D);
bgm = DL == 0;
figure
imshow(bgm), title('Watershed ridge lines (bgm)')
gradmag2 = imimposemin(gradmag, bgm | fgm4);
L = watershed(gradmag2);
I4 = Ifil2;
I4(imdilate(L == 0, ones(3, 3)) | bgm | fgm4) = 255;
figure
imshow(I4)
title('Markers and object boundaries superimposed on original image (I4)')
Lrgb = label2rgb(L, 'jet', 'w', 'shuffle');
figure
imshow(Lrgb)
title('Colored watershed label matrix (Lrgb)')
figure
imshow(Ifil2)
hold on
himage = imshow(Lrgb);
himage.AlphaData = 0.3;
title('Lrgb superimposed transparently on original image')
x = double(fgm);
m = size(fgm,1);
n = size(fgm,2);
signal1 = fgm(:,:);
%Feat = getmswpfeat(signal,winsize,wininc,J,'matlab');
%Features = getmswpfeat(signal,winsize,wininc,J,'matlab');
[cA1,cH1,cV1,cD1] = dwt2(signal1,'db4');
[cA2,cH2,cV2,cD2] = dwt2(cA1,'db4');
[cA3,cH3,cV3,cD3] = dwt2(cA2,'db4');
DWT_feat = [cA3,cH3,cV3,cD3];
G = pca(DWT_feat);
g = graycomatrix(G);
stats = graycoprops(g,'Contrast Correlation Energy Homogeneity');
Contrast = stats.Contrast;
Correlation = stats.Correlation;
Energy = stats.Energy;
Homogeneity = stats.Homogeneity;
Mean = mean2(G);
Standard_Deviation = std2(G);
Entropy = entropy(G);
RMS = mean2(rms(G));
%Skewness = skewness(img)
Variance = mean2(var(double(G)));
a = sum(double(G(:)));
Smoothness = 1-(1/(1+a));
Kurtosis = kurtosis(double(G(:)));
Skewness = skewness(double(G(:)));
% Inverse Difference Movement
m = size(G,1);
n = size(G,2);
in_diff = 0;
for i = 1:m
for j = 1:n
temp = G(i,j)./(1+(i-j).^2);
in_diff = in_diff+temp;
end
end
IDM = double(in_diff);
feat = [Contrast,Correlation,Energy,Homogeneity, Mean, Standard_Deviation, Entropy, RMS, Variance, Smoothness, Kurtosis, Skewness, IDM];
%%SVM TRAINING AND CLASSIFICATION
database = readmatrix('datacan.xls','UseExcel', false);
% Read Database in excel file
disp('data base of patients ');
disp(database);
Contrast = database(:,1);
Correlation = database(:,2);
Energy = database(:,3);
Homogeneity = database(:,4);
Mean = database(:,5);
Standard_Deviation = database(:,6);
Entropy = database(:,7);
RMS = database(:,8);
Variance = database(:,9);
Smoothness = database(:,10);
Kurtosis = database(:,11);
Skewness = database(:,12);
IDM = database(:,13);
xdata = ([Contrast,Correlation,Energy,Homogeneity, Mean, Standard_Deviation, Entropy, RMS, Variance, Smoothness, Kurtosis, Skewness, IDM]);
group = database(:,14);
svmStruct= fitcsvm(xdata,group,'KernelFunction','rbf','KernelScale',3,'ShowPlot',true);
% Classifying New input data
disease = ClassificationSVM(svmStruct,(feat),'ShowPlot',true);
if (disease==1)
disp('Patient is having cancer');
h=msgbox('Patient is having cancer','RESULT','custom',I);
else
disp('Patient is not having cancer');
h=msgbox('Patient is not having cancer','RESULT','custom',I);
end
4 Comments
Kunala Sahith
on 8 Dec 2021
Finally IT WORKED SIR BY USING OLD VERSION OF MATLAB
but
showplot still doesnt work
Warning: The display option can only plot 2D training data.
> In svmtrain (line 399)
In dipmain1 (line 141)
Warning: No figure was created during SVMTRAIN so the showplot argument will be set false.
> In svmclassify (line 122)
In dipmain1 (line 143)
im using smvtrain as it is matlab2016b version.
can you pls correct me
code:-
xdata = ([Contrast,Correlation,Energy,Homogeneity, Mean, Standard_Deviation, Entropy, RMS, Variance, Smoothness, Kurtosis, Skewness, IDM]);
group = database(:,14);
svmStruct= svmtrain(xdata,group,'ShowPlot',true);
% Classifying New input data
disease = svmclassify(svmStruct,[feat],'ShowPlot',true);
Walter Roberson
on 9 Dec 2021
Edited: Walter Roberson
on 9 Dec 2021
Shrug. ShowPlot is restricted to plotting data that has two columns. You cannot use it for your data.
ShowPlot is intended to show the line of division between the two classes. That only works if you only have two input dimensions, not 13.
See Also
Categories
Find more on Biotech and Pharmaceutical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!