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?

 채택된 답변

Arthur Goldsipe
Arthur Goldsipe 2017년 3월 16일

0 개 추천

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

댓글 수: 3

Thanks for your reply. I understood your point regarding the number of rows. My images are 512x512. So, for 2 such images, I have 512x2 rows and only 2 in the Group.
However, I'm not very familiar with the syntax used in matlab. Could you explain what's exactly happening in this code
Sample = [i1(:) i2(:)]';
Training = [i3(:) i4(:)]';
This seems to be working as I don't get the error anymore, but what exactly do you mean by treating each image as a single entity?
Just to give some background - These are gabor filtered CT scan images of lungs. I'm implementing an algorithm which uses K-NN classification on these images.
Arthur Goldsipe
Arthur Goldsipe 2017년 3월 16일
i1(:) reshapes the 512x512 matrix into a (512x1) column vector. The square brackets concatenate . And ' is the transpose operator. So Sample ends up being a 2x512 matrix.
In this case, what I mean by a "single entity" is a "single row" in the matrix, because that's how knnclassify identifies what numbers belong to the same observation.
prahlad h
prahlad h 2017년 3월 17일
Sorry for the noob question but what do you mean by a column vector? When you're reshaping 512x512 matrix into a 512x1 column vector, is there any loss in the image?
Also, a single row, does that mean that all the pixels are not compared when knnclassify is used?
It's working fine but I just wanted to know for my understanding!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 3월 17일

0 개 추천

I know you've already accepted an answer, but you might want to check out my knn demo.

댓글 수: 1

prahlad h
prahlad h 2017년 3월 17일
Sure. I'll look at it. It would help me with understanding it a little better. I've been through your other demos on segmentation and thresholding. They are amazing!

댓글을 달려면 로그인하십시오.

질문:

2017년 3월 16일

댓글:

2017년 12월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by