I'm having a problem with OpenCV's various parameterization of coordinates used for camera calibration purposes. The problem is that three different sources of information on image distortion formulae apparently give three non-equivalent description of the parameters and equations involved:
(1) In their book "Learning OpenCV…" Bradski and Kaehler write regarding lens distortion (page 376):
xcorrected = x * ( 1 + k1 * r^2 + k2 * r^4 + k3 * r^6 ) + [ 2 * p1 * x * y + p2 * ( r^2 + 2 * x^2 ) ],
ycorrected = y * ( 1 + k1 * r^2 + k2 * r^4 + k3 * r^6 ) + [ p1 * ( r^2 + 2 * y^2 ) + 2 * p2 * x * y ],
where r = sqrt( x^2 + y^2 ).
Assumably, (x, y) are the coordinates of pixels in the uncorrected captured image corresponding to world-point objects with coordinates (X, Y, Z), camera-frame referenced, for which
xcorrected = fx * ( X / Z ) + cx and ycorrected = fy * ( Y / Z ) + cy,
where fx, fy, cx, and cy, are the camera's intrinsic parameters. So, having (x, y) from a captured image, we can obtain the desired coordinates ( xcorrected, ycorrected ) to produced an undistorted image of the captured world scene by applying the above first two correction expressions.
However...
(2) The complication arises as we look at OpenCV 2.0 C Reference entry under the Camera Calibration and 3D Reconstruction section. For ease of comparison we start with all world-point (X, Y, Z) coordinates being expressed with respect to the camera's reference frame, just as in #1. Consequently, the transformation matrix [ R | t ] is of no concern.
In the C reference, it is expressed that:
x' = X / Z,
y' = Y / Z,
x'' = x' * ( 1 + k1 * r'^2 + k2 * r'^4 + k3 * r'^6 ) + [ 2 * p1 * x' * y' + p2 * ( r'^2 + 2 * x'^2 ) ],
y'' = y' * ( 1 + k1 * r'^2 + k2 * r'^4 + k3 * r'^6 ) + [ p1 * ( r'^2 + 2 * y'^2 ) + 2 * p2 * x' * y' ],
where r' = sqrt( x'^2 + y'^2 ), and finally that
u = fx * x'' + cx,
v = fy * y'' + cy.
As one can see these expressions are not equivalent to those presented in #1, with the result that the two sets of corrected coordinates ( xcorrected, ycorrected ) and ( u, v ) are not the same. Why the contradiction? It seems to me the first set makes more sense as I can attach physical meaning to each and every x and y in there, while I find no physical meaning in x' = X / Z and y' = Y / Z when the camera focal length is not exactly 1. Furthermore, one cannot compute x' and y' for we don't know (X, Y, Z).
(3) Unfortunately, things get even murkier when we refer to the writings in Intel's Open Source Computer Vision Library Reference Manual's section Lens Distortion (page 6-4), which states in part:
"Let ( u, v ) be true pixel image coordinates, that is, coordinates with ideal projection, and ( u ̃, v ̃ ) be corresponding real observed (distorted) image coordinates. Similarly, ( x, y ) are ideal (distortion-free) and ( x ̃, y ̃ ) are real (distorted) image physical coordinates. Taking into account two expansion terms gives the following:
x ̃ = x * ( 1 + k1 * r^2 + k2 * r^4 ) + [ 2 p1 * x * y + p2 * ( r^2 + 2 * x^2 ) ]
y ̃ = y * ( 1 + k1 * r^2 + k2 * r^4 ] + [ 2 p2 * x * y + p2 * ( r^2 + 2 * y^2 ) ],
where r = sqrt( x^2 + y^2 ). ...
"Because u ̃ = cx + fx * u and v ̃ = cy + fy * v , … the resultant system can be rewritten as follows:
u ̃ = u + ( u – cx ) * [ k1 * r^2 + k2 * r^4 + 2 * p1 * y + p2 * ( r^2 / x + 2 * x ) ]
v ̃ = v + ( v – cy ) * [ k1 * r^2 + k2 * r^4 + 2 * p2 * x + p1 * ( r^2 / y + 2 * y ) ]
The latter relations are used to undistort images from the camera."
Well, it would appear that the expressions involving x ̃ and y ̃ coincided with the two expressions given at the top of this writing involving xcorrected and ycorrected. However, x ̃ and y ̃ do not refer to corrected coordinates, according to the given description. I don't understand the distinction between the meaning of the coordinates ( x ̃, y ̃ ) and ( u ̃, v ̃ ), or for that matter, between the pairs ( x, y ) and ( u, v ). From their descriptions it appears their only distinction is that ( x ̃, y ̃ ) and ( x, y ) refer to 'physical' coordinates while ( u ̃, v ̃ ) and ( u, v ) do not. What is this distinction all about? Aren't they all physical coordinates? I'm lost!
Thanks for any input!
There is no one and only formula for camera calibration, they are all valid. Notice the first one contains constants K1, K2 & K3 for r^2, r^4 & r^6, and the other two only have constants for r^2 and r^4? That is because they are all approximate models. The first one is likely to be more accurate since it has more parameters.
Anytime you see:
r = sqrt( x^2 + y^2 )
it is probably safe to assume x = (the x coordinate pixel) - (the camera center in pixels) since r usually means radius from the center.
What are you trying to do by the way? Estimate the camera parameters, correct for lens distortion, or both?
Related
I am trying to find transformation matrix, which will allow me to transfer given plane to XY plane. Based on this answer, I have prepared small Python function which does these calculations. However I don't understand why final plane is not in XY.
Could you help me and tell me what I am doing wrong?
my input plane (3d view):
390.17 * x + -571.67 * y + -4008.29 * z + 2833797.03 = 0
input = [390.17, -571.67, -4008.29, 2833797.03]
z = 0.0973*x - 0.1427*y + 706.9838
It seems to me that to transfer it to XY plane, rotation about X and Y axes should be done (first). The last element is translation along Z axis.
Based on this answer I have prepared my function transform_plane(). (code below).
As a result function returns new plane: (3d view)
2.19436115e+00 1.61044899e+03 4.06772244e+03 -4.20201320e+04 = 0
out = [2.19436115e+00, 1.61044899e+03, 4.06772244e+03, -4.20201320e+04]
Z = -0.0005*x-0.3959*y+10.3301
It looks like, the plane was only rotated about Y, and to align it with XY, it should also be rotated about X axis.
Does it mean that I forgot about something? or matrix presented in linked answer is not sufficient to get transformation for two different axes?
EDIT:
Thanks to answer below I was able to find missing square root, however still I don't understand why my translation is not sufficient.
Instead of Z = 0, my result is Z = 10. It seems to me that maybe -d/c should also be divided by square root.
My implementation:
import numpy as np
def calc_cos_phi(a, b, c):
return c / sqrt(a*a + b*b + c*c)
def calc_sin_phi(a, b, c):
return sqrt((a*a + b*b) / (a*a + b*b + c*c))
def calc_u1(a, b, c):
return b / (a*a + b*b)
def calc_u2(a, b, c):
return -a / sqrt(a*a + b*b)
def get_transform_matrix(plane):
a, b, c, d = plane
cos_phi = calc_cos_phi(a, b, c)
sin_phi = calc_sin_phi(a, b, c)
u1 = calc_u1(a, b, c)
u2 = calc_u2(a, b, c)
out = np.array([
[cos_phi + u1 * u1 * (1 - cos_phi) , u1 * u2 * (1 - cos_phi) , u2 * sin_phi , 0 ],
[u1 * u2 * (1 - cos_phi) , cos_phi + u2 * u2 * (1 - cos_phi) , -u1 * sin_phi , 0 ],
[-u2 * sin_phi , u1 * sin_phi , cos_phi , -d / c ],
[0 , 0 , 0 , 1 ]
])
return out
def transform_plane(plane):
t = get_transform_matrix(plane)
t_inv = np.linalg.inv(t)
new_plane = np.dot(plane, t_inv)
print("new plane:")
print(new_plane)
return new_plane
Test:
plane = [390.17, -571.67, -4008.29, 2833797.03]
plane_xy = transform_plane(plane)
You have missed sqrt here:
def calc_u1(a, b, c):
return b / (a*a + b*b)
should be
def calc_u1(a, b, c):
return b / sqrt(a*a + b*b)
Replacement gives [-1.13686838e-13 0.00000000e+00 4.06760715e+03 -4.19362793e+04] - x coefficient is almost zero, plane is parallel to OXY (like z=-1.05)
My apologies, for being unclear earlier. I now understand the function a bit more, but could use some assistance on a few aspects.
I would like to get back a relationship of conversion ( X ) versus volume ( V ), or the other way around would be fine as well. It would seem to me that the traditional "times" term is what I want to replace with an X sequence from 0 - 1, X is conversion remember so bounded by 0 and 1.0
Below, rw is the reaction rate, and is a function of the partial pressures at any given moment, which are described as P.w, P.x, P.y, and P.z which themselves are functions of the initial conditions (P.w0, v.0) and the conversion, again X.
Thank you in advance
rm(list = ls())
weight <- function( Vols, State, Pars ) {
with(as.list(c(State, Pars)), {
y = 1
delta = 2
ya.0 = 0.4
eps = ya.0 * delta
temp = 800
R = 8.314
k.2 = exp( (35000 / ( R*temp )) - 7.912 )
K.3 = exp( 4.084 / temp - 4.33 )
P.w <- P.w0 * ( 1 - X ) * y / ( 1 + eps * X )
P.x <- P.w0 * ( 1 - 2*X ) * y / ( 1 + eps * X )
P.y <- P.w0 * ( 1 + X ) * y / ( 1 + eps * X )
P.z <- P.w0 * ( 1 + 4*X ) * y / ( 1 + eps * X )
r.w <- k.2 * ( K.3 * P.w * P.x ^ 2 - P.y * P.z^4 )
F.w0 <- P.w0 * v.0 / ( R * temp )
dX.dq <- r.w / F.w0
res <- dX.dq
return(list(res))
})
}
pars <- c( y = 1,
P.w0 = 23,
v.0 = 120 )
yini <- c( X = 0 )
vols <- seq( 0 , 100 , by = 1 )
out <- ode( yini , vols , weight , pars )
Just running
vol.func(0,0,params)
i.e., evaluating the gradient at the initial conditions, gives NaN. The proper way to diagnose this is to divide your complex gradient expressions up into separate terms and see which one is causing trouble. I'm not going to go through this in detail, but as #Sixiang.Hu points out in comments above, you're dividing by V in your gradient function, which will cause infinite values if the numerator is finite or NaN values if the numerator is zero ...
More generally, it's not clear whether you understand that the first argument to the gradient function (your vol.func) is supposed to be the current time, not a value of the state variable. Perhaps V is supposed to be your state variable, and X should be a parameter ...?
I'm trying to implement a simple water simulation, with theory from GPU Gems 1 chapter 1.
if you imagine a 3D plane (flat in the xz plane, with y denoting height at any point), the height field function is given as:
where:
Wavelength (w): the crest-to-crest distance between waves in world space.
Amplitude (A): the height from the water plane to the wave crest.
Speed (S): the distance the crest moves forward per second.
Direction (D): the horizontal vector perpendicular to the wave front along which the
crest travels.
This is straightforward to implement.
Please note the article in GPUGems uses the z direction for height, but this isn't standard for graphics (normally, x is width, y is height, z is depth). So I'll refer to the xz direction meaning the flat/horizontal plane directions.
So, having computed the height (y) value at any given point, I need to compute the bitangent and tangent vectors to that point, in order that I can compute a normal vector, which I need for lighting equations.
The bitangent and tangent vectors are partial derivatives in the x and z directions (y is the heightfield value).
So my question is, how can I take a partial derivative in the x and then the z directions for the height field function?
The article says that the partial derivative for the x direction is given by
I understand the concept of taking a partial derivative from this video:, but I don't know how to take the partial derivative of my heightfield function.
Can someone explain it (like I'm 5) - My grasp of maths isn't great!
You want to derive the following equation:
W(x) = A * sin(w * (D.x * x + D.y * z) + t * phi)
= A * sin(w * D.x * x + w * D.y * z + t * phi)
which is the above formula with the expanded dot product. Because we want to find the derivative with respect to x, all other variables (except x) are considered constant. So we can substitute the constants:
c1 = A
c2 = w * D.x
c3 = w * D.y * z + t * phi
W(x) = c1 * sin(c2 * x + c3)
The derivative is:
W'(x) = c1 * c2 * cos(c2 * x + c3)
Reverting the substitution we get:
W'(x) = A * w * D.x * cos(w * D.x * x + w * D.y * z + t * phi)
which describes the y-component of the tangent at a given position.
Similarly, the bitangent (derivative with respect to z) can be described by
W'(z) = A * w * D.y * cos(w * D.y * z + w * D.x * x + t * phi)
Therefore:
tangent = (1, W'(x), 0)
= (1, A * w * D.x * cos(w * D.x * x + w * D.y * z + t * phi), 0)
bitangent = (0, W'(z), 1)
= (0, A * w * D.y * cos(w * D.y * z + w * D.x * x + t * phi), 1)
I read the Calculate Bounding box coordinates from a rotated rectangle to know how to calculate bounding box coordinates from a rotated rectangle. But in a special case as follow image:
How to get the rotated rectangle size if had get the bounding box size, coordinates and rotate degree?
I try write code in javascript
//assume w=123,h=98,deg=35 and get calculate box size
var deg = 35;
var bw = 156.9661922099485;
var bh = 150.82680201149986;
//calculate w and h
var xMax = bw / 2;
var yMax = bh / 2;
var radian = (deg / 180) * Math.PI;
var cosine = Math.cos(radian);
var sine = Math.sin(radian);
var cx = (xMax * cosine) + (yMax * sine) / (cosine * cosine + sine * sine);
var cy = -(-(xMax * sine) - (yMax * cosine) / (cosine * cosine + sine * sine));
var w = (cx * 2 - bw)*2;
var h = (cy * 2 - bh)*2;
But...the answer is not match w and h
Solution
Given bounding box dimensions bx by by and t being the anticlockwise rotation of rectangle sized x by y:
x = (1/(cos(t)^2-sin(t)^2)) * ( bx * cos(t) - by * sin(t))
y = (1/(cos(t)^2-sin(t)^2)) * (- bx * sin(t) + by * cos(t))
Derivation
Why is this?
First, consider that the length bx is cut in two pieces, a and b, by the corner of the rectangle. Use trigonometry to express bx in terms of x, y, and theta:
bx = b + a
bx = x * cos(t) + y * sin(t) [1]
and similarly for by:
by = c + d
by = x * sin(t) + y * cos(t) [2]
1 and 2 can be expressed in matrix form as:
[ bx ] = [ cos(t) sin(t) ] * [ x ] [3]
[ by ] [ sin(t) cos(t) ] [ y ]
Note that the matrix is nearly a rotation matrix (but not quite - it's off by a minus sign.)
Left-divide the matrix on both sides, giving:
[ x ] = inverse ( [ cos(t) sin(t) ] * [ bx ] [4]
[ y ] [ sin(t) cos(t) ] ) [ by ]
The matrix inverse is easy to evaluate for a 2x2 matrix and expands to:
[ x ] = (1/(cos(t)^2-sin(t)^2)) * [ cos(t) -sin(t) ] * [ bx ] [5]
[ y ] [-sin(t) cos(t) ] [ by ]
[5] gives the two formulas:
x = (1/(cos(t)^2-sin(t)^2)) * ( bx * cos(t) - by * sin(t)) [6]
y = (1/(cos(t)^2-sin(t)^2)) * (- bx * sin(t) + by * cos(t))
Easy as pie!
You'll probably need something like affine transformation to discover point coordinates. And then using standard geometry formulas calculate the size.
Ok,
I have a projectile that has its position defined such that:
a.x = initialX + initialDX * time;
a.y = initialY + initialDY * time + 0.5 * gravtiy * time^2;
I want to be able to predict which obstacles in my environment this projectile will collide with. I plan on checking the distance from A the closest point on the curve to the point P.
I figure that at the point A the tangent to the curve will be perpendicular to the vector AP, and that the tangent to the curve at A will simply be the velocity V of the projectile at that point.
AP dot V = 0
ap.x = initialX + initialDX * time - p.x;
ap.y = initialY + initialDY * time + gravity * time^2 - p.y;
v.x = initialDX;
v.y = initialDY + gravity * time;
=>
AP dot V =
( 0.5 * gravity^2 ) * t^3 +
( 1.5 * gravity * initialDY ) * t^2 +
( initialDX^2 + initialDY^2 + gravity * ( initialY - p.y ) ) * t +
( initialDX * ( initialX - p.x ) + initialDY * ( initialY - p.y ) )
From here I can see that this is a cubic function. I have spent some time researching online and found that there is a general equation that seems to work for certain values for finding the roots.
This is the process I have attempted to implement.
http://www.sosmath.com/algebra/factor/fac11/fac11.html
a = 0.5 * gravity^2;
b = 1.5 * gravity * initialDY;
c = initialDX^2 + initialDY^2 + gravity * ( initialY - p.y );
d = initialDX * ( initialX - p.x ) + initialDY * ( initialY - p.y );
A = ( c - ( b * b ) / ( 3 * a ) ) / a;
B = -( d + ( 2 * b * b * b ) / ( 27 * a * a ) - ( b * c ) / ( 3 * a ) ) / a;
workingC = -Math.pow( A, 3 ) / 27;
u = ( -B + Math.sqrt( B * B - 4 * workingC ) ) / 2; // Quadratic formula
s = Math.pow( u + B, 1 / 3 );
t = Math.pow( u, 1 / 3 );
y = s - t;
x = y - b / ( 3 * a );
When I plug x back into my original equations for the curve as the time, this should give me A. This seems to work well for certain values, however when p.y is above a certain value, I don't have a positive to take a square root of in the quadratic equation.
I don't have a full enough understanding of the math to understand why this is happening, or what I can do to resolve the issue.
Any help on this would be much appreciated.
UPDATE:
I have adjusted my algorithm to deal with complex roots, however I am still having trouble.
This is what I do now if the discriminant is negative:
a = 0.5 * gravity^2;
b = 1.5 * gravity * initialDY;
c = initialDX^2 + initialDY^2 + gravity * ( initialY - p.y );
d = initialDX * ( initialX - p.x ) + initialDY * ( initialY - p.y );
A = ( c - ( b * b ) / ( 3 * a ) ) / a;
B = -( d + ( 2 * b * b * b ) / ( 27 * a * a ) - ( b * c ) / ( 3 * a ) ) / a;
workingC = -Math.pow( A, 3 ) / 27;
discriminant = B * B - 4 * workingC;
then if discriminant < 0;
uc = new ComplexNumber( -B / 2, Math.sqrt( -discriminant ) / 2 );
tc = uc.cubeRoot( );
uc.a += B;
sc = uc.cubeRoot( );
yc = sc - tc;
yc.a -= b / ( 3 * a );
x = -d / ( yc.a * yc.a + yc.b * yc.b );
For some reason, this is still not giving me the results I expect. Is there anything that stands out as being wrong here?
Real polynomials can have complex number roots and if the roots are not real, they occur in conjugate pairs.
This implies cubics always have at least one real root.
Now if you get a complex root using your method, you can try to get the conjugate, mutiply and divide the constant of the cubic, take reciprocal to get the real root.
So if you had to take the square root of a -ve number, then it is same as multiplying the square root of its modulus by the imaginary number 'i'.
So if you represent your root as (m,n) denoting the complex number m + in. Then the other root is m - in = (m, -n) and m and n are real numbers.
The cubic can then be written as P(x) = (x^2 - 2m + (m^2 + n^2))(x-r).
So if P(x) = x^3 - a_1 *x^2 + a_2*x - a_3, then we have that r = a_3/(m^2 + n^2) (a_3 is the product of the roots, which is r(m^2+n^2))
A simpler way to get r would be to use the formula r = a_1 - 2m (a_1 is the sum of the roots, which is r+2m).
Check out: http://en.wikipedia.org/wiki/Complex_number