I am runnng the motion detection algorithm against a video (file) and following the code sample motion detection, and trying to find the angle of each component and the overall motion. I do get a motion value back, with blobs etc., but the motion direction of each component always is always 0 degrees or 360 degrees and make no sense. What could I be doing wrong? Please help, thanks.
This is the constructor
_motionHistory = new MotionHistory(
10.0, //in second, the duration of motion history you wants to keep
0.05, //in second, parameter for cvCalcMotionGradient
0.5); //in second, parameter for cvCalcMotionGradient
The following is the code for looping through the motion components:
foreach (MCvConnectedComp comp in motionComponents)
{
//reject the components that have small area;
if (comp.area < 1) continue;
// find the angle and motion pixel count of the specific area
double angle, motionPixelCount;
_motionHistory.MotionInfo(comp.rect, out angle, out motionPixelCount);
string motion_direction = GetMotionDescriptor(comp.rect);
Console.writeline (motion_direction);
}
// find and draw the overall motion angle
double overallAngle, overallMotionPixelCount;
_motionHistory.MotionInfo(motionMask.ROI, out overallAngle, out overallMotionPixelCount);
And this where I get my motion descriptor angle
private string GetMotionDescriptor(Rectangle motionRegion)
{
float circleRadius = (motionRegion.Width + motionRegion.Height) >> 2;
Point center = new Point(motionRegion.X + motionRegion.Width >> 1, motionRegion.Y + motionRegion.Height >> 1);
int xDirection = (int)(Math.Cos(angle * (Math.PI / 180.0)) * circleRadius);
int yDirection = (int)(Math.Sin(angle * (Math.PI / 180.0)) * circleRadius);
//double movementAngle = Math.Atan(xDirection / yDirection) * 180 / Math.PI;
Point pointOnCircle = new Point(center.X + xDirection, center.Y - yDirection);
double slope = (double)(pointOnCircle.Y - center.Y)/(double)(pointOnCircle.X - center.X);
double ang = Math.Atan(slope) * 180/Math.PI;
return (ang).ToString() + " degrees";
}
aha! I figured out the reason and posting here if anyone is running into the same problem. The
_motionHistory = new MotionHistory(mhi, maxDelta, minDelta);
Should be adjusted to the frame rate and the motion. The trick lies in the 3 params
(1) motion history to keep, (2) max time delta, (3) min time delta.
They need to be adjusted in some way to reflect the motion you wish to capture.
Hope that helps.
Related
I've been stuck with rotating an asset with an offset.
Sstart = e.GetPosition(dial);
if (dial.IsStylusCaptured)
{
AngleRot = Math.Atan2((Y - Sstart.Y) , (X - Sstart.X));
radAngle = AngleRot / Math.PI * 180 + 180;
radAngle = radAngle - AthetaD;
di.RenderTransform = new RotateTransform(radAngle + 90);
}
Using this I was able to rotate my object from 0° with an offset of x to angle theta. But when I make a second rotation, instead of it rotating from angle theta with an offset x it resets the object back to 0°. How can I make it so the offset is always from theta and not 0°?
Here I Rotate with an offset angle
Here my angle resets back to 0° instead of moving from -56°
Let current direction vector is (cx, cy).
In the beginning set it into (1,0) or another needed value.
When you rotated dial, you have new direction
nx = X - Sstart.X
ny = Y - Sstart.Y
To rotate current direction vector to new one, you need to calculate relative angle. This approach uses cross product and dot product of vectors. Perhaps you'll need to change sign of the first expression
Angle = Math.Atan2(nx * cy - ny * cx, cx * nx + cy * ny)
..apply rotation by Angle
After rotation remember current direction to use it later
cx = nx
cy = ny
First of all, here is the code:
decimal gravity = decimal.Parse(gforce.Text) / 1000;
decimal speed = decimal.Parse(initialSpeed.Text) / 1000;
decimal newAngle = DegreesToRadians(decimal.Parse(angle.Text));
double doubleNewAngle = (double)newAngle;
decimal Px = Math.Round(((decimal)Math.Cos(doubleNewAngle) * 0.001M), 13);
decimal Py = Math.Round(((decimal)Math.Sin(doubleNewAngle) * 0.001M), 13);
string PxTemp = "";
for(decimal t = 0.001M; Py > 0; t = t + 0.001M)
{
gravity = gravity + gravity * 0.001M;
Py = Py + speed - gravity;
Px = (Px + speed * Px);
graphics.DrawArc(new Pen(Color.Magenta, 10f), 45 + (float)Px, 475 - (float)Py, 1, 1, 0, 360);
try
{
graphics.DrawArc(new Pen(Color.Magenta, 10f), 45 + (float)Px, 475 - (float)Py, 1, 1, 0, 360);
}
catch
{
MessageBox.Show("Error Px: " + Px.ToString() + " Py: " + Py.ToString(), "Error");
this.Close();
}
I am attempting to create a projectile simulator, I have successfully created the effect of gravity and acceleration on the y-axis. But however when applying the speed to the x axis(making the speed depend on the angle) I am having trouble. I can make it so every second the projectile moves 1 metre but for it to be correct the projectiles' speed across the x-axis should depend on the speed AND THE ANGLE.
To achieve this I have done:
Px = Px + (speed * Px)
Where Px is the value of distance across the axis Cosine of the angle:
decimal Px = Math.Round(((decimal)Math.Cos(doubleNewAngle) * 0.001M), 13);
When I do
Px = Px + (speed * Px)
The value returns some huge number for example 4412651515851.41214244121, I at first assumed this was because Px was going beyond its precision point but any rounding attempts I have made have failed, How should I achieve a correct Px number?
Here is an image to visualise it:
Any help would be greatly appreciated, I have been struggling all day and I couldn't find anything on-line. Thanks in advance.
The laws of motion are very different to the ones you use:
y'' = -g --> y(t) = y0 + vy0*t - g/2*t*t
x'' = 0 --> x(t) = x0 + vx0*t
These are the solutions for motion without air friction. Most complications of the equation of motion require numerical integration of the ODE.
The initial velocities vx0,vy0 are what you initially compute in Px,Py. But probably you should use
vx0 = speed*cos(angle)
vy0 = speed*sin(angle)
to get the initial velocity compatible with the inputs. Some additional unit conversions may be required.
For a useful tool to help workout the proper calculations.http://www.mrmont.com/teachers/physicsteachershelper-proj.html
I have to display stl models with openGL. (SharpGL.) I'd like to set the initial view, so that the model is at the center of the screen and approximately fills it. I've calculated the bounding cube of the models and set the view like this: (sceneBox is a Rect3D - it stores the location of the left-back-bottom corner and the sizes)
// Calculate viewport properties
double left = sceneBox.X;
double right = sceneBox.X + sceneBox.SizeX;
double bottom = sceneBox.Y;
double top = sceneBox.Y + sceneBox.SizeY;
double zNear = 1.0;
double zFar = zNear + 3 * sceneBox.SizeZ;
double aspect = (double)this.ViewportSize.Width / (double)this.ViewportSize.Height;
if ( aspect < 1.0 ) {
bottom /= aspect;
top /= aspect;
} else {
left *= aspect;
right *= aspect;
}
// Create a perspective transformation.
gl.Frustum(
left / ZoomFactor,
right / ZoomFactor,
bottom / ZoomFactor,
top / ZoomFactor,
zNear,
zFar);
// Use the 'look at' helper function to position and aim the camera.
gl.LookAt(
0, 0, 2 * sceneBox.SizeZ,
sceneBox.X + 0.5 * sceneBox.SizeX, sceneBox.Y + 0.5 * sceneBox.SizeY, sceneBox.Z - 0.5 * sceneBox.SizeZ,
0, 1, 0);
This works nice with my small, hand-made test model: (it has a box size of 2*2*2 units)
This is exactly what I want. (The yellow lines show the bounding box)
But, when I load an stl model, which is about 60*60*60 units big, I get this:
It's very small and too far up.
What should I change to make it work?
Here's the full thing: https://dl.dropbox.com/u/17798054/program.zip
You can find this model in the zip as well. The quoted code is in KRGRAAT.SZE.Control.Engine.GLEngine.UpdateView()
Apparently the problem are the arguments you are using in lookAt function. If you have calculated bounding cube all you need to do is to place it in the distance (eyeZ) from the camera of
sizeX/tan(angleOfPerspective)
where sizeX is width of Quad of which cube is built, angleOfPerspective is first parameter of GlPerspective of course having centerX == posX == centreX of the front quad and centerY == posY == centreY of the front quad and frustum is not necessary
lookAt reference http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml
So, to clarify Arek's answer, this is how I fixed it:
// Calculate viewport properties
double zNear = 1.0;
double zFar = zNear + 10 * sceneBox.SizeZ; // had to increase zFar
double aspect = (double)this.ViewportSize.Width / (double)this.ViewportSize.Height;
double angleOfPerspective = 60.0;
double centerX = sceneBox.X + 0.5 * sceneBox.SizeX;
double centerY = sceneBox.Y + 0.5 * sceneBox.SizeY;
double centerZ = sceneBox.Z + 0.5 * sceneBox.SizeZ;
// Create a perspective transformation.
gl.Perspective( // swapped frustum for perspective
angleOfPerspective / ZoomFactor, // moved zooming here
aspect,
zNear,
zFar);
// Use the 'look at' helper function to position and aim the camera.
gl.LookAt(
centerX, centerY, sceneBox.SizeX / Math.Tan(angleOfPerspective), // changed eye position
centerX, centerY, -centerZ,
0, 1, 0);
I want to find the GeoCoordinate of the place that is 4000km right to my position, name it newLocation.
I need that in order to convert the result into ViewportPoint like this:
System.Windows.Point p1 =
mapControl.ConvertGeoCoordinateToViewportPoint(newLocation);
and assuming my location is defined as:
System.Windows.Point p2 =
radiusMap.ConvertGeoCoordinateToViewportPoint(myLocation);
then I can simply add a circle centered on my location, and the border passes from newLocation that I need to calculate, like this:
double radius = getDistance(p1, p2);
Ellipse circle = new Ellipse();
circle.Width = radius * 2;
circle.Height = radius * 2;
circle.Opacity = 0.4;
circle.Fill = new SolidColorBrush(Colors.Green);
MapOverlay mapOverLay = new MapOverlay();
mapOverLay.PositionOrigin = new System.Windows.Point(0.5, 0.5);
mapOverLay.Content = circle;
mapOverLay.GeoCoordinate = myLocation;
MyLayer.Add(mapOverLay);
radiusMap.Layers.Add(MyLayer);
So, calculating newLocation is all what I need.
Notes:
I tried this solution but there is a limitation.
We can do the following in visual studio:
GeoCoordinate location1 = ..., location2 = ...;
double distance = location1.GetDistanceTo(location2);
I wonder if the inverse is already implemented.
UPDATE: I found the implementation of GetDistanceTo(...) and here it is:
double d = this.Latitude * 0.017453292519943295;
double num3 = this.Longitude * 0.017453292519943295;
double num4 = other.Latitude * 0.017453292519943295;
double num5 = other.Longitude * 0.017453292519943295;
double num6 = num5 - num3;
double num7 = num4 - d;
double num8 = Math.Pow(Math.Sin(num7 / 2.0), 2.0) + ((Math.Cos(d) * Math.Cos(num4)) * Math.Pow(Math.Sin(num6 / 2.0), 2.0));
double num9 = 2.0 * Math.Atan2(Math.Sqrt(num8), Math.Sqrt(1.0 - num8));
return (6376500.0 * num9);
I wonder if getting the inverse is possible or not. i.e. giving it the geocoordinates of me, and the distance from me, so that it returns the geocoordinates of the new location.
There are of course an infinite number of points (around a circle) which are 400km from an initial starting point, you will need to define both the distance and the bearing in order to reduce this to a single point.
If you can assume the Earth to be a sphere, you can travel around a great circle as shown
φ2 = asin( sin(φ1)*cos(d/R) + cos(φ1)*sin(d/R)*cos(θ) )
λ2 = λ1 + atan2( sin(θ)*sin(d/R)*cos(φ1), cos(d/R)−sin(φ1)*sin(φ2) )
where
φ is latitude
λ is longitude
θ is the bearing (in radians, clockwise from north)
d is the distance traveled
R is the earth’s radius 6 378.1 kilometers
d/R is the angular distance, in radians
So plug in your values to calculate the new location φ2, λ2
If by "right to my position" you mean due East, θ will be 90 degrees = Π / 2
Of course there is a hack you could use to find a point due East. One degree of longitude at the Equator = 110.57 km . Hence you could calculate an upper bound and then interate down to a reasonable threshold.
Take your start point - for 400km, add 3.61 degrees.
Check the distance with the distanceTo() method
If it is too big, chop off a part of a degree.
Repeat 2)+ 3) until within an acceptable threshold.
I am writing a program in which I need to draw polygons of an arbitrary number of sides, each one being translated by a given formula which changes dynamically. There is some rather interesting mathematics involved but I am stuck on this probelm.
How can I calculate the coordinates of the vertices of a regular polygon (one in which all angles are equal), given only the number of sides, and ideally (but not neccessarily) having the origin at the centre?
For example: a hexagon might have the following points (all are floats):
( 1.5 , 0.5 *Math.Sqrt(3) )
( 0 , 1 *Math.Sqrt(3) )
(-1.5 , 0.5 *Math.Sqrt(3) )
(-1.5 , -0.5 *Math.Sqrt(3) )
( 0 , -1 *Math.Sqrt(3) )
( 1.5 , -0.5 *Math.Sqrt(3) )
My method looks like this:
void InitPolygonVertexCoords(RegularPolygon poly)
and the coordinates need to be added to this (or something similar, like a list):
Point[] _polygonVertexPoints;
I'm interested mainly in the algorithm here but examples in C# would be useful. I don't even know where to start. How should I implement it? Is it even possible?!
Thank you.
for (i = 0; i < n; i++) {
printf("%f %f\n",r * Math.cos(2 * Math.PI * i / n), r * Math.sin(2 * Math.PI * i / n));
}
where r is the radius of the circumsribing circle. Sorry for the wrong language No Habla C#.
Basically the angle between any two vertices is 2 pi / n and all the vertices are at distance r from the origin.
EDIT:
If you want to have the center somewher other than the origin, say at (x,y)
for (i = 0; i < n; i++) {
printf("%f %f\n",x + r * Math.cos(2 * Math.PI * i / n), y + r * Math.sin(2 * Math.PI * i / n));
}
The number of points equals the number of sides.
The angle you need is angle = 2 * pi / numPoints.
Then starting vertically above the origin with the size of the polygon being given by radius:
for (int i = 0; i < numPoints; i++)
{
x = centreX + radius * sin(i * angle);
y = centreY + radius * cos(i * angle);
}
If your centre is the origin then simply ignore the centreX and centreY terms as they'll be 0,0.
Swapping the cos and sin over will point the first point horizontally to the right of the origin.
Sorry, I dont have a full solution at hand right now, but you should try looking for 2D-Rendering of Circles. All classic implementations of circle(x,y,r) use a polygon like you described for drawing (but with 50+ sides).
Say the distance of the vertices to the origin is 1. And say (1, 0) is always a coordinate of the polygon.
Given the number of vertices (say n), the rotation angle required to position the (1, 0) to the next coordinate would be (360/n).
The computation required here is to rotate the coordinates. Here is what it is; Rotation Matrix.
Say theta = 360/n;
[cos(theta) -sin(theta)]
[sin(theta) cos(theta)]
would be your rotation matrix.
If you know linear algebra you already know what i mean. If dont just have a look at Matrix Multiplication
One possible implementation to generate a set of coordinates for regular polygon is to:
Define polygon center, radius and first vertex1. Rotate the vertex n-times2 at an angle of: 360/n.
In this implementation I use a vector to store the generated coordinates and a recursive function to generate them:
void generateRegularPolygon(vector<Point>& v, Point& center, int sidesNumber, int radius){
// converted to radians
double angRads = 2 * PI / double(sidesNumber);
// first vertex
Point initial(center.x, center.y - radius);
rotateCoordinate(v, center, initial, angRads, sidesNumber);
}
where:
void rotateCoordinate(vector<Point>& v, Point& axisOfRotation, Point& initial, double angRads, int numberOfRotations){
// base case: number of transformations < 0
if(numberOfRotations <= 0) return;
else{
// apply rotation to: initial, around pivot point: axisOfRotation
double x = cos(angRads) * (initial.x - axisOfRotation.x) - sin(angRads) * (initial.y - axisOfRotation.y) + axisOfRotation.x;
double y = sin(angRads) * (initial.x - axisOfRotation.x) + cos(angRads) * (initial.y - axisOfRotation.y) + axisOfRotation.y;
// store the result
v.push_back(Point(x, y));
rotateCoordinate(v, axisOfRotation, Point(x,y), angRads, --numberOfRotations);
}
}
Note:
Point is a simple class to wrap the coordinate into single data structure:
class Point{
public:
Point(): x(0), y(0){ }
Point(int xx, int yy): x(xx), y(yy) { }
private:
int x;
int y;
};
1 in terms of (relative to) the center, radius. In my case the first vertex is translated from the centre up horizontally by the radius lenght.
2 n-regular polygon has n vertices.
The simple method is:
Let's take N-gone(number of sides) and length of side L. The angle will be T = 360/N.
Let's say one vertices is located on origin.
* First vertex = (0,0)
* Second vertex = (LcosT,LsinT)
* Third vertex = (LcosT+Lcos2T, LsinT+Lsin2T)
* Fourth vertex = (LcosT+Lcos2T+Lcos3T, LsinT+Lsin2T+Lsin3T)
You can do in for loop
hmm if you test all the versions that are listed here you'll see that the implementation is not good. you can check the distance from the center to each generated point of the polygon with : http://www.movable-type.co.uk/scripts/latlong.html
Now i have searched a lot and i could not find any good implementation for calculating a polyogon using the center and the radius...so i went back to the math book and tried to implement it myself. In the end i came up with this...wich is 100% good:
List<double[]> coordinates = new List<double[]>();
#region create Polygon Coordinates
if (!string.IsNullOrWhiteSpace(bus.Latitude) && !string.IsNullOrWhiteSpace(bus.Longitude) && !string.IsNullOrWhiteSpace(bus.ListingRadius))
{
double lat = DegreeToRadian(Double.Parse(bus.Latitude));
double lon = DegreeToRadian(Double.Parse(bus.Longitude));
double dist = Double.Parse(bus.ListingRadius);
double angle = 36;
for (double i = 0; i <= 360; i += angle)
{
var bearing = DegreeToRadian(i);
var lat2 = Math.Asin(Math.Sin(lat) * Math.Cos(dist / earthRadius) + Math.Cos(lat) * Math.Sin(dist / earthRadius) * Math.Cos(bearing));
var lon2 = lon + Math.Atan2(Math.Sin(bearing) * Math.Sin(dist / earthRadius) * Math.Cos(lat),Math.Cos(dist / earthRadius) - Math.Sin(lat) * Math.Sin(lat2));
coordinates.Add(new double[] { RadianToDegree(lat2), RadianToDegree(lon2) });
}
poly.Coordinates = new[] { coordinates.ToArray() };
}
#endregion
If you test this you'll see that all the points are at the exact distance that you give ( radius ). Also don't forget to declare the earthRadius.
private const double earthRadius = 6371.01;
This calculates the coordinates of a decagon. You see the angle used is 36 degrees. You can split 360 degrees to any number of sides that you want and put the result in the angle variable.
Anyway .. i hope this helps you #rmx!