MATLAB: Grouping boundary points of several ROIs

image processingImage Processing Toolboxsort

Hi,
I have 6 region of interests (ROI) and their boundary points and centroids. I got this boundary points randomly through MATLAB programming. Do you have an idea how to group them according to corresponding ROIs in MATLAB? I need individual ROI's boundary points for further calculations.
I have applied Kmeans and it didn't work.

Best Answer

This may be extraneous, but if you don't need the boundary coordinates for anything other than finding the extrema, consider this instead:
inpict = imread('lumber.png')>128;
% you'll need the centroid anyway
S = regionprops(inpict,'centroid','extrema');
imshow(inpict); hold on
for n = 1:numel(S)
% calculate the right and left extrema of this object
rextrema = mean(S(n).Extrema(3:4,:),1);
lextrema = mean(S(n).Extrema(7:8,:),1);
% for sake of demonstration, show where the extrema are
plot(rextrema(1),rextrema(2),'*')
plot(lextrema(1),lextrema(2),'*')
end