Detect the angle of a surface in real-time - python

My project is about image detection. I am using 1 camera and have 2 shapes (rectangle and triangle) placed on a flat surface. I have successfully been able to:
Detect the contours
Detect the area of the contours
Make a relation between both areas and the distances, to be able to calculate both distances from the camera
Calculate the lengths, corner coordinates and the angle of each corner in both shapes
Now I need to calculate the angle of rotation of the whole surface when rotated around the x-axis (up & down), y-axis (left & right) and both axes (oblique). This can be seen in the gif I provided. The default angle (0 degrees) is when the surface is parallel to the camera, and any rotation would mean that an angle around 1 or both axes is present.
P.S When the surface is rotating around both axes (oblique), both angles have to be detected separately. For example, I would get 14° x-axis & 27° y-axis.
Any help would be appreciated.

Related

Extract pixels within quarter circle in python

I have an image which I want to extract the pixels of the specific part of that. This part is a quarter circle, and my desire is to obtain the pixels of that.
I have the coordinates of the center and points which the lines connected to the circle. How is it possible to extract one quarter and ignore other parts?
The disk has the equation (X-Xc)²+(Y-Yc)²≤R².
The half planes meeting at the center have equations c(X-Xc) + s(Y-Yc)≥0 where c and s are the cosine and sine of the angle.
Hence, scan the image (or just the bounding box of the circle) and consider the pixels (X, Y) such that the three constraints are satisfied.

How do you approximate a shape with fixed angle polygon in OpenCV?

I have some contours for example the three colored shapes in this image.
I would like to approximate the boundary of each with a polygon WITH the constraint that the interior angles of the polygon are some fixed values. So the angle should snap to say {45 or 90} degrees and also ensure no edge is less that a certain number of pixels .e.g 50 - so there aren't many tiny edges. So the result for the green contour would be:
Is this possible in OpenCV?

Approximate 4 circles inside ellipse to get the radii

I need the approximate radii of the following ellipse.
The bottom/top and left/right radii should be the same nevertheless need to be checked. Which means 4 radii should be the result of my code. I did the following in paint, the green circle should give me the top radius and red the left (the right and bottom one aren't drawn here).
The idea I'm working on is to crop the image (left/right/top/bottom side) and approximate circles to the cropped images. With the cv2.findContours-feature some white pixels get recognized as highlighted here.
Is there a way to approximate my drawn red circle from above with these given coordinates? The problems I've seen on the internet are all with a given center point or angle which I don't have. Is there a cv2 function that draws circles with only some given coordinates or something similar?
Use this function : cv2.fitEllipse(points) and pass contour points -Ziri
Yes this did the trick. I got the radii after your function with:
(x, y), radius = cv2.minEnclosingCircle(i)

Maths/Python - given a sphere, plot sequential points around the sphere?

I am trying to rotate a vtk camera around its focal point. The aim being to 'orbit' the model.
I'm using the camera.SetPosiiton(x, y, z) call to set the camera location, and I know I can do the same at each update period in my render window.
The focal point has the location (0, 0, 0), and some other bounding box getting gives me my initial camera (x, y, z) location. The distance from the focal point (0, 0, 0) to the camera location 9x, y, z) describes the radius the of the sphere.
In my head, this essentially moving the camera in steps around the point (0, 0, 0) and I am presuming there is a maths function I could use to feed it my starting camera point, and work out my next camera location.
This should result in the model appearing to spin in space. My camera view is offset from all x, y, z, planes, making it a 3d problem, not a 2d problem. However, I do want my camera to remain the same distance from the model (focal point)
What I am trying to achieve is like this:- take a pencil (my model is long and narrow). Hold it in your finger tips at arms length, tip pointing to the ceiling. Tilt the pencil by ~30 degrees. This is the camera start position. Rotate the pencil body in your fingers, maintaining tilt angle, and the distance from your eye.
THis post looks helpful: Plotting a point on the edge of a sphere however, this assumes you know the radius to get to the initial x, y location.
Could some one point me towards the maths I need to do this, my maths is horribly rusty.
It seems what you want is to rotate a vector about an axis, this can be most easily done using a rotation matrix
So, if your desired axis of rotation is tilted 30 degrees from the z axis on the zx plane, your axis of rotation is (cos(pi/6),0,sin(pi/6)), increment the rotation angle, plug that into the rotation matrix to get matrix R, the new camera position vector will be R*(x,y,z)'
Start off with the points (+-1,0,0), (0,+-1,0), (0,0,+-1). These form two Pyramids with all the points on the unit sphere.
Now you can take the midpoints of each triangle, and project out so they lie on the unit sphere too. For each triangle this now gives you 3 new triangles, and you can repeat the process.
The alternative to the midpoint of the triangle is to take the midpoints of each side, and join them up. That gives 3 new points that can be projected out to the unit circle. This gives you 4 triangles for each sub division.
Repeat as many times as you need.

time series for binary shapes

I have been working around extracting the time series from shapes based on distances to center of mass clockwise starting from angle 0 to 360.
My Implementation that arranges contour points based on their angle to the [1,0], vector might be good for some shapes but is not useful for shapes that has much articulation. Consider the following code:
im = Image.open(os.path.join(path,filename))
im = im.filter(ifilter.MedianFilter)
contim = im.filter(ifilter.CONTOUR)
contim = contim[1:-1,1:-1] # this is because borders are extracted here as contours
contpts = np.where(contim ==0)
contpts = np.vstack((contpts[0],contpts[1])) # Just need to arrange these points clockwise with respect to the center of mass of the shape
Can anyone direct me to how I can extract that feature from any shape where I can start from a point and keep going along the contour to extract all the distances to the center of mass of the shape.
For more information about the feature, please view this paper: "LB_Keogh Supports Exact Indexing of Shapes under Rotation Invariance with Arbitrary Representations and Distance Measures"
If I understood, there's a geometrical figure in a discretized plane, represented as a matrix. If the entry is 1, you're inside the figure. If it's 0, you're outside. He wants to determine de distance between the edge of the figure and the center of the figure for all points in the edge. He parametrized it with a polar coordinate system. The center of the figure is the origin and now he wants to get the distance to the border as a function of the angle. This is what he calls his "time series".
Is this correct?
If yes, couldn't you just:
1. determine the center of mass,
2. reposition the origin to match the center of mass.
3. start angle at 0
4. r = 0
5. for each angle in [0,1,...,360]
1. If you're in inside the figure, increase r until you reach the border.
2. If you're outside the figure, decrease r until you reach the border.
3. When you reach the border, d(angle) = r
It the figure have a more or less continuous border, this will follow the contour.
Would this work?

Categories