이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
how to find the distance of all objects in given image
조회 수: 9 (최근 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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)