Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Rotating target changes distances computed with solvePnP

I've been working on finding the distance to a target (of known real-world dimensions). The distance seems consistent enough when viewed from a forward-facing angle, but when I rotate the target (without translating it) the distance changes:

image description

Here is the relevant code:

vector<Point3f> objectPoints;
objectPoints.push_back(Point3f(-1, -1, 0));
objectPoints.push_back(Point3f(-1, 1, 0));
objectPoints.push_back(Point3f(1, 1, 0));
objectPoints.push_back(Point3f(1, -1, 0));
Mat objectPointsMat(objectPoints);

Mat rvec;
Mat tvec;
solvePnP(objectPointsMat, inputContour, cameraMatrix, distortionCoeffs, rvec, tvec);

double tx = tvec.at<double>(0, 0);
double ty = tvec.at<double>(1, 0);
double tz = tvec.at<double>(2, 0);
double dist = std::sqrt(tx*tx + ty*ty + tz*tz);

char *str;
asprintf(&str, "DIST = %7.3f", dist);
putText(original, str, Point(0, original.size().height), CV_FONT_HERSHEY_PLAIN,
    3, Scalar(255, 255, 255), 3);

Rotating target changes distances computed with solvePnP

I've been working on finding the distance to a target (of known real-world dimensions). The distance seems consistent enough when viewed from a forward-facing angle, but when I rotate the target (without translating it) the distance changes:

image description

Here is the relevant code:

vector<Point3f> objectPoints;
objectPoints.push_back(Point3f(-1, -1, 0));
objectPoints.push_back(Point3f(-1, 1, 0));
objectPoints.push_back(Point3f(1, 1, 0));
objectPoints.push_back(Point3f(1, -1, 0));
Mat objectPointsMat(objectPoints);

Mat rvec;
Mat tvec;
solvePnP(objectPointsMat, inputContour, cameraMatrix, distortionCoeffs, rvec, tvec);

double tx = tvec.at<double>(0, 0);
double ty = tvec.at<double>(1, 0);
double tz = tvec.at<double>(2, 0);
double dist = std::sqrt(tx*tx + ty*ty + tz*tz);

char *str;
asprintf(&str, "DIST = %7.3f", dist);
putText(original, str, Point(0, original.size().height), CV_FONT_HERSHEY_PLAIN,
    3, Scalar(255, 255, 255), 3);

EDIT: As Eduardo suggested, I have drawn the computed prose. I have also changed the objects points to match the width/height ratio of the actual target using:

objectPoints.push_back(Point3f(-1, -0.7, 0));
objectPoints.push_back(Point3f(-1, 0.7, 0));
objectPoints.push_back(Point3f(1, 0.7, 0));
objectPoints.push_back(Point3f(1, -0.7, 0));

The issue is that while yawing seems to work now, rolling makes the distance jump: image description