Skip to content

Fix the bug in case determinant of rotation matrix is -1 - #13601

Merged
alalek merged 2 commits into
opencv:3.4from
JimZhou-001:JimZhou-001
Jan 11, 2019
Merged

alalek merged 2 commits into
opencv:3.4from
JimZhou-001:JimZhou-001

Conversation

@JimZhou-001

Copy link
Copy Markdown
Contributor

resolves #4978

Fix the bug in case determinant of rotation matrix is -1

@alalek

alalek commented Jan 9, 2019

Copy link
Copy Markdown
Member

I will rebase this patch.

@alalek

alalek commented Jan 9, 2019 •

Copy link
Copy Markdown
Member

Done (updated branch in your GitHub fork). Please update your local git repository (on your machine).

Could you capture problem inputs and create simple test for this case (to avoid future regressions)?


Update: Inputs are available in original issue.

@alalek

alalek commented Jan 11, 2019

Copy link
Copy Markdown
Member

Added test

@alalek alalek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done! Thank you 👍

@alalek
alalek merged commit 78bd55c into opencv:3.4 Jan 11, 2019
@JimZhou-001

JimZhou-001 commented Jan 12, 2019 •

Copy link
Copy Markdown
Contributor Author

@alalek Thank you for your kind help.

I recognize that it depends on whether and how we check the result furtherly.

As we know, homography can be induced by pure rotation or planar scenes. And in your test, you set translation as vector 0. It seems that you cionsider pure rotation. I have a doubt whether these two kinds of homographies are consistent. Since in the case of planar scene, homography strongly relies on the plane in real world. But in image mosaicing (pure rotation), the homography is (theoretically) unique for two images.

Since we are discussing camera calibration and 3D reconstruction, it seems strange that the camera center never moves. (I'm not saying your test is wrong. It's just an example to check the result. I am consulting the differences between these two homographies.)

Since your test doesn't consider translation, here I give an example. I'm so sorry I didn't know the format of test and how to write code like that, so I didn't create the test. But we can discuss here with more freedom.

By the way, if the homography considers pure rotation, the estimated normal of the plane makes no sense. If the homography considers planar scene, the input of function findHomography should be coplanar points, right?

As I said at the beginning, we need to check the result, so the code before is not wrong. You just implement the algorithms of HomographyDecompInria and HomographyDecompZhang according to related references. The results of these methods should be checked by us manually. The reason why I say this is that I realize the simple check of det(R) > 0 (as I pulled request) is far from enough.

Suppose
cv::Matx33d H;
cv::Matx33d K;

H << -0.0003348703671253786, 1.600035513782181e-05, 0.6118483492435596,
-0.0006197791876426851, 1.176780051073813e-05, 0.7909747255691038,
-1.578360198423081e-07, 7.725062124382988e-09, 0.000290482169257178;

K << 2905.88, 0, 1416,
     0, 2905.88, 1064,
     0, 0, 1;

Using Zhang's method (you use Inria's as default) implemented in opencv (considering there may be difference between two intrinsic matrixes. But in this case, I set them the same), I get the following results:

Total number of solutions: 8

R =
[-0.9364946025241672, 0.3120714797227837, -0.1599664054464929;
0.1521885764052158, 0.7726349432416537, 0.6163390963561511;
0.3159374885396886, 0.5528531775804026, -0.771062168331385]
det(R)=
1
t=
[2.17194388281231;
4.802225976544642;
4.710798275733059]

R =
[1.21566372748587, -0.05669625349220311, -0.179458903335696;
4.910668166876392, -0.04272037130885919, 0.5732406625282626;
4.983822248521784, -0.2469789062604448, -0.8133400679460332]
det(R)=
0.00537154
t=
[0.01166667678070407;
0.02579533418870028;
0.02530422691718559]

R =
[1.21566372748587, -0.05669625349220311, -0.179458903335696;
4.910668166876392, -0.04272037130885919, 0.5732406625282626;
4.983822248521784, -0.2469789062604448, -0.8133400679460332]
det(R)=
0.00537154
t=
[-0.01166667678070407;
-0.02579533418870028;
-0.02530422691718559]

R =
[-0.9364946025241672, 0.3120714797227837, -0.1599664054464929;
0.1521885764052158, 0.7726349432416537, 0.6163390963561511;
0.3159374885396886, 0.5528531775804026, -0.771062168331385]
det(R)=
1
t=
[-2.17194388281231;
-4.802225976544642;
-4.710798275733059]

R =
[1.205355598255962, -0.05459292495324943, -0.1794144018133017;
4.910986780251332, -0.03548683604340186, 0.5721812469516737;
4.986385500086242, -0.2396315265538234, -0.8144920730750855]
det(R)=
0.00537154
t=
[0.00119940039434821;
0.02593477835716218;
0.02770854061742445]

R =
[0.9825332959057924, -0.07949774256686437, -0.1682510962077778;
0.09287345319609841, -0.5740066896954943, 0.8135667408841405;
-0.1612539739636958, -0.8149824716328578, -0.5565974548533852]
det(R)=
1
t=
[0.2232881221031006;
4.828185803355463;
5.158400839149721]

R =
[0.9825332959057924, -0.07949774256686437, -0.1682510962077778;
0.09287345319609841, -0.5740066896954943, 0.8135667408841405;
-0.1612539739636958, -0.8149824716328578, -0.5565974548533852]
det(R)=
1
t=
[-0.2232881221031006;
-4.828185803355463;
-5.158400839149721]

R =
[1.205355598255962, -0.05459292495324943, -0.1794144018133017;
4.910986780251332, -0.03548683604340186, 0.5721812469516737;
4.986385500086242, -0.2396315265538234, -0.8144920730750855]
det(R)=
0.00537154
t=
[-0.00119940039434821;
-0.02593477835716218;
-0.02770854061742445]

As you can see, det(R) may even be 0.00537154, which is larger than 0, too. Maybe it's because the homofgraphy itself is wrong, but as an example, it can reflect some facts of the code. So for more accurate results, we should write the code like this:
if (std::fabs(std::fabs(cv::determinant(motion.R)) - 1) < 0.01) { //TODO }

Actually, if we estimate homography from features (such as points and lines), the estimated rotation R and translation t should meet that (theoretically) all features are in front of both cameras. So we can check further. But simply choosing the pose with most features in the front is thoughtless. just for (R, t) and (R, -t), we may do it like this. In my test, there are still two sulutions to be checked after the steps above. Maybe we can check through the normal. Since the feature points are from the same plane, a line segment formed by any two of these points (theoretically) ought to be perpendicular to the normal. Then we may get the final one. I just analyze like this, but still don't have a try.

Of course, the funtion decomposeHomographyMat itself doesn't need to check by features. The above paragraph is just to discuss.

Last but not least, when I use 4 points to calculate homography and decompose it, the result (R and t) changes a lot even with one small pixel change of a point. So you provide robust methods such as RANSAC. I'm not so good at reading code. Could you tell me how you estimate homography robustly, randomly select 4 points every time and choose the best one, or estimate it based on so many points? I'd like to have a try in the former way, but don't know how to choose the best one (in detail).

Oh, could you tell me the right steps to pull request in detail (code) if I want to change it like what I said above?

Thank you so much.

@alalek alalek mentioned this pull request Jan 22, 2019
SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
* Fix the bug in case determinant of rotation matrix is -1

* calib3d(test): check det(R) == 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants