kmeans image segmentation and confusion matrix
Show older comments
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)
Image Analyst
on 4 Oct 2014
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
bayoishola20
on 5 Oct 2014
Image Analyst
on 5 Oct 2014
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???
bayoishola20
on 5 Oct 2014
Image Analyst
on 5 Oct 2014
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;
bayoishola20
on 6 Oct 2014
Image Analyst
on 6 Oct 2014
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;
bayoishola20
on 7 Oct 2014
Image Analyst
on 7 Oct 2014
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;
bayoishola20
on 9 Oct 2014
Categories
Find more on k-Means and k-Medoids Clustering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!