how to find the distance of all objects in given image
4 次查看(过去 30 天)
显示 更早的评论
采纳的回答
DGM
2021-5-11
编辑:DGM
2021-5-11
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 个评论
Rahul punk
2021-5-15
??? 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
2021-5-15
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
2021-5-17
编辑:Rahul punk
2021-5-17
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
2021-5-17
编辑:DGM
2021-5-17
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
2021-5-18
If you have the stats toolbox you could also use pdist2() to find the distance of every point to every other point.
DGM
2021-5-18
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
2021-5-18
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
2021-5-19
getting error
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> test at 123
D = D-R-R.';
DGM
2021-5-19
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
2021-5-20
thanks for giving me your precious time to helping my question please tell can i get the ony horizontal distances.??
DGM
2021-5-20
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
2021-5-21
编辑:Rahul punk
2021-5-21
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
2021-5-21
编辑:Image Analyst
2021-5-21
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
2021-5-22
i d'nt required center distance i have only required corner to corner distances. i agree with DGm Answer.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)