Finding object for which centroids are in a horizontal line.

1 view (last 30 days)
I have an binary image with different objects in different positions. I want to find the sum of the length of the objects which are placed in a same line and CONSECUTIVE. In my image the required objects are 1,2,3 and 4. Here we cant add the length of the object 5 though its in the same line because its not successive. Is it possible to do that by using regionprops? Thank you
  5 Comments
Walter Roberson
Walter Roberson on 18 Mar 2016
Edited: Walter Roberson on 18 Mar 2016
I do not understand what you mean by "consecutive". The order that objects are returned is not under your control and should be treated as being random, so you should be applying any sorting measures you want based upon the properties returned rather than upon the order that regionprops returns the objects. (The order is not actually random, but the order will not always make immediate sense visually.)
Possibly what you want is to look for objects whose centroid all have the same y coordinate (to within a tolerance) ?
rupam baruah
rupam baruah on 20 Mar 2016
Yes . The objects with same centroid y coordinate . Is it possible with regionprops to do so?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Mar 2016
props = regionprops(YourBinaryImage, 'Centroid');
centroids = vertcat(props.Centroid);
cents_y = centroids(:,2);
match_tolerance = 2; %for example. This says the centroids can be up to 2 pixels out of line
y_match = bsxfun(@(y1,y2) abs(y1 - y2) <= match_tolerance, cents_y, cents_y.');
At this point, y_match is a square logical matrix in which each element (J,K) of the matrix is true if and only if the J'th centroid is within the match tolerance of the K'th centroid.
But you have a problem: being within a distance tolerance is not a transitive property. For example consider [5 6 8]: 5 is within 2 of 6, and 6 is within 2 of 8, but 5 is not within 2 of 8.
When you are using pixels that are themselves at integer coordinates, your centroid is still not at integer coordinates except in some special cases of symmetry, so you must test to within a tolerance rather than testing for equality -- unless you can guarantee that symmetry. For example, even completely symmetric "circles" will have non-integer coordinates for their centroids unless the circles are an odd number of pixels high.
So now what? Well, one way to proceed would be to look for cliques, the existence of which I was reminded about a few hours ago. For your purposes, a clique would be a group of points which were all within a certain distance of each other.
But even then, you would have the problem that if you had, for example, 4 5 6 7 and your maximum tolerance is 2, then [4 5 6] satisfies that all the points are within 2 of each other, but so does [5 6 7], but the complete [4 5 6 7] does not. Match within a tolerance cannot be defined uniquely.... and yet it is your only choice unless your objects happen to have exactly the right properties to force the centroids to be at integer locations and you specifically do not want an object 1 pixel higher to be considered to be within a straight line for your purposes.
This is generally the point at which people reveal that they hard additional information that they forgot to mention earlier.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!