% Show the image according to the segmentation result

function show_segmentation(im)
global label

[i, j, k]=size(im);

% initialize
for r=1:max(max(label))
    c_r(r,1) = 0;
    c_g(r,1) = 0;
    c_b(r,1) = 0;
    cluster(r,1) = 1;
end

% To compute the mean value of Red, Green and Blue value for each cluster
for r=1:size(im, 1)
    for t=1:size(im,2)
        temp = label(r,t);
        if temp <= 0
            result(r, t, 1) = 0;
            result(r, t, 2) = 0;
            result(r, t, 3) = 0;
        else
            c_r(temp,1) = c_r(temp,1)+im(r,t,1);
            c_g(temp,1) = c_g(temp,1)+im(r,t,2);
            c_b(temp,1) = c_b(temp,1)+im(r,t,3);
            cluster(temp,1) = cluster(temp,1) + 1;
        end
    end
end

for r=1:max(max(label))
       c_r(r,1) = c_r(r,1)/cluster(r,1);
       c_g(r,1) = c_g(r,1)/cluster(r,1);
       c_b(r,1) = c_b(r,1)/cluster(r,1);
end

% Construct the result picture according to the label matrix and according RGB value
for r= 1:size(im, 1)
    for t = 1:size(im, 2)
        temp = label(r, t);
        if temp > 0
            result(r, t, 1) = c_r(temp,1);
            result(r, t, 2) = c_g(temp,1);
            result(r, t, 3) = c_b(temp,1);
%         else
%             result(r, t, :) = im(r, t, :);
        end
    end
end

% Display the picture
figure; imshow(uint8(result));