Closed Rombond closed 4 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]
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:
C#:
Did I miss something ? There is an OpenCV error but it doesn't help at all, here it is: