procesing n number of segments using a loop

A
Akmol · Mar 2, 2023 · 2.3K views
Question
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 PhD Expert
Answered Aug 23, 2026

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

 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!