Minimum distance between two Irregular forms
조회 수: 2(최근 30일)
표시 이전 댓글
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
mohammed alany
2019년 3월 31일
what you mean by xy?
what i have is like : points = [ 1 2, 4 6 .....] for the big 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?
Walter Roberson
2019년 3월 31일
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

Walter Roberson
2019년 3월 31일
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.
Image Analyst
2019년 3월 31일
If you used the rectangle() function, then you can get the coordinates of the corners from the 'Position' array you passed in.
mohammed alany
2019년 3월 31일
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
Walter Roberson
2019년 3월 31일
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.
mohammed alany
2019년 3월 31일
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
Image Analyst
2019년 3월 31일
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???
Walter Roberson
2019년 4월 1일
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.
mohammed alany
2019년 4월 1일
shape_one = 9x2 double;
xshape_2 = 1090x1 double;
yshape_2 = 1090x1 double;
how i can get the minimum distance now?
highly appreciate your cooperations
mohammed alany
2019년 4월 1일
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.
Image Analyst
2019년 4월 1일
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.
Walter Roberson
2019년 4월 1일
[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.
추가 답변(0개)
참고 항목
범주
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)