shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.39k stars 1.15k forks source link

Cv2.RecoverPose (triangulatedPoints overload) throws AccessViolationException #1410

Open ShinNoNoir opened 2 years ago

ShinNoNoir commented 2 years ago

Summary of your issue

The Cv2.RecoverPose overload with OutputArray triangulatedPoints throws an AccessViolationException.

Environment

OpenCvSharp4.Windows: 4.5.5.20211231

OS: Windows 10 Pro 21H1 19043.1586 Visual Studio 2019 16.10.4

Example code:

        static void Main(string[] args)
        {
            var points1 = new[,] 
            {
                { 0.38813609924847137, 0.02447841812986895 },
                { 0.08423523188782245, 0.10197624851964165 },
                { 0.29981091670244864, 0.10563331155486635 },
                { 0.2692439454334949, 0.11443490334674666 },
                { -0.041948288655290975, -0.05514874152548128 },
                { 0.03992329933832534, 0.007632737039647461 },
                { 0.050901003100829574, 0.05711866453816542 },
                { -0.03948646783477256, 0.025999204793101333 },
                { -0.06209751680095625, 0.04221654108790821 },
                { -0.016555534818875183, 0.06435009173308306 },
                { -0.16903822109902406, -0.07257834381179194 },
                { 0.41642570198356116, 0.12256932045317968 },
                { 0.39861580051504203, 0.16142837173027308 },
                { 0.04655404967444799, -0.010591253965935497 },
                { 0.03556041780480632, -0.006437507577777012 },
                { 0.41248124653016055, 0.02397771978349547 },
                { 0.3801891632514438, 0.026561067331762406 },
                { 0.42808861337304654, 0.029163367387340686 },
                { 0.4318539553218957, 0.03833957070243556 },
                { 0.44248180108275476, 0.04785089487375638 },
                { 0.4262027536470637, 0.050711978734889696 },
                { 0.0874961830532552, 0.1031790076314136 },
                { 0.2700899364672753, 0.11163060447412339 },
                { 0.3582427322108526, 0.11545310747602498 },
                { 0.041542631217902054, -0.02727039291563035 },
                { 0.4303139947145481, 0.03949740376628924 },
                { 0.06001969886286597, 0.046760602502455145 },
                { -0.03946697379884526, 0.021581967008446938 },
                { -0.1642986257578312, -0.06865176910206533 },
                { 0.03938910580334193, -0.07728559157122807 },
                { 0.06254455304488589, -0.03771698379780939 },
                { -0.07909181296840861, -0.03482664261191205 },
                { -0.08020047248432952, -0.04234032884282093 },
            };
            var points2 = new[,]
            {
                { -0.2789943914026892, 0.30720648107100057 },
                { -0.03211411217735307, 0.0552583557497171 },
                { -0.29157998452774253, 0.1485318201443865 },
                { -0.2702940577548254, 0.12072539127028371 },
                { 0.2477206015038991, 0.2415913262853919 },
                { 0.06618681525128994, 0.17127756148205156 },
                { 0.042038487143667536, 0.10712379560384284 },
                { 0.17024542526228917, 0.09981948720558462 },
                { 0.19885502624141976, 0.06308992557874996 },
                { 0.12763100053213033, 0.058938319107061155 },
                { 0.46339803911837213, 0.1987904682083972 },
                { -0.433620692570429, 0.16921885039668041 },
                { -0.46932283702262495, 0.10055432703225901 },
                { 0.07157221583536884, 0.2046623745409314 },
                { 0.08044325517579885, 0.1909797723911847 },
                { -0.30160697877887155, 0.31671400091223556 },
                { -0.2720897728043967, 0.30219843720518874 },
                { -0.3233379323990584, 0.31416448403147024 },
                { -0.3395905678403646, 0.3015414353998168 },
                { -0.36075032186748296, 0.291377190103051 },
                { -0.34930563708219925, 0.2816288831915651 },
                { -0.03776495756263569, 0.054842338028296626 },
                { -0.26713013306303746, 0.1259610178894828 },
                { -0.366032826498244, 0.15698442478244704 },
                { 0.09215684671120347, 0.2314028881588346 },
                { -0.3382650691409414, 0.2996505624675723 },
                { 0.032134959390848615, 0.12605425231232353 },
                { 0.17160473809736793, 0.10638491555956729 },
                { 0.45231703093455844, 0.19440993018522748 },
                { 0.14987665442569326, 0.319307869795434 },
                { 0.06438724162613875, 0.256554254526816 },
                { 0.268219830870227, 0.17746488412848505 },
                { 0.27588351860241883, 0.19031175105448103 },
            };

            using var points1m = Mat<double>.FromArray(points1);
            using var points2m = Mat<double>.FromArray(points2);

            using var K = Mat.Eye(3, 3, MatType.CV_64F);

            recoverPoseTest1(points1m, points2m, K); // works
            recoverPoseTest2(points1m, points2m, K); // fails: System.AccessViolationException
        }

        private static void recoverPoseTest1(Mat<double> points1m, Mat<double> points2m, Mat K)
        {
            using var inliers = new Mat();
            using var E = Cv2.FindEssentialMat(points1m, points2m, K, EssentialMatMethod.LMedS, 0.99, 1.0, inliers);

            using var R = new Mat();
            using var t = new Mat();
            int numInliers = Cv2.RecoverPose(E, points1m, points2m, K, R, t, inliers);
        }

        private static void recoverPoseTest2(Mat<double> points1m, Mat<double> points2m, Mat K)
        {
            using var inliers = new Mat();
            using var E = Cv2.FindEssentialMat(points1m, points2m, K, EssentialMatMethod.LMedS, 0.99, 1.0, inliers);

            using var R = new Mat();
            using var t = new Mat();
            using var triangulatedPoints = new Mat();
            int numInliers = Cv2.RecoverPose(E, points1m, points2m, K, R, t, 50.0, inliers, triangulatedPoints);
        }

Expected results:

Cv2.RecoverPose should not crash when asked to triangulate points.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ShinNoNoir commented 2 years ago

Still unresolved in version 4.6.0.20220608.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ShinNoNoir commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Still unresolved in version 4.7.0.20230115.

stale[bot] commented 10 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ShinNoNoir commented 10 months ago

Bug is still present in 4.8.0.20230708.