Best way to find the top most left coordinate point in set of Cartesian coordinates? - coordinates

I have a set of cartesian coordinates. What is the best way to find the top most left coordinate?
My approach is to find max Y and then corresponding lowest X coordinate. (Since there could be multiple points with same Ymax). This works fine but wonders if there are other cute methods?

If your x and y coordinates are not sorted, then you have to check all the input coordinates - at first your y values (for topmost, the higher the better) and then all corresponding x values for such y, but now you look for minimal x.

Related

Find closest point in matlab grid

G'day
I'm trying to program a smart way to find the closest grid points to the points along a contour.
The grid is a 2-dimensional grid, stored in x and y (which contain the x and y kilometre positions of the grid cells).
The contour is a line, made up of x and y locations, not necessarily regularly spaced.
This is shown below - the red dots are the grid, and the blue dots are the points on the contour. How do I find the indices of the red dot closest to each blue dot?
Edit - I should mention that the grid is a latitude/longitude grid, of an area fairly close to the south pole. So, the points (the red dots) are the position in metres from the south pole (using a polar stereographic representation). Since the grid is a geographic grid there is unequal grid spacing - with slightly different shaped cells (where the red dots define the vertices of the cells) due to the distortion at high latitudes.
The result is that I can't just find which row/column of the x and y matrix corresponds closest to the input point coordinates - unlike a regular grid from meshgrid, the values in the rows and columns vary...
Cheers
Dave
The usual method is to go:
for every blue point {
for every red point {
is this the closest so far
}
}
But a better way is to put the red data into a kd tree. This is a tree that splits the data along its mean, then splits the two data sets along their means etc until you have them separated into a tree structure.
This will change your searching effeciancy from O(n*m) to O(log(n)*m)
Here is a library:
http://www.mathworks.com.au/matlabcentral/fileexchange/4586-k-d-tree
This library will provide you the means to easily make a kd tree out of the data and to search for the closest point in it.
Alternatively you can use a quadtree, not as simple but the same idea. (you may have to write your own library for that)
Make sure the largest data set (in this case your red points) go into the tree as this will provide the greatest time reduction.
I think I've found a way to do it using the nearest flag of griddata.
I make a matrix that is the same size as the grid x and y matrices, but is filled with the linear indices of the corresponding matrix element. This is formed by reshaping a vector (which is 1:size(x,1)*size(x,2)) to the same dimensions as x.
I then use griddata and the nearest flag to find the linear index of the point closest to each point on my contour (blue dots). Then, simply converting back to subscript notation with ind2sub leaves me with a 2 row vectors describing the matrix subscripts for the points closest to each point on the blue-dotted contour.
This plot below shows the contour (blue dots), the grid (red dots) and the closest grid points (green dots).
This is the code snippet I used:
index_matrix1 = 1:size(x,1)*size(x,2);
index_matrix1 = reshape(index_matrix1,size(x));
lin_ind = griddata(x,y,index_matrix1,CX,CY,'nearest'); % where CX and CY are the coords of the contour
[sub_ind(1,:),sub_ind(2,:)] = ind2sub(size(x),lin_ind);
I suppose that in the stereographic representation, your points form a neat grid in r-theta coordinates. (I'm not too familiar with this, so correct me if I'm wrong. My suggestion may still apply).
For plotting you convert from the stereographic to latitude-longitude, which distorts the grid. However, for finding the nearest point, consider converting the latitude-longitude of the blue contour points into stereographic coordinates, where it is easy to determine the cell for each point using its r and theta values.
If you can index the cell in the stereographic representation, the index will be the same when you transform to another representation.
The main requirement is that under some transformation, the grid points are defined by two vectors, X and Y, so that for any x in X and y in Y, (x, y) is a grid point. Next transform both the grid and the contour points by that transformation. Then given an arbitrary point (x1, y1), we can find the appropriate grid cell by finding the closest x to x1 and the closest y to y1. Transform back to get the points in the desired coordinate system.
dsearchn: N-D nearest point search.
[k, d] = dsearchn(A,B) : returns the distances, d, to the closest points. d is a column vector of length p.
http://au.mathworks.com/help/matlab/ref/dsearchn.html?s_tid=gn_loc_drop

OpenNI range of returned coordinates

I am using the HandsGenerator class of OpenNI, and I want to use it to track the users' movements.
I've registered my own callback for getting the updated position of the hand, and everything works fine, except I can't find information about the coordinate system etc. of the returned XnPoint3D. Is there a spec somewhere that precisely specifies the X,Y,Z ranges, and perhaps scaling information (so that I would know that say a change of 100 in the XnPoint3D's X corresponds to a movement of 10 centimeters, or something).
The HandsGenerator returns real world coordinates in millimeters from the sensor. This means that depth points that are right in the middle of the depthmap will have an X and Y of 0.
A change of 100 (in X, Y, or Z) is indeed a change of 10 centimeters (100mm = 10cm).
The range of the X an Y values depends on the Z value of the hand point. Assuming you have a hand point at the top left of the depthmap (or 0,0 in projective coordinates) the possible X and Y values depend on how far away the hand is. The closer the hand, the smaller X and Y. To get the max range your hand positions can be you should choose an arbitrary max Z value and then find the X & Y values of the corners of the depth map at that distance. Or in other words - convert the projective coordinates (0,0,maxZ) and (DepthmapWidth,DepthmapHeight,maxZ) to real world coordinates. All hand points that have a Z value less than maxZ will fall between those 2 real world coordinates)
Note that you can convert projective coordinates to real world using DepthGenerator::ConvertProjectiveToRealWorld.

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.

How to get real world coordinates (x, y, z) from a distinct object using a Kinect

I have to get the real world coordinates (x, y, z) using Kinect. Actually, I want the x, y, z distance (in meters) from Kinect.
I have to get these coordinates from a unique object (e.g. a little yellow box) in the scenario, colored in a distinct color.
Here you can see an example of the scenario
I want the distance (x, y, z in meters) of the yellow object in the shelf.
Note that is not required a person (skeleton) in the scenario.
First of all, I would like to know if it is possible and simple to do?
So, I would appreciate if you send some links/code that could help me doing this task.
You would need to use both the Color Stream and the Depth Stream.
First, using the Color Stream you would need to collect an array of pixels that match the color you are looking for and then lookup the depth data from the Depth Stream for those pixels to get an average distance from the camera. That gives you the Z.
To get the X and Y you would use the math from this answer.
The Z distance (from object to kinect) you get from Position.Z of a specific Joint. So there is no problem with getting it.
The X and Y. It depends do you want to get distance from joint to joint or from joint to Kinect. You can calculate it. Use the math. You need to take angle of view of kinect and distance from it

Dividing a geographic region

I have a certain geographic region defined by the bottom left and top right coordinates. How can I divide this region into areas of 20x20km. I mean in practial the shape of the earth is not flat it's round. The bounding box is just an approximation. It's not even rectangular in actual sense. It's just an assumption. Lets say the bottomleft coordinate is given by x1,y1 and the topright coordinate is given by x2,y2, the length of x1 to x2 at y1 is different than that of the length between x1 to x2 at y2. How can I overcome this issue
Actually, I have to create a spatial meshgrid for this region using matlab's meshgrid function. So that the grids are of area 20x20km.
meshgrid(x1:deltaY:x2,y1:deltaX:y2)
As you can see I can have only one deltaX and one deltaY. I want to choose deltaX and deltaY such that the increments create grid of size 20x20km. However this deltaX and deltaY are supposed to vary based upon the location. Any suggestions?
I mean lets say deltaX=del1. Then distance between points (x1,y1) to (x1,y1+del1) is 20km. BUt when I measure the distance between points (x2,y1) to (x2, y1_del1) the distance is < 20km. The meshgrid function above does creates mesh. But the distances are not consistent. Any ideas how to overcome this issue?
Bear in mind that 20km on the surface of the earth is a REALLY short distance, about .01 radians - so the area you're looking at would be approximated as flat for anything non-scientific. Assuming it is scientific...
To get something other than monotonic steps in meshgrid you should create a function which takes as its input your desired (x,y) and maps it relative to (x_0,y_0) and (x_max,y_max) in your units of choice. Here's an inline function demonstrating the idea of using a function for meshgrid steps
step=inline('log10(x)');
[x,y]=meshgrid(step(1:10),step(1:10));
image(255*x.*y)
colormap(gray(255))
So how do you determine what the function should be? That's hard for us to answer exactly without a little more information about what your data set looks like, how you're interacting with it, and what your accuracy requirements are. If you have access to the actual location at every point, you should vary one dimension at a time (if your data grid is aligned with your latitude grid, for example) and use a curve fit with model selection techniques (akaike/bayes criterion) to find the best function for your data.