Coder Social home page Coder Social logo

Comments (1)

EnoxSoftware avatar EnoxSoftware commented on August 16, 2024

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]

from opencvforunity.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.