How to match pixel locations to blob identity with region props

7 views (last 30 days)
I am using regionprops to identify blobs in a binary image. I am able to detect a couple of blobs, get their centroids and all the properties using the function regionprops.
I would like now to estimate/calculate the direction of the objects. Since they are not simmetrical but more like "comet shaped", I thought one way to go would be to get all pixels of each blob (using the PixelList property) and calculate how many of them are below or above the minor axis of the blob....basically where most pixels are, that's where the front part of the objecft is (perhaps there are better methods but this is the simplest one I could think of).
To do so, I would need to determine which pixels belong to each blob, which is not given by any property of the function regionprops.
Maybe is just a matter of finding an easy nearestneighbor search but so far I was not lucky.
this is my code
%% these lines just import an image and subtract background in order to detect the objects
I = imread('image1.png');
B = imread('bknd.png');
D = imabsdiff(I,B);
Db = im2bw(D,[],0.07); % image is thresholded and binarized
Db2 = bwareaopen(Db, 5); % remove objects having less than 5 pixels
se = strel('disk',6,6); % shape used to dilate objects
Db3 = imdilate(Db2,se);
f= figure();
[labeledImage, numSpots] = bwlabel(Db3);
boundaries=bwboundaries(labeledImage); % coordinates of blob boundaries: I initially thought to use them but they are kind of offset relative to the blobs which makes them useless
stats = regionprops('struct',Db3,'Centroid','PixelList','PixelIdxList','Orientation','MajorAxisLength','MinorAxisLength');
centers = vertcat(stats.Centroid); % [stats(1).Centroid(1), stats(1).Centroid(2);stats(2).Centroid(1), stats(2).Centroid(2)];
angles = horzcat(stats.Orientation);
pixels = stats.PixelList;
pixel_idx = stats.PixelIdxList;
diameters = mean([stats.MajorAxisLength stats.MinorAxisLength],2);
radii = diameters/2;
Major_axes = horzcat(stats.MajorAxisLength);
Minor_axes = horzcat(stats.MinorAxisLength);
imshow(labeledImage)
%% find centroid axes
hold on
scatter(centers(:,1),centers(:,2),'r','filled')

Answers (1)

Shashank Gupta
Shashank Gupta on 18 Nov 2020
Hi,
regionprops function have one property which finds orientations of the blobs, you could directly use them to find the direction of the blobs, I did see that you calculated the orientation but you are not using them anywhere. can you elaborate why orientation property will not help you out? Because what I understood is you want to know the direction of the object and orientation will give you the angle that the major axis of the blob makes it with x-axis, which I think is the way to find the direction of the object. Correct me If I am wrong here.
  1 Comment
LO
LO on 18 Nov 2020
Thanks Shashank for your reply. I know about the orientation property, however what I need is not only an angle, is the "orientation" of the object along the direction axis. My wording was probably unclear.
Since the object has two halves of slightly different sizes, I thought one way to go would be counting pixels (as above described) on either side of the minor blob axis.
However I was wondering whether there are less dumb and more solid methods (not based on the blob axes but based on the cloud of pixels). Something like k-means clustering came to my mind...(one could find statistically significantly different data clouds, likely belonging to different segments) however, as far as I know, this clustering method would really just split the data points based on the random shape they take (not exactly in the middle of it...as would probably be better for my case). Due to its randomness the k-means clustering would give me two clouds (assuming I set 2 as a limit) which will be too irregular with time (sampling the same objects at different times will result in clusters of different shape).
Something like a median cutting line in 2D ? not sure what I am even talking about :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!