How to do manual calculation based on distance transform obtain in MATLAB? - matlab

Euclidean is distance transform from a . I am using Euclidean = bwdist(a,'euclidean');
Based on this, may i know how the calculation works? from/to what point MATLAB calculate to get the Euclidean based on MATLAB?
From formula , sqrt[(x2-x1)^2 + (y2-y1)^2], which means we need 2 points. How do MATLAB calculation for each pixel? Thank you

I think the following link explains about the function quite directly.
https://uk.mathworks.com/help/images/ref/bwdist.html
D = bwdist(BW) computes the Euclidean distance transform of the binary image BW. For each pixel in BW, the distance transform assigns a number that is the distance between that pixel and the nearest nonzero pixel of BW.
for your first point a(1,1), the nearest point is a(2,2), so the distance is sqrt(2).
for a(1,2), the nearest non-zero is a(2,2) too, so the distance is sqrt(1) = 1.
for a(2,2), the nearest non-zero is it self, so the distance is sqrt(0) = 0.
Good luck.

Related

Is there a fast method to calculate the nearest point in a data set under a point-depending distance function

I am searching for a (fast) way to calculate the nearest point y in a dataset to a given point x under a (x,y)-depending distance function.
My distance function has the form: d(x,y) = 1/f(x,y) * |||x-y||^2, where ||x|| denotes the standard Euclidean-norm. The function f(x,y) fulfills all necessary properties such that d(x,y) is a distance measurement i.e. positive, symmetric,...
For a "normal" distance function I could to some transformation on the data itself and use some k-nearest neighbor approaches. But for this case I could not find something useful. Does anyone have an idea?
Right now, I am using Julia for the implementation.
You should be able to use most standard spacial indexes (kd-tree, r-tree, quadtree, and their derivatives) as long as d(x,y) is "convex".
With "convex" I mean that a curve of equidistant points around P is convex. E.g. for Euclidean this is a circle, for Manhatten/Taxi distance it is a square.
This is required because these indexes usually partition the data into squares, rectangles or half-spaces (kd-tree), so they rely on calculating the minimum distance to a group of points by calculating the distance to the corner or sides of a bounding rectangle. As long as your distance function is convex (or at least not concave) then any index of these indexes should work.

Distance matrix in kilometres from latitude and longitude data in matlab

In matlab, I have a list of 2410 locations given by their latitude and longitude. I want to create a distance matrix in kilometres. I know how to do this in degrees but how do I do this in kilometres? I have the mapping toolbox, using 2016b. Thanks!
For example, my distance matrix in degrees looks like this:
First you need to ask your self what you mean by distance.
Do you want the euclidean distance between the points? Imagine you could tunnel through the earth from one point to the other, this is the euclidean distance between the points. To calculate this distance you need to first convert each of the lat long points to ecef points. You can do this conversion with this code (https://www.mathworks.com/matlabcentral/fileexchange/7942-covert-lat--lon--alt-to-ecef-cartesian). After you've converted each point to an ecef point you can now calculate the euclidean norm https://en.wikipedia.org/wiki/Norm_(mathematics)) between each possible pair of points.
Or do you want to calculate the distance a traveler would traverse if they were to walk along the surface of the earth. From the looks of it, this is a much more difficult problem requiring an iterative solver. Fortunately someone has already done the work of implementing an algorithm to do this for you (https://www.mathworks.com/matlabcentral/fileexchange/5379-geodetic-distance-on-wgs84-earth-ellipsoid). Note if you read the comments of this function it appears as if mathworks has already implemented a different algorithm to perform the same calculation in the mapping toolbox. To calculate the matrix you simply need to iterate over each possible pairing of lat long points and plug them into the vdist function.
Following should calculate the distance matrix for you using the vdist function above. Note I have not tested this code so you may to to correct errors.
points % assuming this is a matrix of your points [2 x N] formatted as follows
% [ lat1 , lat2, ... ]
% [ lon1 , lat2, ... ]
dist = zeros(N,N); % the resulting distance matrix
for(idx1 = 1:N)
for(idx2 = 1:N)
dist(idx1,idx2) = vdist(points(1,idx1),points(2,idx1),points(1,idx2)points(2,idx2) );
end
end
Note because the earth surface is manifold (https://en.wikipedia.org/wiki/Manifold) the results will be similar if the points are close to each other. If speed is important to you and the points are closely grouped, you may want to use the first method to calculate your distance matrix. How close together the points should be to make use of this approximation will depend on how accurate you need the results to be.

How to order one dimensional matrices base on values

I want to determine a point in space by geometry and I have math computations that gives me several theta values. After evaluating the theta values, I could get N 1 x 3 dimension matrix where N is the number of theta evaluated. Since I have my targeted point, I only need to decide which of the matrices is closest to the target with adequate focus on the three coordinates (x,y,z).
Take a view of the analysis in the figure below:
Fig 1: Determining Closest Point with all points having minimal error
It can easily be seen that the third matrix is closest using sum(abs(Matrix[x,y,z])). However, if the method is applied on another figure given below, obviously, the result is wrong.
Fig 2: One Point has closest values with 2-axes of the reference point
Looking at point B, it is closer to the reference point on y-,z- axes but just that it strayed greatly on x-axis.
So how can I evaluate the matrices and select the closest one to point of reference and adequate emphasis will be on error differences in all coordinates (x,y,z)?
If your results is in terms of (x,y,z), why don't evaluate the euclidean distance of each matrix you have obtained from the reference point?
Sort of matlab code:
Ref_point = [48.98, 20.56, -1.44];
Curr_point = [x,y,z];
Xd = (x-Ref_point(1))^2 ;
Yd = (y-Ref_point(2))^2 ;
Zd = (z-Ref_point(3))^2 ;
distance = sqrt(Xd + Yd + Zd);
%find the minimum distance

Calculate the maximum euclidean distance from a MST (minimal spanning tree)

Task: I am working in Matlab and I have to construct a dendrogram from maximum values of a matrix of Euclidean distance.
What have I done so far: I have constructed the distance matrix based on the correlation coefficients of returns of prices (this is what I have in my application). I have also built the MST based on these distances. Now I have to construct the ultrametric matrix which is obtained by defining the subdominant ultrametric distance D*ij between i and j as the maximum value of any Euclidean distance Dkl detected by moving in single steps from i to j in the MST.
CorrelMatrix=corrcoef(Returns);
DistMatrix=sqrt(2.*(1-CorrelMatrix));
DG=sparse(DistMatrix);
[ST,pred] = graphminspantree(DG,'Method','Prim');
Z = linkage(DistMatrix);
dendrogram(Z)
I am a newbie in Matlab and I do not know if there is a function or something that I should use to find the maximum distance between two nodes, and to put if after in a matrix.

SIFT feature matching through Euclidean distance

SIFT feature matching is done through a Euclidean-distance based nearest neighbor approach. Can some please explain this? Is there a calculation? If then can someone help me to calculate the Euclidean distance for my SIFT feature vector? I want to save calculated Euclidean distances to feed for neural network with some more features like roundness and color of images.
SIFT feature matching through Euclidean distance is not a difficult task. The process can be explained as follows:
Extract the SIFT keypoint descriptors for both images.
Take one keypoint descriptor (reference descriptor) from one image.
2.1 Now, find the Euclidean distances between the reference descriptor and all keypoint descriptors of the other image.
2.2 Consequently, you have the Euclidean distances from the reference descriptor to all the keypoint descriptors in image2. Arrange them in ascending order.(It implies the nearest distances for keypoint in image1 to keypoints in image2)
2.3 Now, set some threshold T ( mostly in the range of 0.3 to 0.7).
2.4 Take the ratio of the first nearest distance to the second nearest distance and if it is below the threshold T, then it is a match and, therefore, you save that index. Otherwise, there is no match.
Repeat this for all keypoint descriptors in image1.
Now you have the matches. You can plot the matches by appending the two images and then based on keypoint locations.
I think your doubt is what the euclidean distance is. The euclidean distance is the distance between two points as seen in a Euclidean (or 2 dimensional) plane.
It is very visual fo a two dimensional plane, but as SIFT descriptors are vectors of 128 dimension it gets tricky. You just have to stick to the formula (https://en.wikipedia.org/wiki/Euclidean_distance)
This is my code for calculating the euclidean distance:
for j = 1 : length(SIFT2)
euclideanDist(j) = sqrt(sum((SIFT1{i} - SIFT2{j}).^2));
end
The code will find the distance from point 'i' on the first image to all of the encountered points in the 2nd image, 'j' in this case. I store this distances in the vector euclideanDist.
The cell-arrays SIFT1 and SIFT2 contain the descriptors of each image.