3

I need to use the kmeans function on a rgb image. Each element of the algorithm need to have 3 dimensions, one for each channel of the image. The number of elements will be the total amount of pixels of the image. I need to use kmeans on the cluster #5.

So this is what I tried:

img = imread('mypic.jpg'); red = img(:,:,1); green = img(:,:,2); blue = img(:,:,3); kmeans(red,5) 


I dont know if I'm doing it correctly or not. I'm having this error:

??? Error using ==> plus Integers can only be combined with integers of the same class, or scalar doubles. Error in ==> kmeans>distfun at 704 D(:,i) = D(:,i) + (X(:,j) - C(i,j)).^2; Error in ==> kmeans at 313 D = distfun(X, C, distance, 0); Error in ==> mysegmentation at 9 kmeans(R,2) 

Can anyone give me a hand? Thanks

3 Answers 3

5

Your exception, is due to the fact that kmeans is expecting data of type double (Thus the call to double in the second line below). But you have an additional issue, in that you're not passing the proper data into kmeans. You need to create a single numpixels x 3 matrix. reshape is your friend for this stuff. Here's an example. Good luck.

img = imread('mypic.jpg'); flatImg = double(reshape(img,size(img,1)*size(img,2),size(img,3))); idx = kmeans(flatImg,5); imagesc(reshape(idx,size(img,1),size(img,2))); 
Sign up to request clarification or add additional context in comments.

6 Comments

Yes! That's what I needed, a numpixels x 3 matrix. Is hard for me to understand a bit the reshape funcion. That function returns a 'size(img,1)*size(img,2)' x 'size(img,3)' matrix, right? Can you explain it a bit? You are putting the 'R*G x B' on the matrix? I'm sorry but I'm quite new with image processing and I need some help with it :)
The value of img(1,1,:) is a 3 element vector holding the RGB values for the pixel at index (1,1). The value of flatImg(1,:) is the same as the value of img(1,1,:). Similarly, the 3 elements at img(1,2,:) are the same as the 3 elements at flatImg(2,:). We are reorganizing the structure of the img matrix to treat the pixels as just vectors of RGB values, and ignoring their spatial relationships.
Great, I see, I'm understanding it little by little! And what if I want to join 2 more dimensions (now will be a total of 5 dimensions). One with the row number of the the pixel and the other one with the number of the column. How can I change that 'flatImg' reshape? Thanks in advance.
The code already handles that issue size(img,3) handles 5 channel images, or 30 channel images....
How can I select the row #3 of the matrix? I know how to get the elements of row #3 (img(3,:)). But I dont know how can I put as a input the row number. I'm getting this error when I add something to the reshape function: "To RESHAPE the number of elements must not change."
|
1

Try img = double(imread('mypic.jpg'))/255.;

Comments

0
I1= imread('d:\flowers.jpg'); I2=rgb2gray(I1); I=double(I2); figure subplot(1,3,1) imshow(I1) subplot(1,3,2) imshow(I2) g=kmeans(I(:),4); J = reshape(g,size(I)); subplot(1,3,3) imshow(J,[]) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.