Numerical Stability of solvePoly
I'm working with OpenCV 5 point Essential Matrix Computation Algorithm and I'm facing Numerical Instability issue when working with these 2 platforms (Windows Visual Studio and Linux Gcc compiler). Both compiled as a 64 Bit application.
The problem was caused by this function solvePoly and here is a sample code to reproduce the Error with same inputs in two different platforms.
The following code will produce two different outputs in Windows msvc and Linux GCC and here is a Sample Output.
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/opencv_modules.hpp"
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
using namespace cv;
int main(int, char**)
{
double b[3 * 13]= { 43.62899215341317, -142.8659619253204, 182.4526535510934, -62.12438333428298, -9.66204383178342, 76.3870398608554, -116.055365497144, 40.87998289431373, -42.59799056519181, 254.9402120596346, -504.9633997399467, 384.3746161586831, -95.89887859435427,
77.7070672570582, -245.6776216042046, 302.6478282208725, -89.53722757851534, -18.16867987414801, 137.6158773648859, -198.6262187902296, 60.54628991983429, -77.94581368911055, 454.935337437791, -873.484503220278, 629.3372557512528, -141.5849526705521,
59.937068130234, -203.3296090558193, 269.1321546914465, -102.6876384579035, -12.58131418572418, 103.8840936956255, -166.5017038614965, 66.32614093605801, -57.22582537611544, 350.6649163333698, -716.5726353181753, 574.4994715331657, -155.9118381651257 };
Mat B(3, 13, CV_64F, b);
double c[11];
Mat coeffs(1, 11, CV_64F, c);
c[10] = (b[0] * b[17] * b[34] + b[26] * b[4] * b[21] - b[26] * b[17] * b[8] - b[13] * b[4] * b[34] - b[0] * b[21] * b[30] + b[13] * b[30] * b[8]);
c[9] = (b[26] * b[4] * b[22] + b[14] * b[30] * b[8] + b[13] * b[31] * b[8] + b[1] * b[17] * b[34] - b[13] * b[5] * b[34] + b[26] * b[5] * b[21] - b[0] * b[21] * b[31] - b[26] * b[17] * b[9] - b[1] * b[21] * b[30] + b[27] * b[4] * b[21] + b[0] * b[17] * b[35] - b[0] * b[22] * b[30] + b[13] * b[30] * b[9] + b[0] * b[18] * b[34] - b[27] * b[17] * b[8] - b[14] * b[4] * b[34] - b[13] * b[4] * b[35] - b[26] * b[18] * b[8]);
c[8] = (b[14] * b[30] * b[9] + b[14] * b[31] * b[8] + b[13] * b[31] * b[9] - b[13] * b[4] * b[36] - b[13] * b[5] * b[35] + b[15] * b[30] * b[8] - b[13] * b[6] * b[34] + b[13] * b[30] * b[10] + b[13] * b[32] * b[8] - b[14] * b[4] * b[35] - b[14] * b[5] * b[34] + b[26] * b[4] * b[23] + b[26] * b[5] * b[22] + b[26] * b[6] * b[21] - b[26] * b[17] * b[10] - b[15] * b[4] * b[34] - b[26] * b[18] * b[9] - b[26] * b[19] * b[8] + b[27] * b[4] * b[22] + b[27] * b[5] * b[21] - b[27] * b[17] * b[9] - b[27] * b[18] * b[8] - b[1] * b[21] * b[31] - b[0] * b[23] * b ...
Can it be a problem with compiler optimization ? lapack ? My vs 2017 it is in cmake:
@LBerger Thank you for your comments. I've disabled all the optimizations in both the places. Still the results are same. Also after looking at the code of solveCubic & solvePoly functions there are no SIMD based operations are performed in these functions. But it is definitely something related to the compiler optimization. Not sure from where it is originating.
my results in csv file : roots:
Looks like you are getting max error in terms of 1.25E-06.
Should we report this bug?