How to segment a matrix into N regions with sizes XYZ?

5 views (last 30 days)
Hi everyone,
I have a matrix (i.e. 100 x 100, but the shape may vary) and I want to segment it into contiguous regions with specific numbers of elements. So for example 3 contiguous regions, with sizes equal to 2450, 3350 and 4200 elements. As if the matrix had been segmented by k-means but into regions with these exact areas.
Ideally I would also like have some control over roughly where these regions are formed around (i.e. I would like to give 3 points, one of which must be contained within each region), but this is not so important.
This is part of an image segmentation process where we detect N segments in an image and want to recreate a facsimile image with the same number of segments with the same areas.
I have coded an iterative process which starts with N starting points and adds elements to each one consequtively until the whole image is filled, but this is not fast enough and also does not replicate the original segments very well.
I could assign values to a matrix (i.e. 2450 1s, 3350 2s, 4200 3s) based on linear indices but then the resulting segments would be very blocky and may not be contiguous. I could assign these values to random positions and then iteratively move them to congregate around specific points, but that would likely also be a slow process.
Is there such a thing as a 2D topographical sorting algorithm?
Thanks for any help.
  10 Comments
Matt J
Matt J on 1 Feb 2024
Edited: Matt J on 1 Feb 2024
Can you not introduce a surrounding squish region whose area you would allow to change? Affine transformations can be designed that preserve straight lines, area, and boundary continuity (so most of the properties you've mentioned) but forcing them also to respect the same rectangular image boundaries is probably a tall order.

Sign in to comment.

Answers (1)

Pratik
Pratik on 13 Feb 2024
Hi Neurolab,
As per my understanding, you want to segment a matrix into N regions with predetermined sizes. You have few approaches for the same and a common issue with them is the speed of the algorithm.
Segmenting a matrix into contiguous regions with specific sizes is an interesting challenge, particularly if you want to control where the regions form. There is no built-in function in MATLAB as of now that can do this.
For an efficient solution, you might consider implementing your algorithm in a compiled language like C++ and call it from MATLAB using a MEX-function.
Please refer to the following documentation for more information on creating high-performance functions, implemented in modern C++, that can be called from MATLAB:
Also, documentation about best practices for using MEX Functions to Accelerate MATLAB Algorithm can be referred:
I hope this helps!

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!