MATLAB: K-NN Classsification on images

Bioinformatics Toolboximage processingImage Processing Toolboxk-nn classificationStatistics and Machine Learning Toolbox

i1=dicomread('1.dcm');
i2=dicomread('2.dcm');
i3=dicomread('3.dcm');
i4=dicomread('4.dcm');
Sample=[i1;
i2];
Training=[i3;
i4];
Group=['1';
'2'];
k=2;
Class = knnclassify(Sample, Training, Group, k);
disp(Class);
I get an error – The length of GROUP must equal the number of rows in TRAINING.
The size of both the images are same.
It works when I'm using co-ordinates instead of i1, i2 and so on.
I know I should be using fitcknn, but it should work as well. Any inputs please?

Best Answer

Hi,
Your input arguments Training and Group must have the same number of rows. I see that Group has only 2 rows, but Training probably has many more rows that than. Training consists of tow matrices stacked on top of each other. These matrices represent images as M-by-N matrices. So if all your images (i1, i2, i3, and i4) are all of size M-by-N, then Sample and Training have 2M rows. If your intention is to treat each image as a single entity, then you could construct Sample and Training as follows:
Sample = [i1(:) i2(:)]';
Training = [i3(:) i4(:)]';
That said, I don't think this classification approach is very useful if you only have one training example from each class.
-Arthur