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
Your exception, is due to the fact that
kmeansis expecting data of type double (Thus the call todoublein 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.reshapeis your friend for this stuff. Here’s an example. Good luck.