現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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

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)

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 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)