EnoxSoftware / OpenCVForUnity

OpenCV for Unity (Untiy Asset Plugin)
https://assetstore.unity.com/packages/tools/integration/opencv-for-unity-21088
557 stars 175 forks source link

Broken solvePnP in C# but not in Python #186

Closed Rombond closed 4 months ago

Rombond commented 5 months ago

After following the workaround for the issue #169, it seems that it still doesn't work in C# but it works in Python. Here is what I did:

Python:

import cv2 as cv
import numpy as np

mtx = np.array([[600.0, 0.0, 1280/2], [0.0, 600.0, 720/2], [0.0, 0.0, 1.0]])
dist = np.array([[0.0,  0.0, 0.0, 0.0, 0.0]])
qrcode_size = 0.176
qrcode_sizes = np.array([[0,0,0], [0, qrcode_size, 0], [qrcode_size, qrcode_size, 0], [qrcode_size, 0, 0]])
points = np.array([[842, 414], [544.48657, 416.01022], [546.0603, 121.71874], [819.1106, 141.89487]])
rvec = np.array([[0.0,  0.0, 0.0]])
tvec = np.array([[0.0,  0.0, 0.0]])
succeeded= cv.solvePnP(qrcode_sizes, points, mtx, dist, rvec, tvec)
print(succeeded) # returns True

C#:

float qrSize = 0.176f;
MatOfPoint2f pointsTransformed = new MatOfPoint2f(new Point(842, 414), new Point(544.48657, 416.01022), new Point(546.0603, 121.71874), new Point(819.1106, 141.89487));
MatOfPoint3f objectPoints = new MatOfPoint3f(new Point3(0, 0, 0), new Point3(0, qrSize, 0), new Point3(qrSize, qrSize, 0), new Point3(qrSize, 0, 0));
MatOfPoint3f camera_matrix = new MatOfPoint3f(new Point3(600, 0, 1280 / 2), new Point3(0, 600, 720 / 2), new Point3(0, 0, 1));
MatOfDouble camera_distortions = new MatOfDouble(0, 0, 0, 0);

Mat rvec = new Mat(3, 1, CvType.CV_64FC1);
Mat tvec = new Mat(3, 1, CvType.CV_64FC1);
Utils.setDebugMode(true);
succeeded = Calib3d.solvePnP(objectPoints, pointsTransformed, camera_matrix, camera_distortions, rvec, tvec);
Utils.setDebugMode(false);
print(succeeded); // returns False

Did I miss something ? There is an OpenCV error but it doesn't help at all, here it is:

calib3d::solvePnP_12() : OpenCV(4.9.0-dev) C:\Users\satoo\Desktop\opencv\modules\core\include\opencv2/core/mat.inl.hpp:1442: error: (-215:Assertion failed) DataType<_Tp>::channels == m.channels() || m.empty() in function 'cv::Mat_<double>::operator ='

UnityEngine.Debug:LogError (object)
OpenCVForUnity.UnityUtils.Utils:debugLogFunc (string)
OpenCVForUnity.Calib3dModule.Calib3d:solvePnP (OpenCVForUnity.CoreModule.MatOfPoint3f,OpenCVForUnity.CoreModule.MatOfPoint2f,OpenCVForUnity.CoreModule.Mat,OpenCVForUnity.CoreModule.MatOfDouble,OpenCVForUnity.CoreModule.Mat,OpenCVForUnity.CoreModule.Mat)
EnoxSoftware commented 5 months ago

We have modified some of your code and now we get the same results in python and c#. It seems that the error was caused by the CvType of "camera_matrix" and the matrix shape.

The corrected code is below.

Python:

import cv2 as cv
import numpy as np

mtx = np.array ([[ 600.0 , 0.0 , 1280 / 2 ], [ 0.0 , 600.0 , 720 / 2 ], [ 0.0 , 0.0 , 1.0 ]])
dist = np.array ([[ 0.0 ,  0.0 , 0.0 , 0.0 , 0.0 ]])
qrcode_size = 0.176
qrcode_sizes = np . array ([[ 0 , 0 , 0 ], [ 0 , qrcode_size , 0 ], [ qrcode_size , qrcode_size , 0 ], [ qrcode_size, 0 , 0 ]])
points = np.array ([[ 842 , 414 ], [ 544.48657 , 416.01022 ], [ 546.0603 , 121.71874 ], [ 819.1106 , 141.89487 ]])
rvec = np.array ([[ 0.0 ,  0.0 , 0.0 ]])
tvec = np.array ([[ 0.0 ,  0.0 , 0.0 ]])

succeeded, rvec, tvec = cv.solvePnP ( qrcode_sizes , points , mtx , dist , rvec , tvec )
print ( succeeded ) # returns True
print ( rvec ) # returns [[ 2.19557741] [-2.21423351] [ 0.389877  ]]
print ( tvec ) # returns [[0.12095571] [0.03197572] [0.35943074]]

C#:

float qrSize = 0.176f;
MatOfPoint2f pointsTransformed = new MatOfPoint2f(new Point(842, 414), new Point(544.48657, 416.01022), new Point(546.0603, 121.71874), new Point(819.1106, 141.89487));
MatOfPoint3f objectPoints = new MatOfPoint3f(new Point3(0, 0, 0), new Point3(0, qrSize, 0), new Point3(qrSize, qrSize, 0), new Point3(qrSize, 0, 0));
Mat camera_matrix = new Mat(3, 3, CvType.CV_64FC1);
camera_matrix.put(0, 0, new double[] { 600, 0, 1280 / 2, 0, 600, 720 / 2, 0, 0, 1 });
MatOfDouble camera_distortions = new MatOfDouble(0, 0, 0, 0);

bool succeeded = Calib3d.solvePnP(objectPoints, pointsTransformed, camera_matrix, camera_distortions, rvec, tvec);
Debug.Log(succeeded); // returns True
Debug.Log(rvec.dump()); // returns [2.195577403858928; -2.214233531573715; 0.3898769918731001]
Debug.Log(tvec.dump()); // returns [0.1209557065023445; 0.03197572143545069; 0.3594307467251833]