procesing n number of segments using a loop

Illustration
Akmol - 2023-03-02T11:32:55+00:00
Question: procesing n number of segments using a loop

I have segmented an image using k-means. The number of cluster is varying say 'n' number. This part of the code 'segmented_images{k}' contains all the segments. I would like process all the segments using a loop. For instance, If I want to calculate the mean values of all segments, how can I do that using a loop? part of the code is given below- for k = 1:nclusters     color = I1; color(rgb_label ~= k ) = 0; segmented_images{k} = color; end figure,imshow(segmented_images{1}), title('objects in cluster 1'); figure,imshow(segmented_images{2}), title('objects in cluster 2'); figure,imshow(segmented_images{3}), title('objects in cluster 3');     Any thing can be done individually. For example, displaying them one by one or calculating their mean. How can I write a code that will display all the segments one by one and will calculate the mean of each segment available in the 'segmented_images{k}'?

Expert Answer

Profile picture of Neeta Dsouza Neeta Dsouza answered . 2025-11-20

Use regionprops. For example (untested):

 

for k = 1 : 3
    thisBinaryImage = segmented_images{k};  % Extract segmented image, which should be binary/logical, not masked gray level.
    [labeledImage, numBlobs] = bwlabel(thisBinaryImage);
    props = regionprops(labeledImage, thisGrayImage{k}, 'MeanIntensity');
    allIntensities = [props.MeanIntensity];
    for b = 1 : numBlobs
        fprintf('For image %d, blob #%d mean intensity = %f\n', ...
            k, b, allIntensities(b));
    end
end

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!