How to calculate the distance, in pixels, between each centroid in a binary image.

28 visualizzazioni (ultimi 30 giorni)
Hi All,
In short, my question is, "how do you calculate or find out the distance in pixels, between each centroid in a binary image?". I have looked at numerous other questions and can't seem to find one that explains it in a way that I can understand. I am relatively new to Matlab (2 months to be exact, with no previous experience in any other computer language). I have attached the "original image (unprocessed)", the "binary (and rotated) figure", and the "cropped binary figure" I am working with.
I have plotted the centroids onto the cropped binary image using this code:
a1RP=regionprops(a1C,'Centroid','MajorAxisLength',...
'MinorAxisLength','Orientation', 'Eccentricity');
CentroidS=cat(1,a1RP.Centroid);
figure
imshow(a1C)
hold on
plot(CentroidS(:,1),CentroidS(:,2),'b*')
hold off
which, gives the following figure: attached as "binary image with plotted centroids"
I have tried pdist() and pdist2() but I don't understand what the output means. I get a 100x100 double output, and I am not sure how to interpret that. The code I used for that is as follows:
CentroidDistances=pdist2(CentroidS(:,1),CentroidS(:,2),'euclidean');
The reason I want to know the distance between each centroid is because I want to know what the pitch is, in the grid I am imaging, in pixels. The pitch is ~64 microns, but I want to know how many pixels in the image, correspond to 64 microns, and I think the distance between each neighbouring centroid would give me that. In my mind, I expected each of these distances to be the same, but the 100x100 array threw me off.
I really hope I am making sense. If I am not, please feel free to ask me to clarify or expand, if you want more detail
Thank you.
P.s. This is the full code I used:
imshow(a1) % a1=original image (unprocessed)
a1G=mat2gray(a1);
a1GC=imadjust(a1G);
a1GCT=graythresh(a1GC);
a1B=imbinarize(a1GC,a1GCT);
a1R=imrotate(a1B,17,'nearest','loose');
a1C=imcrop(a1R);
a1RP=regionprops(a1C,'Centroid','MajorAxisLength',...
'MinorAxisLength','Orientation', 'Eccentricity');
CentroidS=cat(1,a1RP.Centroid);
figure
imshow(a1C)
hold on
plot(CentroidS(:,1),CentroidS(:,2),'b*')
hold off
CentroidDistances=pdist2(CentroidS(:,1),CentroidS(:,2),'euclidean');

Risposta accettata

Image Analyst
Image Analyst il 15 Giu 2019
Try
xy = vertcat(a1RP.Centroid)
distances = pdist2(xy, xy)
Be aware that centroids are x,y, which is (column, row), not (row, column) -- just in case you ever need to do anything with the image itself. plot() should be okay though because it uses x & y.
So you did basically that and got a 100x100 array that gives the distances in pixels between every one of the 100 blobs in your image.
For the spatial calibration, what is 64 microns? What is "pitch"? Is that the distance between pixels, or the full field of view of the image?
If you used XData and YData options in imshow(), then pdist2 should give the output in microns already. If you didn't then the result in in pixels and you'll have to multiply those distances by the spatial calibration factor. Let's say that the pixel to pixel spacing is 64 microns, and you want the distance between the 34th blob and the 78th blob. You can do
dist = distances(34, 78) * 64;
If you want, you can convert the whole distances matrix at once so you don't have to do it one by one:
distances = pdist2(xy, xy) * 64 % ALL spacings will be in microns now.
  3 Commenti
David Mabwa
David Mabwa il 17 Giu 2019
Apologies for the delayed response and thank you very much for your help with this.
For the spacial calibration, 64 microns is the distance between an identical point in two neighbouring grid squares. I have attached an image showing what I mean.
The image is a TEM sampling grid, with ~50micron sized grids. I am imaging it in transmission, using a 1.7um laser. I am doing this to see what sort of resolution the particular sensor I am using can achieve, in practice.
I've managed to get the pixel count I wanted, thank you again!
x50_2.jpg

Accedi per commentare.

Più risposte (1)

darova
darova il 15 Giu 2019
pdist2 finds distances between every point (every possible combinations. Zeros in matrix means distance to points themselves)
1.png 12.png 13.png 14.png
Also you use pdist2 in a wrong way. Has to be:
CentroidDistances=pdist2(CentroidS,CentroidS,'euclidean');
% something like D=pdist2(xy,xy)
Can you just pick two point, calculate distance and count the number of squares between?
img1.png
  5 Commenti
darova
darova il 17 Giu 2019
Try this
User picks two points, find correspoding points in existing data, calculate distance between them
David Mabwa
David Mabwa il 18 Giu 2019
Wow, thank you very much, that worked exactly the way I had hoped. Thank you!distance tool.jpg

Accedi per commentare.

Categorie

Scopri di più su Dimensionality Reduction and Feature Extraction in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by