Calculate distance between coordinates but with restrictions - matlab

I managed to find the intersections of an arbitrary number of lines within a binary image.
Then i use a function where i detect the intersections between the lines. So now i have the coordinates of the intersections saved in an array.
Now i want to calculate the distance between the intersections(an imaginary line that connects all my intersections) but i want the distance calculation to traverse along the lines that are already on the binary image.
So the distance calculation cannot escape a line while calculating, instead it must ''walk along it''.
The imaginary path(of which eventually we calculate its distance) must walk along the already drawn lines.
EDIT** THIS IS MY INTERSECTION DETECTION ''ALGO''
clear all
pellara4=imread('C:/users/lemesios/desktop/pellara4.jpg');
blackwhitepellara=im2bw(pellara4,0.5);
I = blackwhitepellara;
C = corner(I);
num_of_rows=size(C,1);
num_of_cols=size(C,2);
for z =1:num_of_rows
k=C(z,2);
j=C(z,1);
if (I(k+1,j)==0)&& (I(k,j+1)==0) && (I(k-1,j)==0) && (I(k,j-1)==0)
imshow(I);
hold on
plot((j), (k), 'b*');
disp(k);
disp(j);
end
end

I feel this is similar to Dijkstra's algorithm. You can denote the intersection points by nodes. Then generate a mesh where each intersecting point is connected to every other intersecting point. Then if there exists a line in the binary image, assign a unit weight, otherwise assign inf i.e. infinite weight. When you have to measure distnace between m-th point and n-th point (say), then make m-th point as source and n-th point as destination and find the shortest path according to Dijkstra's algorithm.

Related

Optimizing computation of distance to triangle using barycentric coordinates

Building on the discussions here and here. I'm trying to compute the shortest distance between a 3D line and a 3D triangle.
I'm using barycentric coordinates to determine whether or not the point is inside the triangle. So given a triangle defined by vertices UVW and a line defined by point AB, I first compute the intersection of line AB with the plane defined by UVW. Let's call this intersection P and assume I've already done the checks to verify whether or not the point actually intersects the plane at all.
I then compute barycentric coordinates (S,T) such that S is defined along the edge UV and T is defined along the edge UW. Naturally, if 0≤S and 0≤T and S+T≤1 then P is on the triangle (or its edge) and my distance to the triangle is obviously zero.
If that's not true then P is outside the triangle and I need to compute the distance. The guidance of from the first link says to project point P onto all three edges to get three candidate points. Adding those points to the three triangle's vertices, you then have six points to test against.
Isn't it easier than that, though? If T<0, then don't you already know that UV is the closest edge and you only have to test against the projection of P onto that line? Similarly, if S<0 then UW would be the closest edge. If T>0 and S>0 then VW is the closest edge.
Thus based on the signs of S and T you already know the closest edge and only have to compute the distance from P to its projection onto that edge. If the projection isn't inside the triangle, then the closest point is either vertex. Thus your computations are about 1/3 of the proposed methods.
Am I missing something here, or is this a valid optimization? I'm fairly new to barycentric coordinates and their attributes.
It turns out that the problem of closest distance from a point and from a line are very similar and can both be reduced to a pure 2D problem.
Distance from a point
By Pythagoras, the squared distance from a point to a point of the triangle is the sum of the squared distance to the plane of support of the triangle and the squared distance of the projection of the point to that plane.
The latter distance is precisely the distance from the normal line to the triangle.
Distance from a line
Looking in the direction of the line, you see the projected triangle and the line is reduced to a single point. The requested 3D distance is equal to the 2D distance seen on the projection.
To obtain the desired coordinates, you use an auxiliary coordinate frame such that Z is in the direction of the line (and XY is a perpendicular plane); for convenience, choose the origin of the new frame to be on the line. Then by just ignoring Z, you get a planar problem in XY. The change of coordinates is an affine tranformation.
Point vs. triangle
Consider the three triangles formed by the origin (projection of the point/line) and a pair of triangle vertices (taken in cyclic order). The signed area of these triangles is a mere 2x2 determinant.
If the three areas have the same sign, the point is inside. Otherwise, the signs tell you where you are among the six surrounding regions, either past an edge or past a vertex.
On the upper figure, the point is inside (three positive areas). On the other figure, it is outside of the top-right edge (one negative area). Also note that dividing an area by the length of the corresponding side, you get the distance to the side. (Factor 2 omitted.)
The total work is
compute the affine frame;
convert the coordinates of the 3 or 4 points;
compute the three signed areas;
if inside, you are done;
otherwise, if in an edge region, compute a distance to a line and two distances to points;
otherwise you are in a vertex region, compute two distances to lines and one distance to vertex.

Intersection of segment with polygon

I have to create a function in MATLAB that performs the following task:
Input:
p polygon in the form
p = [x1,y1; x2,y2; x3,y3; x4,y4...]
s struct with the segment from A to B
s = struct('A',[x,y],'B'[u,w])
Return:
1) An integer indicating how many intersections there are between the segment and the polygon (e.g., 0,1,2)
2) A new segment from A to B, where A is the first intersection or the initial point of the input segment and B the second point of the intersection or the last point of the segment input.
I have an idea on how to do it by using the function inpolygon. I have been reading how to use this function, and know that to use that, I should provide a query point and the coordinates of the polygon vertices. It will return 1 or 0 depending on whether it is inside or not.
My question is, how can I get the query point of the segment that is placed exactly in the boundary (in the case that the segment intersects with it)?
If you have the Mapping Toolbox installed, you could use polyxpoly. As this is a rather basic problem, there are quite a few free MATLAB-codes out there on the File Exchange. Here is what I found for the search term 'polygon intersect':
2D Polygon edges intersection by Bruno Luong
Find the intersection points of the edges of two 2D polygons, a simple function made to follow up a Newsgroup discussion
Curve Intersect 2 by Sebastian Hölz
This file is based on the Curve Intersect function by Duane Hanselman. It extends the scope of the function to handle arbitrary lines / polygons, which may also have vertical segments or segments with non-increasing x-values.
Curve intersections by NS
While a few other functions already exist in FEX that compute the
intersection points of curves, this short piece of code was written
with speed being the highest priority. No loops are used throughout,
taking full advantage of MATLAB's vectorization capabilities
Fast and Robust Curve Intersections by Douglas Schwarz
This function computes the (x,y) locations where two curves intersect. The curves can be broken with NaNs or have vertical segments. It is also very fast (at least on data that represents what I think is a typical application).
geom2d by David Legland
[...] derive new shapes: intersection between 2 lines, between a line and a circle, parallel and perpendicular lines

Plotting a Line from a known point to points in a fitted curve in MATLAB

I have a set of data whose points I have plotted and fitted using a power of 2 fit in MATLAB. I'm trying to draw 3 lines to that curve as tangential lines. Each of these lines start from the co-ordinates of say, (x,y): (2,0) (4,0) (9,0).
Is it possible for MATLAB to draw lines from the curve to the first known point until the line has only one solution (tangent to the curve) with the curve?
I feel that this requires some sort of specified interval which tells MATLAB to step the co-ordinates until it finds the closest point. Does anyone know if this has been done or can be done at all?
From a point not lying on the curve, you want to draw a line that is tangent to it. In case of a convex function like y=2^x this is only possible from a point under the curve (not over it).
Since you already have the point (call it (a,b)), you need the slope of such a line. The slope is determined by the values (y-b)/(x-a) where (x,y) runs over the curve. Specifically, the "forward-looking" tangent has the slope equal to the minimum of (y-b)/(x-a) over all x>a. And the "backward-looking " tangent has the slope equal to the maximum of (y-b)/(x-a) over all x
Here is a very straightforward implementation of the above: I used find to restrict the search to either x>a or x<a and took min and max to find the slopes.
x = 0:0.01:4;
y = 2.^x;
a = 2;
b = 3;
k = min((y(find(x>a))-b)./(x(find(x>a))-a));
plot(x,y)
hold on
plot(x,k*(x-a)+b,'r')
k = max((y(find(x<a))-b)./(x(find(x<a))-a));
plot(x,k*(x-a)+b,'g')

How can I calculate the distance between two points along a complex path?

I have a set of lines that define a W shape. On each line I have then defined a set of M points that separated by a fixed width. Now I'd like to calculate the MxM matrix of distances where the value of (i,j) contains the along-the-path-distance between the points i and j.
Here is an of the along-the-path-distance between two points.
How can I calculate this distance in Matlab?
It mostly comes down to whether a simple algorithm that only works for this sort of shape is sufficient or if you'll need to also find the distance with different complex paths. If it's just this shape then it's rather trivial
Here the yHoriz is the y value of the horizontal line that connects the vertical lines and x and y I and J are the x and y values of the points i and j.
distance= abs(yI - yHoriz) + abs(xJ-xI) + abs(yJ - yHoriz)
You would need to check if the the points are on the same vertical line and just find the difference in their y values instead of the above value if they are on the same line.

Matlab - Propagate points orthogonally on to the edge of shape boundaries

I have a set of points which I want to propagate on to the edge of shape boundary defined by a binary image. The shape boundary is defined by a 1px wide white edge.
I have the coordinates of these points stored in a 2 row by n column matrix. The shape forms a concave boundary with no holes within itself made of around 2500 points. I have approximately 80 to 150 points that I wish to propagate on the shape boundary.
I want to cast a ray from each point from the set of points in an orthogonal direction and detect at which point it intersects the shape boundary at. The orthogonal direction has already been determined. For the required purposes it is calculated taking the normal of the contour calculated for point, using point-1 and point+1.
What would be the best method to do this?
Are there some sort of ray tracing algorithms that could be used?
Thank you very much in advance for any help!
EDIT: I have tried to make the question much clearer and added a image describing the problem. In the image the grey line represents the shape contour, the red dots the points
I want to propagate and the green line an imaginary orthongally cast ray.
alt text http://img504.imageshack.us/img504/3107/orth.png
ANOTHER EDIT: For clarification I have posted the code used to calculate the normals for each point. Where the xt and yt are vectors storing the coordinates for each point. After calculating the normal value it can be propagated by using the linspace function and the requested length of the orthogonal line.
%#derivaties of contour
dx=[xt(2)-xt(1) (xt(3:end)-xt(1:end-2))/2 xt(end)-xt(end-1)];
dy=[yt(2)-yt(1) (yt(3:end)-yt(1:end-2))/2 yt(end)-yt(end-1)];
%#normals of contourpoints
l=sqrt(dx.^2+dy.^2);
nx = -dy./l;
ny = dx./l;
normals = [nx,ny];
It depends on how many unit vectors you want to test against one shape. If you have one shape and many tests, the easiest thing to do is probably to convert your shape coordinates to polar coordinates which implicitly represent your solution already. This may not be a very effective solution however if you have different shapes and only a few tests for every shape.
Update based on the edited question:
If the rays can start from arbitrary points, not only from the origin, you have to test against all the points. This can be done easily by transforming your shape boundary such that your ray to test starts in the origin in either coordinate direction (positive x in my example code)
% vector of shape boundary points (assumed to be image coordinates, i.e. integers)
shapeBoundary = [xs, ys];
% define the start point and direction you want to test
startPoint = [xsp, ysp];
testVector = unit([xv, yv]);
% now transform the shape boundary
shapeBoundaryTrans(:,1) = shapeBoundary(:,1)-startPoint(1);
shapeBoundaryTrans(:,2) = shapeBoundary(:,2)-startPoint(2);
rotMatrix = [testVector(2), testVector(1); ...
testVector(-1), testVector(2)];
% somewhat strange transformation to keep it vectorized
shapeBoundaryTrans = shapeBoundaryTrans * rotMatrix';
% now the test is easy: find the points close to the positive x-axis
selector = (abs(shapeBoundaryTrans(:,2)) < 0.5) & (shapeBoundaryTrans(:,1) > 0);
shapeBoundaryTrans(:,2) = 1:size(shapeBoundaryTrans, 1)';
shapeBoundaryReduced = shapeBoundaryTrans(selector, :);
if (isempty(shapeBoundaryReduced))
[dummy, idx] = min(shapeBoundaryReduced(:, 1));
collIdx = shapeBoundaryReduced(idx, 2);
% you have a collision with point collIdx of your shapeBoundary
else
% no collision
end
This could be done in a nicer way probably, but you get the idea...
If I understand your problem correctly (project each point onto the closest point of the shape boundary), you can
use sub2ind to convert the "2 row by n column matrix" description to a BW image with white pixels, something like
myimage=zeros(imagesize);
myimage(imagesize, x_coords, y_coords) = 1
use imfill to fill the outside of the boundary
run [D,L] = bwdist(BW) on the resulting image, and just read the answers from L.
Should be fairly straightforward.