**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# Minimum distance between two Irregular forms

1 view (last 30 days)

Show older comments

mohammed alany
on 31 Mar 2019

Commented: mohammed alany
on 1 Apr 2019

WHAT is the min distance between these two shapes?

note: i know the connecting points of big figure.

##### 0 Comments

### Accepted Answer

Image Analyst
on 31 Mar 2019

Edited: Image Analyst
on 1 Apr 2019

If you have the stats toolbox, try this:

distances = pdist2(xyShape1, xyShape2)

minDistance = min(distances(:))

[minRows, minColumns] = find(distances == minDistance)

##### 18 Comments

mohammed alany
on 31 Mar 2019

what you mean by xy?

what i have is like : points = [ 1 2, 4 6 .....] for the big shape

Image Analyst
on 31 Mar 2019

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
on 31 Mar 2019

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
on 31 Mar 2019

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
on 31 Mar 2019

Image Analyst
on 31 Mar 2019

mohammed alany
on 31 Mar 2019

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
on 31 Mar 2019

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
on 31 Mar 2019

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
on 31 Mar 2019

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
on 1 Apr 2019

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
on 1 Apr 2019

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
on 1 Apr 2019

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
on 1 Apr 2019

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
on 1 Apr 2019

[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.

### More Answers (0)

### See Also

### Tags

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

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 (한국어)