How can we map the degree in between two points on an image IN matlab? - matlab

We know the center point of an image (say X0, Y0) and also know the observed point coordinates (say X, Y). Then we have to calculate the degree of that point (X, Y) and also the direction of that point. Finally we have to convert it into the value of compass angle.

Related

Find 3D coordinate with respect to the camera using 2D image coordinates

I need to calculate the X,Y coordinates in the world with respect to the camera using u,v coordinates in the 2D image. I am using an S7 edge camera to send a 720x480 video feed to MATLAB.
What I know: Z i.e the depth of the object from the camera, size of the camera pixels (1.4um), focal length (4.2mm)
Let's say the image point is at (u,v) = (400,400).
My approach is as follows:
Subtract the pixel value of center point (240,360) from the u,v pixel coordinates of the point in the image. This should give us the pixel coordinates with respect to the camera's optical axis (z axis). The origin is now at the center of the image. So new coordinates are: (160, -40)
Multiply the new u,v pixel values with pixel size to obtain the distance of the point from the origin in physical units. Let's call it (x,y). We get (x,y) = (0.224,-0.056) in mm units.
Use the formula X = xZ/f & Y = yZ/f to calculate X,Y coordinates in the real world with respect to the camera's optical axis.
Is my approach correct?
Your approach is going in the right way, but it would be easier if you use a more standardize approach. What we usually do is use Pinhole Camera Model to give you a transformation between the world coordinates [X, Y, Z] to the pixel [x, y]. Take a look in this guide which describes step-by-step the process of building your transformation.
Basically you have to define you Internal Camera Matrix to do the transformation:
fx and fy are your focal length scaled to use as pixel distance. You can calculate this with your FOV and the total pixel in each direction. Take a look here and here for more info.
u0 and v0 are the piercing point. Since our pixels are not centered in the [0, 0] these parameters represents a translation to the center of the image. (intersection of the optical axis with the image plane provided in pixel coordinates).
If you need, you can also add a the skew factor a, which you can use to correct shear effects of your camera. Then, the Internal Camera Matrix will be:
Since your depth is fixed, just fix your Z and continue the transformation without a problem.
Remember: If you want the inverse transformation (camera to world) just invert you Camera Matrix and be happy!
Matlab has also a very good guide for this transformation. Take a look.

How does interp2(X,Y,image,XI,YI) knows to rotate the image when XI and YI is a rotated coordinate system of X and Y? and why not use imrotate?

In radon.m the projections in the various angles are done by rotating the original image using interp2(x1,y1,padImg,x,y) where x1 and y1 are gotten using meshgrid of n points between -1 and 1 (where n is the number of the rows of the zero padded image) and x and y are gotten from x1, and y1 by multiplying the vector (x1, y1) with a rotation matrix. I don't get the idea of this manipulation of interp2. How does interp2 knows that the image itself should be rotated? Can someone explain this to me please? and also- why not use imrotate?
Interp2 does not know anything about rotation, it just interpolates at the points you put in. Plot x and y as 2d points, you will see that the rotation is already in there.

Onscreen angle of 3D vector

My math is too rusty to figure this out. I want to derive the onscreen angle (the angle as seen on the 2d screen) of a 3d vector.
Given the x and y rotation of a vector (z rotation is zero and doesn't mstter), what does the angle on screen look like?
We know when y is zero and x is positive, the angle is 90. When y is zero and x is negative the angle is -90. When y is 90, for any value of x, the angle is 180. When y is -90, for any value of x, the angle is 0.
So what the formula here so I can derive the angle for the other values of x and y rotation?
The problem, as stated, doesn't make sense. If you're holding z to zero rotation, you've converted a 3D problem to 2D already. Also, it seems the angle you're measuring is from the y-axis which is fine but will change the ultimate formula. Normally, the angle is measured from the x-axis and trigometric functions will assume that. Finally, if using Cartesian coordinates, holding y constant will not keep the angle constant (and from the system you described for x, the angle would be in the range from -90 to 90 - but exclusive of the end points).
The arctangent function mentioned above assumes an angle measured from the x-axis.
Angle can be calculated using the inverse tangent of the y/x ratio. On unity3d coordinated system (left-handed) you can get the angle by,
angle = Mathf.Rad2Deg * Mathf.Atan(y/x);
Your question is what will a 3-d vector look like.
(edit after posted added perspective info)
If you are looking at it isometrically from the z-axis, it would not matter what the z value of the vector is.
(Assuming a starting point of 0,0,0)
1,1,2 looks the same as 1,1,3.
all x,y,z1 looks the same as any x,y,z2 for any values of z1 and z2
You could create the illusion that something is coming "out of the page" by drawing higher values of z bigger. It would not change the angle, but it would be a visual hint of the z value.
Lastly, you can use Dinal24's method. You would apply the same technique twice, once for x/y, and then again with the z.
This page may be helpful: http://www.mathopenref.com/trigprobslantangle.html
Rather than code this up yourself, try to find a library that already does it, like https://processing.org/reference/PVector.html

Create depth map from 3d points

I have given 3d points of a scene or a subset of these points comprising one object of the scene. I would like to create a depth image from these points, that is the pixel value in the image encodes the distance of the corresponding 3d point to the camera.
I have found the following similar question
http://www.mathworks.in/matlabcentral/newsreader/view_thread/319097
however the answers there do not help me, since I want to use MATLAB. To get the image values is not difficult (e.g. simply compute the distance of each 3d point to the camera's origin), however I do not know how to figure out the corresponding locations in the 2d image.
I could only imagine that you project all 3d points on a plane and bin their positions on the plane in discrete, well, rectangles on the plane. Then you could average the depth value for each bin.
I could however imagine that the result of such a procedure would be a very pixelated image, not being very smooth.
How would you go about this problem?
Assuming you've corrected for camera tilt (a simple matrix multiplication if you know the angle), you can probably just follow this example
X = data(:,1);
Y = data(:,1);
Z = data(:,1);
%// This bit requires you to make some choices like the start X and Z, end X and Z and resolution (X and Z) of your desired depth map
[Xi, Zi] = meshgrid(X_start:X_res:X_end, Z_start:Z_res:Z_end);
depth_map = griddata(X,Z,Y,Xi,Zi)

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.