How can I process the image and extract the spacing between individuals dots in a row ?
Show older comments
Accepted Answer
More Answers (3)
Walter Roberson
on 22 Apr 2023
1 vote
Invert the image so that the black-on-white becomes white-on-black. regionprops looking for the centroids and extrema. The angle between the upper left extrema and the upper right extrema gives you a rotation angle. Apply a transform matrix to the coordinates: translate to move the upper left extrema to the center, rotate the centroid coordinates by the angle... no need to translate back for this purpose. This should align centroids horizontally; you should be able to ismembertol() on the y coordinates to find all centroids on the same x row, and then you can find the difference between the corresponding (rotated) x coordinates.
3 Comments
Muneeb Ahmed Ahmed
on 23 Apr 2023
Walter Roberson
on 23 Apr 2023
Edited: Walter Roberson
on 23 Apr 2023
or imbinarize with ForegroundPolarity "dark"
Bear in mind that the image you gave is an indexed image. If you want to use either, you'd need to convert it to a grayscale image.
In the answer I gave, I showed a simple method to convert the given indexed image to a clean logical image.
% image is indexed (don't actually need the map though)
[inpict,~] = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1363878/image.png');
% convert to a logical image; cleanup
mask = inpict == 2; % foreground is black
mask = bwareaopen(mask,150); % get rid of speckles
imshow(mask,'border','tight')
chicken vector
on 22 Apr 2023
Edited: chicken vector
on 22 Apr 2023
img = imread('image.png');
[centers, radii, metric] = imfindcircles(img,[5 18]);
You can compute the distances from centers.
Do you need row spacing, or do you simply need the spacing between each blob and its neighbors?
You can also use pdist2() if you have the stats toolbox.
If you do want the row spacing, Walter's suggestion is probably a good place to start. I'm just going to throw this out there.
% image is indexed
[inpict map] = imread('pads.png');
% convert to a logical image; cleanup
mask = inpict == 2;
mask = bwareaopen(mask,150);
% get centroids
S = regionprops(mask,'centroid');
C = vertcat(S.Centroid);
% get distances to 4-neighbors
D = pdist2(C,C);
ref = min(nonzeros(D))*sqrt(2);
D(D>=ref) = 0;
% show distribution of 4-neighbor distances
histogram(nonzeros(D))
% try clustering
[idx c] = kmeans(nonzeros(D),2);
c
If there is a distinct difference between row spacings, that may tell you what they are. It won't directly tell you which is which or whether those spacings even correspond to orthogonal directions, but perhaps this partial example will help verify the results you get from a more useful solution. In this case, those values do correspond to the column and row spacings.
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



