Minimum distance between two Irregular forms
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
WHAT is the min distance between these two shapes?
note: i know the connecting points of big figure.

채택된 답변
Image Analyst
2019년 3월 31일
편집: Image Analyst
2019년 4월 1일
If you have the stats toolbox, try this:
distances = pdist2(xyShape1, xyShape2)
minDistance = min(distances(:))
[minRows, minColumns] = find(distances == minDistance)
댓글 수: 18
what you mean by xy?
what i have is like : points = [ 1 2, 4 6 .....] for the big shape
i donot have the xy for small shape
Image Analyst
2019년 3월 31일
편집: Image Analyst
2019년 3월 31일
Two non-useful comments.
Exactly what DO you have? An image? Set(s) of coordinates? Exactly how are you defining the smaller, square shape?
Note that pdist2 would most typically be used for the case where you have the coordinates of each of the vertices, and you want to find the two vertices that are closest to each other. That would not be the same as the two closest points: notice for example that the upper left corner of the rectangle is closer to an edge than to a vertex.
pdist2() can also be successfully used if you have the coordinates of every pixel in the objects, in which case it will return distances between pixels, which might not be exactly the same as theoretical distance that might apply for functions that the pixelated versions approximate. For one thing, pdist2 is happy to say that adjacent pixels are either distance 1 or sqrt(2) from each other rather than say that the "distance" between them is 0, because pdist2 would be looking at distances between pixel centers.
If what you have is coordinates of the vertices but you want to know distance to closest approach implied as if there are lines connecting vertices, then there are multiple approaches. One of the approaches is to use something like poly2mask() to render a polygon into pixels, after which you can use the pdist2() approach. There are also more mathematical approaches.
mohammed alany
2019년 3월 31일
편집: mohammed alany
2019년 3월 31일
Dear Image Analyst
i have image, like this below.
then i made some caculation till i get 10 X and Y points in matrix, which is is represent the shape of petagon
as a resulat
petagon draw from points in matrix p=[ 22. 159, 21 162, 100 131, ....]
rectangle inseart from mycomputer as JPG image

Use poly2mask to "draw" the pentagon into a new image. find() the locations of the pixels in the original image, and find() the locations of the pixels in the new image that has the pentagon, and then pdist2() between the two like Image Analyst showed.
If you used the rectangle() function, then you can get the coordinates of the corners from the 'Position' array you passed in.
Dear all
how i can get the coordinates of any shape from image, i.e. i have JPG image in matlab and i would like to know the coordinates of it
img = imread('YourImage.jpg');
bw = im2bw(img);
[row_coords, column_coords] = find(bw);
Now for any given index, K, img(row_coords(K), column_coords(K), :) was non-zero.
Caution: this code finds white pixels. If your image is black on white, then you would use find(~bw) to find the black pixels.
Dear Walter Roberson
thank you for your replay, and sorry for wasting your time
But, how i can redraw these '[row coords, column coords]'to check if this is the actual coordinates of my shape
There is no real need to, but you could do
checkImage = false(size(bw));
for k = 1 : length(row_coords)
checkImage(row_coords(k), column_coords(k)) = true;
end
imshow(checkImage);
I suggest that you don't do this - it's not needed.
Again, why can't you simply attach your image and script???
Or you could
scatter(column_coords, row_coords, 'ks', 'filled')
Notice that the column coordinates are before the row coordinates for this call. Column coordinates correspond to X and row coordinates corespond to Y.
shape_one = 9x2 double;
xshape_2 = 1090x1 double;
yshape_2 = 1090x1 double;
how i can get the minimum distance now?
highly appreciate your cooperations
distances = pdist2(shape_one, [xshape_2, yshape_2]);
this can show you the distance. what i want is the minimun distance
If i used
xyShape1 = [xpic(:), ypic(:)];
xyShape2 = im;
distances = pdist2(pathShape2, xyShape1);
minDistance = min(distances);
[minRows, minColumns] = find(distances == minDistance)
matlab show me error says
Error using ==
Matrix dimensions must agree.
It looks like it should work if you use distances(:):
minDistance = min(distances(:))
If not, attach your im, xpic, and ypic in a .mat file
save('answers.mat', 'im', 'xpic', 'ypic');
Then upload 'answers.mat' with the paper clip icon. Note: im must be the 9-by-2 double array, not an image.
[minDistance, minColumns] = min(distances);
minColumns is already a vector.
Note: you might need min(distances,2) depending on whether you need the minimum distance from each point in the first to any point in the second, or the minimum distance from each point in the second to any point in the first.
When you do a single min() call and the try to use find(), then that could be appropriate for the case where one point might happen to be exactly the same minimum distance to two more or points in the other shape, and you need to know all of them. In a situation such as that, you would generally get a different number of minimum points for each, which is a bit tricky to represent in a rectangular numeric matrix. Is this the situation that applies for you, that you need to know all of the points that are the same minimum distance? If so then typically a loop would be easiest.
many thanks, now it's work correctly.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Image Arithmetic에 대해 자세히 알아보기
참고 항목
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)
