The confusion matrix and confusionmat function between pixels of two images

5 views (last 30 days)
Hello. I am new to matlab and I am trying to make the confusion matrix between the pixels of two images. The 2 images have only the value 0 and 1 of the pixels, that's why I should have a confusion matrix with 2 rows and 2 columns. The first image is segmented with kmeans algorithm and the second one(the MaskImage) is an image in black and white. When I run the code below, it appears a matrix with 42 columns and 42 rows and I don't understand what I am doing wrong. I wil also attach the 2 images, maybe there is something wrong. Please help me! Thank you!

Accepted Answer

Raunak Gupta
Raunak Gupta on 21 Feb 2020
Hi,
After running the attached code, I see that while VectorImagKmeans is a Boolean vector (having only 0s and 1s as entries), VectorMaskImage is not a Boolean vector (It consists 42 different values). That is why the confusion matrix is 42x42 and not 2x2.
The most probable reason is as you are loading the MaskImage from a ‘.jpeg’ file due to quality degradation some pixel value gets merged to different values rather than what is saved originally.
One workaround is to have a threshold on the image that is read, for example if the pixel value is less than 0.2, most probably its 0 and if its greater than 0.8, most probably its 1.
Below code (before calling confusionmat) may help correcting this issue.
VectorMaskImage(VectorMaskImage>0.8)=1;
VectorMaskImage(VectorMaskImage<0.2)=0;

More Answers (0)

Community Treasure Hunt

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

Start Hunting!