OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Sun, 29 Mar 2020 06:33:36 -0500Inverse Matrix from a Decomposehomographyhttp://answers.opencv.org/question/228224/inverse-matrix-from-a-decomposehomography/I need to obtain the inverse of a rotation matrix I get from decomposehomography() but I'm having some trouble as it looks like the type of the matrix I obtain from that function does not seem to work with .inv(). Here's an example where Prev_rot_matrix is another matrix
int solutions = decomposeHomographyMat(H, cameraMatrix, Rs_decomp, ts_decomp, normals_decomp);
for (int i = 0; i < solutions; i++)
{
if(normals_decomp[i].at<double>(2)>0)
{
aux=FrameVar(Rs_decomp[i], Prev_rot_matrix); /*Prev_rot_matrix has the same structure as Rs_decomp[i]*/
if(aux<Var)
{
Var=aux;
SOL=i;
}
}
}
double FrameVar(Mat Rot_Curr, Mat Rot_Prev)
{
//Previous rotation: Rot_Prev ;
current rotation: Rot_Curr
double NewAngle, OldAngle, aux;
Mat Rot_Curr_vect, Rot_Prev_vect;
Mat VarAngle(cv::Size(1,1), CV_64FC1);
Rodrigues(Rot_Curr, Rot_Curr_vect);
Rot_Prev_vect=Rot_Prev.inv()*Rodrigues(Rot_Prev, Rot_Prev_vect);
...
So when I try to compile it I get:
> ‘cv::MatExpr’ is not derived from
> ‘const cv::Affine3<T>’
> Rot_Prev_vect=Rot_Prev.inv()*Rodrigues(Rot_Prev,
> Rot_Prev_vect);
and a bunch of other errors.
In case that that meant Rot_Prev class does not have .inv(), how can I obtain its inverse matrix? I want to get the previous rotation vector in the new frame coordinatesAquasSun, 29 Mar 2020 06:33:36 -0500http://answers.opencv.org/question/228224/Rotation matrix to euler angles with opencv c++ correct value wrong placehttp://answers.opencv.org/question/139472/rotation-matrix-to-euler-angles-with-opencv-c-correct-value-wrong-place/ I am working on a project wich involves Aruco markers and opencv. I am quite far in the project progress. I can read the rotation vectors and convert them to a rodrigues matrix using rodrigues() from opencv.
This is a example of a rodrigues matrix I get:
[0,1,0;
1,0,0;
0,0,-1]
I use the following code.
Mat m33(3, 3, CV_64F);
Mat measured_eulers(3, 1, CV_64F);
Rodrigues(rotationVectors, m33);
measured_eulers = rot2euler(m33);
Degree_euler = measured_eulers * 180 / CV_PI;
I use the predefined rot2euler to convert from rodrigues matrix to euler angles. And I convert the received radians to degrees.
rot2euler looks like the following.
Mat rot2euler(const Mat & rotationMatrix)
{
Mat euler(3, 1, CV_64F);
double m00 = rotationMatrix.at<double>(0, 0);
double m02 = rotationMatrix.at<double>(0, 2);
double m10 = rotationMatrix.at<double>(1, 0);
double m11 = rotationMatrix.at<double>(1, 1);
double m12 = rotationMatrix.at<double>(1, 2);
double m20 = rotationMatrix.at<double>(2, 0);
double m22 = rotationMatrix.at<double>(2, 2);
double x, y, z;
// Assuming the angles are in radians.
if (m10 > 0.998) { // singularity at north pole
x = 0;
y = CV_PI / 2;
z = atan2(m02, m22);
}
else if (m10 < -0.998) { // singularity at south pole
x = 0;
y = -CV_PI / 2;
z = atan2(m02, m22);
}
else
{
x = atan2(-m12, m11);
y = asin(m10);
z = atan2(-m20, m00);
}
euler.at<double>(0) = x;
euler.at<double>(1) = y;
euler.at<double>(2) = z;
return euler;
}
If I use the rodrigues matrix I give as an example I get the following euler angles.
[0; 90; -180]
But I am suppose to get the following.
[-180; 0; 90]
When is use this tool
[danceswithcode.net/engineeringnotes/rotations_in_3d/demo3D/rotations_in_3d_tool.html]
You can see that [0; 90; -180] doesn't match the rodrigues matrix but [-180; 0; 90] does. (I am aware of the fact that the tool works with ZYX coordinates)
So the problem is I get the correct values but in a wrong order.
Another problem is that this isn't always the case. For example rodrigues matrix:
[1,0,0;
0,-1,0;
0,0,-1]
Provides me the correct euler angles.
If someone knows a solution to the problem or can provide me with a explanation how the rot2euler function works exactly. It will be higly appreciated.
Kind Regards
Brent Convens
Brent ConvensWed, 12 Apr 2017 05:03:32 -0500http://answers.opencv.org/question/139472/Rodrigues giving empty Matrixhttp://answers.opencv.org/question/88581/rodrigues-giving-empty-matrix/ Hi,
I'm implementing markerbased tracking on Android and I'm stuck at the rotation matrix.
Up until the solvePnP everything works fine and I get results (rvec, tvec) that seem legit.
But after using the rvec in Rodrigues for having a rotation matrix, it just results in a 3x3 Matrix, filled only with zeroes.
I really don't get this, because there are definitely values in rvec, but Rodrigues just doesn't use them?
I hope somebody knows whats wrong.
Heres the code snippet:
Mat rvec = new Mat(3,1,CvType.CV_64F);
Mat tvec = new Mat(3,1,CvType.CV_64F);
MatOfPoint3f markerPointsMat = new MatOfPoint3f(new Point3(0,0,0),new Point3(1,0,0),new Point3(1,1,0),new Point3(0,1,0));
MatOfPoint2f pointsMat = new MatOfPoint2f(points.get(0),points.get(1),points.get(2),points.get(3));
//cam matrix, by hand
Mat cam=new Mat(3,3,CvType.CV_64F);
cam.zeros(cam.size(),CvType.CV_64F);
cam.put(0, 0, 1400.533595140377 );
cam.put(0, 2, 175.5 );
cam.put(1, 1, 1400.533595140377 );
cam.put(1, 2, 143.5);
cam.put(2,2,1.0);
double[] a = {0.1155606860709641,-0.3046292381782507,0,0,0};
MatOfDouble dist = new MatOfDouble(a);
Calib3d.solvePnP(markerPointsMat, pointsMat, cam, dist, rvec, tvec);
//rot vector to matrix
Mat rmat = Mat.zeros(3,3,CvType.CV_64F);
Rodrigues(rvec, rmat);eqnxThu, 25 Feb 2016 09:28:12 -0600http://answers.opencv.org/question/88581/