Clear Filters
Clear Filters

Pixel Grouping in Image

31 views (last 30 days)
Dennis Premoli
Dennis Premoli on 4 Aug 2023
Answered: Image Analyst on 4 Aug 2023
Hi everyone,
After thresholding, binarizing and removing small stray pixels, I have the following image:
and I essnetially aim to obtain ROI drawn around group of pixels.
However, I am having some trouble. I have tried with
boundaries = bwboundaries(I_noholes);
with similar results, where the counturs were drawn around each individual detail within the desired ROIs, rather than around the group.
I have also tried closing the image but I end up losing too much detail within the ROIs tehmselves, due to the pixels being too sparse and far from each other and ahving to use a base shape to close the image.
se = strel('disk',30);
I_closed = imclose(I_contour,se);
Finally, I have a,lso tried convex hull but even using the "objects" parameter, it considers my entire circle as a giant hull, rather than going in an individually closing each sub group of pixels.
Any suggestions are welcome!

Answers (1)

Image Analyst
Image Analyst on 4 Aug 2023
As you found out, morphological things will change the shape of the binary blobs. This is a case for dbscan.
You can get all the (row, column) coordinates of the binary image from
[rows, columns] = find(binaryImage);
or if you prefer x,y
[y, x] = find(binaryImage);
Then put those into dbscan to find clusters.
help dbscan
DBSCAN Density-Based algorithm for clustering IDX = DBSCAN(X, EPSILON, MINPTS) partitions the points in the N-by-P data matrix X into clusters based on parameters EPSILON and MINPTS. EPSILON is a threshold for a neighborhood search query. MINPTS is a positive integer used as a threshold to determine whether a point is a core point. IDX is an N-by-1 vector containing cluster indices. An index equal to '-1' implies a noise point. IDX = DBSCAN(D, EPSILON, MINPTS, 'DISTANCE', 'PRECOMPUTED') is an alternative syntax that accepts distances D between pairs of observations instead of raw data. D may be a vector or matrix as computed by PDIST or PDIST2, or a more general dissimilarity vector or matrix conforming to the output format of PDIST or PDIST2. [IDX, COREPTS] = DBSCAN(...) returns a logical vector COREPTS indicating indices of core-points as identified by DBSCAN. IDX = DBSCAN(..., 'PARAM1',val1, 'PARAM2',val2, ...) specifies optional parameter name/value pairs to control the algorithm used by DBSCAN. Parameters are: 'Distance' - a distance metric which can be any of the distance measures accepted by the PDIST2 function. The default is 'euclidean'. For more information on PDIST2 and available distances, type HELP PDIST2. An additional choice is: 'precomputed' - Needs to be specified when a custom distance matrix is passed in 'P' - A positive scalar indicating the exponent of Minkowski distance. This argument is only valid when 'Distance' is 'minkowski'. Default is 2. 'Cov' - A positive definite matrix indicating the covariance matrix when computing the Mahalanobis distance. This argument is only valid when 'Distance' is 'mahalanobis'. Default is NANCOV(X). 'Scale' - A vector S containing non-negative values, with length equal to the number of columns in X. Each coordinate difference between X and a query point is scaled by the corresponding element of S. This argument is only valid when 'Distance' is 'seuclidean'. Default is NANSTD(X). Example: % Find clusters in data X, using the default distance metric % 'euclidean'. X = [rand(20,2)+2; rand(20,2)]; idx = dbscan(X,0.5,2); See also KMEANS, KMEDOIDS, PDIST2, PDIST. Documentation for dbscan doc dbscan
See my attached dbscan demo, and adapt it. See in the picture below with randomly placed points, it found 7 clusters of points that were close to each other, and 10 isolated points that were not close enough to any other points to be considered as being part of a cluster.

Community Treasure Hunt

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

Start Hunting!