kmeans image segmentation and confusion matrix

A sample kmeans image segmentation code as well as for its confusion matrix would be greatly appreciated. Thanks. Note: For a non-grayscale image please.

Answers (1)

See the Mathworks demo of using kmeans() on a color (3D) image: http://www.mathworks.com/products/demos/image/color_seg_k/ipexhistology.html
For the confusion matrix you need to know the "true" class for each pixel that was classified. There may be some function to do that in the stats toolbox that I'm not aware of, but if not it's easy enough to just scan the matrices and make it
confusionMatrix = zeros(numberOfClasses); % Initialize
for col = 1 : columns
for row = 1 : rows
% Find the row and column that these classes
% will have in the confusion matrix.
r = classifiedImage(row, column);
c = trueClassificationImage(row, column);
confusionMatrix(r, c) = confusionMatrix(r, c) + 1;
end
end

9 Comments

Thank you so much image analyst but I'm having a little issue running your code for the confusion Matrix. After the segmentation for six classes, I have
segmented_images =
Columns 1 through 4
[2592x4608x3 uint8] [2592x4608x3 uint8] [2592x4608x3 uint8] [2592x4608x3 uint8]
Columns 5 through 6
[2592x4608x3 uint8] [2592x4608x3 uint8]
While for my trained (true class), I have BW_1,BW_2,BW_3,BW_4,BW_5 and BW_6. Each having the below.
<2592x4608 logical>
I'm a newbiew on MATLAB but I tried implementing your code for the confusion matrix but I was unable to.
Please help me on this. Thanks
It looks like segmented_images is a cell array containing 6 RGB images. Why? RGB images are not classified images. Classified images should be integer or uint8 images where the pixel value has the class that has been assigned to the pixel. So if you have 4 classes, the classified images would have pixels values of 1, 2, 3, or 4, with perhaps 0 being unidentified, if you allow unidentifiable classes. So why are yours color? Or 3D? Did you perhaps colorize the classified image somehow?
It sort of appears that you, for some reason, took the classified image and split it up into 6 binary images, which is what your BW_n images are. So each binary image is one of your 6 classes. I guess you could do it that way. But does segmented_images represent 6 original images? Or the 6 binary images for one image, where each image inside each cell is supposed to be one class? But if that's the case, I still don't understand why they're 3D (color). Why aren't they binary/logical???
Same thought here; is why is it so that the segmented_images is a cell array but I used the exact link demo you posted and the only thing different I did is to change the image as well as number of classes (to 6 now) and then it gave me the cell array as posted earlier. Yes on the BW_n images are okay with me. From the algorithm on the demo link, after running it, it appears each cell contains each segmented class and it gave it in 3D (color).
Please how do I resolve the conflict???Most likely I would have like it in the binary or logical format but don't know how to go about it. Thank you.
Oh, in that demo. I actually can't run that because I don't have the Statistics Toolbox. But looking over the code I can see that segmented_images are the masked color images, where each image is the original image that is masked by the class. What you want is the image they call pixel_labels which is the classified image - where each pixel is the class that pixel got assigned to. But since your BW_n are just one image, you're going to have to get single class images from pixel_labels. So for that you do
BW_class_1 = pixel_labels == 1;
BW_class_2 = pixel_labels == 2;
BW_class_3 = pixel_labels == 3;
BW_class_4 = pixel_labels == 4;
BW_class_5 = pixel_labels == 5;
BW_class_6 = pixel_labels == 6;
Yes Yes! You are very correct on this but with respect to your codes on confusion matrix above, please how is it going to be???
Make up a classification image like this
classifiedImage = BW_class_1 + 2 * BW_class_2 + 4 * BW_class_3 + ...
8 * BW_class_4 + 16 * BW_class_2 + 32 * BW_class_3 + ...
64 * BW_class_6;
ease I seem not to understand these codes,that is, why multiply with multiples of 2 and my "class 5" is not accounted for also. I'm I to do likewise for "trueclassificationImage"? Thank you N:B I'm a newbie
You need to create an image where you have 1 for pixels that are class 1, 2 where pixels are class 2, 3 where pixels are class 3, etc. I guess that should actually instead be
classifiedImage = BW_class_1;
classifiedImage(bw_class2) = 2;
classifiedImage(bw_class3) = 3;
classifiedImage(bw_class4) = 4;
classifiedImage(bw_class5) = 5;
classifiedImage(bw_class6) = 6;
After doing as mentioned in the last comment for the classifiedImage, I'm still unable to achieve the confusion matrix for the segmented image. I would love it if you could help summarize to finally achieve the confusion matrix. Thanks for the so much patience #Newbie

Sign in to comment.

Asked:

on 4 Oct 2014

Commented:

on 9 Oct 2014

Community Treasure Hunt

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

Start Hunting!