It'll be better idea if you do feature extraction on your image before using kmeans, but If you simply want to perform kmeans on pixel intensities of your gray level image, you can try this:
im=imread('cameraman.tif'); % an exemplar image (256*256)
imData = reshape(im,[],1); % convert it to an array (65536*1)
imData = double(imData); % convert from uint8 to double
IDX = kmeans(imData,2); % perform kmeans with two clusters
imIDX = reshape(IDX,size(im)); % a (256*256) matrix of indexes