K stands for Grouping in MATLAB - Output Image

For execution, K means clustering with k = 3 (segments). So I:

1) Converted RGB img to grayscale

2) Dropped the original image into a matrix of n X 1, columns

3) idx = kmeans (column_matrix)

4) output = idx, returned in the same dimensions as the original image.

My questions:

AND

When I do imshow (output) I get a plain white image. However, when I do imshow (output [0 5]) it shows the output image. I understand that 0 and 5 define the display range. But why would I do this?

C) Now the output image should be divided into 3 segments to the right. How do I set it in such a way that I assign

0 for clusters of area 1 1 for clusters of area 2 2 for clusters of area 3

Anyway, I am doing this clustering so that I can segment the image into 3 regions.

Many thanks.

Sincerely.

+1


source to share


2 answers


A: Given that your matrix output

contains scalar values ​​in the range 1 to 3, imshow(output)

treats this as a grayscale matrix and assumes the full range of values ​​is 0 to 255 Therefore, limiting color constraints is necessary, otherwise your image will be white or almost white.



B: output = output - 1

+3


source


As Ryan pointed out, your problem is probably the way you are displaying the image. Here's a working example:



snow = rand(256, 256);
figure;
imagesc(snow);

nClusters = 3;
clusterIndices = kmeans(snow(:), nClusters);

figure;
imagesc(reshape(clusterIndices, [256, 256]));

      

+1


source







All Articles