Find how close is object to boundary - matlab

I'm working on a Matlab script where I have a bunch of objects (particles) within another bigger object (channel or a tube). I can get the centroids of participles and boundaries of the bigger object (stored in a matrix).
I need to figure out if the particle is in the center or the boundary of the tube. The tube has an irregular shape (think sin like figure). I couldn't find a function that can do that within Image Processing library.
Any help will be appreciated.
Thanks!

You can either use some function that tells you if a point is inside a polygon, like
https://de.mathworks.com/help/matlab/ref/inpolygon.html
Or you fill the boundary polygon with a given value, then add another value to each particle coordinate. Now every point with a value of that sum is inside the large object.
Of course you can also use some boolean logic. Large object and particles are true, background is false. Then AND link both matrices/images to get any particles inside the object.

if the boundary of the tube is a polygon you can calculate it's centroid using the formula from:
https://en.wikipedia.org/wiki/Centroid#Centroid_of_a_polygon
Than iterate over all the particles and calculate the euclidean distance between the particle and the centroid of the tube and if it is in close enough you know the particle is in the center.

Related

Measuring objects in a photo taken by calibrated cameras, knowing the size of a reference object in the photo

I am writing a program that captures real time images from a scene by two calibrated cameras (so the internal parameters of the cameras are known to us). Using two view geometry, I can find the essential matrix and use OpenCV or MATLAB to find the relative position and orientation of one camera with respect to another. Having the essential matrix, it is shown in Hartley and Zisserman's Multiple View Geometry that one can reconstruct the scene using triangulation up to scale. Now I want to use a reference length to determine the scale of reconstruction and resolve ambiguity.
I know the height of the front wall and I want to use it for determining the scale of reconstruction to measure other objects and their dimensions or their distance from the center of my first camera. How can it be done in practice?
Thanks in advance.
Edit: To add more information, I have already done linear trianglation (minimizing the algebraic error) but I am not sure if it is any useful because there is still a scale ambiguity that I don't know how to get rid of it. My ultimate goal is to recognize an object (like a Pepsi can) and separate it in a rectangular area (which is going to be written as a separate module by someone else) and then find the distance of each pixel in this rectangular area, i.e. the region of interest, to the camera. Then the distance from the camera to the object will be the minimum of the distances from the camera to the 3D coordinates of the pixels in the region of interest.
Might be a bit late, but at least for someone struggling with the same staff.
As far as I remember it is actually linear problem. You got essential matrix, which gives you rotation matrix and normalized translation vector specifying relative position of cameras. If you followed Hartley and Zissermanm you probably chose one of the cameras as origin of world coordinate system. Meaning all your triangulated points are in normalized distance from this origin. What is important is, that the direction of every triangulated point is correct.
If you have some reference in the scene (lets say height of the wall), then you just have to find this reference (2 points are enough - so opposite ends of the wall) and calculate "normalization coefficient" (sorry for terminology) as
coeff = realWorldDistanceOf2Points / distanceOfTriangulatedPoints
Once you have this coeff, just mulptiply all your triangulated points with it and you got real world points.
Example:
you know that opposite corners of the wall are 5m from each other. you find these corners in both images, triangulate them (lets call triangulated points c1 and c2), calculate their distance in the "normalized" world as ||c1 - c2|| and get the
coeff = 5 / ||c1 - c2||
and you get real 3d world points as triangulatedPoint*coeff.
Maybe easier option is to have both cameras in fixed relative position and calibrate them together by stereoCalibrate openCV/Matlab function (there is actually pretty nice GUI in Matlab for that) - it returns not just intrinsic params, but also extrinsic. But I don't know if this is your case.

Smooth circular data - Matlab

I am currently doing some image segmentation on a bone qCT picture, see for instance images below.
I am trying to find the different borders in the picture for instance the outer border separating the bone to the noisy background. In this analysis I am getting a list of points (vec(1,:) containing x values and vex(2,:) containing the y values) in random order.
To get them into order I am using using a block of code which effectively takes the first point vec(1,1),vec(1,2) and then finds the closest point among the rest of the points in the vector. And then repeats.
Now my problem is that I want to smooth the data but how do I do that as the points lie in a circular formation? (I do have the Curve Fitting Toolbox)
Not exactly a smoothing procedure, but a way to simplify your data would be to compute the boundary of the convex hull of the data.
K = convhull(O(1,:), O(2,:));
plot(O(1,K), O(2,K));
You could also consider using alpha shapes if you want more control.

Azimuth and Elevation from one moving object to another, relative to first object's orientation

I have two moving objects with position and orientation data (Euler Angles, Quaternions) relative to ECI coordinate frame. I would like to calculate AZ/EL from what I'm guessing is the "body frame" of the first object. I have attempted to convert both objects into the body frame through rotation matrices (X-Y-Z and Z-Y-X rotation sequence) and calculate a target vector AZ/EL this way but have not had success. I've also tried to get body frame positions and calculate the body axis/angles and convert back to Eulers (relative to body frame). I'm never sure how the coordinate system axes I'm supposedly creating are aligned along my object.
I have found a couple other questions like this answered with Quaternion solutions so that may be the best path to take, however my Quaternion background is weak at best and I fail to see how the computations given result in a target vector.
Any advice would be much appreciated and I'm happy to share my progress/experiences going forward.
get the current NEH transform matrix for the moving object
you must know position and at least two directions from North,East,Height(Up or Altitude) of the moving object otherwise is your problem unsolvable no matter what. This matrix/frame is called NEH (X=North,Y=East,Z=Height) or sometimes also ENU (X=East,Y=North,Z=Up). Look here transform matrix anatomy and here Earth's NEH construction and change the position and radius to match your moving object.
convert point P0 from GCS (global coordinate system) to NEH
simply: P1=Inverse(NEH)*P0 where P1 is now in NEH LCS (Local coordinate system). Both P0,P1 are in homogenous coordinates { x,y,z,w=1 } to allow multiplications with 4x4 matrix so you can compute azimut and elevation directly from it:
Azimut=atanxy(P1.x,P1.y);
Elevation=atan(P1.z/sqrt((P1.x*P1.x)+(P1.y*P1.y)));
where atanxy is mine atan2 (4 quadrant atan) first is dx then dy. I think atan2 in matlab has it in reverse.
[Notes]
Always visually check all frames (especially NEH). Just draw the 3 axises as lines of some length to validate if the result is correct. It should look like on image, just different color for each axis. You can move to next point only if NEH is OK !!!
Check atan2/atanxy operands order and also check goniometric functions units (rad,deg) to avoid confusions.

compute pitch, roll and yaw movement of an object in Matlab

I am currently working, with Matlab, on a 3D simulator whose aim is to move an object (currently it's just a simple circle) in space (using plot3).
Although it's easy to compute a trajectory without any rotation of my object, I do not manage to rotate my object around its own axis. Indeed, I have computed the 3 well-known rotation matrix but it (of course) rotate my object (represented by a set of points) around the axis of my figure (in the "world" system).
For example, the center of inertia of my object (currently the center of my circle) is I whose coordinates are (Xi,Yi,Zi). Thus, I suppose that I need to define an additional system for my object to be able to rotate my object about these 3 new axis composing such a system...
I would like something like:
[X2,Y2,Z2]=Mat*[X1,Y1,Z1] where [X1,Y1,Z1] is the coordinates of a point of my object before the rotation, [X2,Y2,Z2] the coordinates after the rotation and Mat the matrix I am looking for. Of course, the center of inertia must be unchanged whichever the rotation (yaw and/or pitch or/and roll)
However I have no idea about the way to compute such a matrix. The link below summarizes my wish.
Drawing of my problem

Calculate the perimeter of a shape specified with a cloud of points

I have a shape which you can imagine as a lake in a field observed from the top (2D). I determined the border pixels of the shape after an image processing, so that I have the coordinates of each border point.
Now I want to calculate the perimeter of this shape. My problem is that I have the points not in following order that would give a closed loop, but unordered.
How can a problem like this be solved in Matlab? (including Curve-Fitting-Toolbox etc.)
Thank you for any suggestions!
You can use the function regionprops for this.
Turn your image into a binary image with 1 inside your 'lake' and 0 outside (which you should be easily able to do, as you mention you extracted the boundaries).
Then use:
props=regionprops(YourBinaryImage, 'Perimeter');
You can then access the perimeter as follows: props.Perimeter
If you have set of 3D points with (x,y,z) coordinates, you may set z to zero and use the 2D (x,y) points to find the convex hull using convhull regardless of their order .