Numerical Stability of solvePoly

asked 2018-06-18 05:06:09 -0500

updated 2018-06-18 05:18:44 -0500

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 ...
(more)
edit retag flag offensive close merge delete

Comments

Can it be a problem with compiler optimization ? lapack ? My vs 2017 it is in cmake:

 CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (3 files):          + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (2 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (9 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
LBerger gravatar imageLBerger ( 2018-06-20 02:10:43 -0500 )edit

@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.

Balaji R gravatar imageBalaji R ( 2018-06-20 03:51:17 -0500 )edit

my results in csv file : roots:

0.5259628430429782, -1.862168088227023e-27, 1.279105946931318, 0.4180923917916413, 1.722392279412144, 1.622214396023977, 0.4425747197215948, 0, -0.3149920515340438, 6.204438043053039e-32, 0.4157558308136742, 0, 0.4396132684237557, 0, 1.722392279412145, -1.622214396023977, 1.279105946931324, -0.4180923917916384, 2.066241418000117, -1.291577856033717e-29
LBerger gravatar imageLBerger ( 2018-06-20 04:53:55 -0500 )edit

Looks like you are getting max error in terms of 1.25E-06.

Balaji R gravatar imageBalaji R ( 2018-06-20 05:35:30 -0500 )edit

Should we report this bug?

Balaji R gravatar imageBalaji R ( 2018-06-20 05:36:50 -0500 )edit