You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to find the distance of all objects in given image
4 views (last 30 days)
Show older comments
Accepted Answer
DGM
on 11 May 2021
Edited: DGM
on 11 May 2021
This will give an array mapping the distance from every object to every other object. You could reduce this with triu() if you want, due to the symmetry.
inpict = rgb2gray(imread('dots.jpeg'))>128;
L = bwlabel(inpict); % this identfies all the objects
C = regionprops(inpict,'centroid');
C = vertcat(C.Centroid);
D = sqrt((C(:,1)-C(:,1).').^2 + (C(:,2)-C(:,2).').^2);
If you wanted to find the distance to the nearest object, you could use this (there are probably other ways).
D(abs(D)<1E-6) = NaN; % remove zeros
[Dn Nn] = min(D,[],2); % minimize
% Dn is distance to nearest neighbor
% Nn is nearest neighbor
.
18 Comments
Rahul punk
on 15 May 2021
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> te4st at 9
D = sqrt((C(:,1)-C(:,1).').^2 + (C(:,2)-C(:,2).').^2);
DGM
on 15 May 2021
You must be using something prior to R2016b. If so, just use bsxfun().
D = sqrt(bsxfun(@minus,C(:,1),C(:,1).').^2 + bsxfun(@minus,C(:,2),C(:,2).').^2)
Rahul punk
on 17 May 2021
Edited: Rahul punk
on 17 May 2021
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
tt= plot(bc(1),bc(2), '.');
aa=text(bc(1),bc(2), strcat('X: ', num2str(round(bc(1)))));
aa=text(bc(1),bc(2), strcat('X: ', num2str(round(bc(1)))));
how to extract aa values table on matlab array and ,get these x cordinates points to subtract each other to accurate distance measure
above solution work but not accurate in my case
DGM
on 17 May 2021
Edited: DGM
on 17 May 2021
You're going to have to better explain what you want or explain what exactly is wrong with accuracy. None of the code posted on this page mentions a table or any variable called aa. The code you posted just plots points. You're not going to have enough room to put labels on every point.
FWIW
inpict = rgb2gray(imread('dots.jpeg'))>128;
L = bwlabel(inpict);
C0 = regionprops(inpict,'centroid');
C = vertcat(C0.Centroid);
% distance from every object to every other object
%D = sqrt((C(:,1)-C(:,1).').^2 + (C(:,2)-C(:,2).').^2)
D = sqrt(bsxfun(@minus,C(:,1),C(:,1).').^2 + bsxfun(@minus,C(:,2),C(:,2).').^2)
D(abs(D)<1E-6) = NaN; % remove zeros
[Dn Nn] = min(D,[],2); % minimize
% Dn is distance to nearest neighbor
% Nn is nearest neighbor
% plot lines between points and the calculated nearest neighbor
imshow(inpict); hold on
for p = 1:numel(Dn)
pts = vertcat(C0([p Nn(p)]).Centroid);
plot(pts(:,1),pts(:,2))
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/620898/image.png)
Of course, I imagine a lot of these points have multiple neighbors which are at the same minimal distance.
You can still try to cram the labels in there
text(pts(1,1),pts(1,2),sprintf('X: %d\nY: %d',pts(1,1),pts(1,2)),'fontsize',8)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/620908/image.png)
Image Analyst
on 18 May 2021
If you have the stats toolbox you could also use pdist2() to find the distance of every point to every other point.
DGM
on 18 May 2021
I didn't even know about that.
D = pdist2(C,C);
It's certainly a lot more concise, and having the extra distance types is a nice feature.
DGM
on 18 May 2021
C0 = regionprops(inpict,'centroid','equivdiameter');
C = vertcat(C0.Centroid);
R = vertcat(C0.EquivDiameter)/2;
% distance from every object to every other object (centers)
%D = sqrt((C(:,1)-C(:,1).').^2 + (C(:,2)-C(:,2).').^2)
D = sqrt(bsxfun(@minus,C(:,1),C(:,1).').^2 + bsxfun(@minus,C(:,2),C(:,2).').^2);
% distance between edges of objects (center distance minus each radius)
D = D-R-R.';
D(D<1E-6) = NaN; % remove self-distances
[Dn Nn] = min(D,[],2); % minimize
This will find (and minimize) the distance based on the distance between centroids minus the equivalent radius of each dot. I imagine this could also be done using improfile(), but I don't see any big advantage to doing it that way.
Rahul punk
on 19 May 2021
getting error
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> test at 123
D = D-R-R.';
DGM
on 19 May 2021
Auugh. I forgot you're using an older version. Same story:
D = bsxfun(@minus,bsxfun(@minus,D,R),R.');
Anytime you see something that looks like an implicit array expansion during an elementwise operation, bsxfun() can do it.
Rahul punk
on 20 May 2021
thanks for giving me your precious time to helping my question please tell can i get the ony horizontal distances.??
DGM
on 20 May 2021
If this is the euclidean distance
D = sqrt(bsxfun(@minus,C(:,1),C(:,1).').^2 + bsxfun(@minus,C(:,2),C(:,2).').^2);
Then these are the components
Dx = bsxfun(@minus,C(:,1),C(:,1).';
Dy = bsxfun(@minus,C(:,2),C(:,2).';
Rahul punk
on 21 May 2021
Edited: Rahul punk
on 21 May 2021
could you find this distances in all blob mention in image?? i have try many method but dont get result?.if possible then all blob area find??
Image Analyst
on 21 May 2021
Edited: Image Analyst
on 21 May 2021
Distance(s) of what from what? Did you get the centroids and then use pdist2()? I can't understand what your sentences are saying. Do you want the distances, areas, both? I have almost no idea. Please explain in much more detail.
% Find centroids and areas of all blobs.
props = regionprops(mask, 'Centroid', 'Area');
% Extract the area of all blobs.
allAreas = [props.Area];
% Get the centroids in an N-by-2 list of (x,y) coordinates.
xy = vertcat(props.Centroid);
% Get the distance of every blob's centroid to every other blob's centroid.
distances = pdist2(xy, xy);
Rahul punk
on 22 May 2021
i d'nt required center distance i have only required corner to corner distances. i agree with DGm Answer.
More Answers (0)
See Also
Categories
Find more on Logical 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)