how to find the distance of all objects in given image

 Respuesta aceptada

DGM
DGM el 11 de Mayo de 2021
Editada: DGM el 11 de Mayo de 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 comentarios

??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> te4st at 9
D = sqrt((C(:,1)-C(:,1).').^2 + (C(:,2)-C(:,2).').^2);
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
Rahul punk el 17 de Mayo de 2021
Editada: Rahul punk el 17 de Mayo de 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
DGM el 17 de Mayo de 2021
Editada: DGM el 17 de Mayo de 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
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)
If you have the stats toolbox you could also use pdist2() to find the distance of every point to every other point.
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.
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.
getting error
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> test at 123
D = D-R-R.';
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.
thanks for giving me your precious time to helping my question please tell can i get the ony horizontal distances.??
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
Rahul punk el 21 de Mayo de 2021
Editada: Rahul punk el 21 de Mayo de 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
Image Analyst el 21 de Mayo de 2021
Editada: Image Analyst el 21 de Mayo de 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);
DGM
DGM el 21 de Mayo de 2021
I don't know what that means.
i d'nt required center distance i have only required corner to corner distances. i agree with DGm Answer.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 11 de Mayo de 2021

Comentada:

el 22 de Mayo de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by