現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Minimum distance between two Irregular forms
4 ビュー (過去 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 件)
参考
カテゴリ
Help Center および File Exchange で Image Segmentation and Analysis についてさらに検索
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 (한국어)