Converting projectPoints from C to C++
I'm trying to convert simple code for cvProjectPoints2 to C++, so I'm using cv::ProjectPoints. I'm using
the cv
namespace to avoid prefixing everything with cv::
Mat_<double>* object_points = new Mat_<double>(10, 3, CV_64FC1);
Mat_<double>* rotation_vector = new Mat_<double>(3,3开发者_如何学编程, CV_64FC1);
Mat_<double>* translation_vector = new Mat_<double>(Size(3,1), CV_64FC1);
Mat_<double>* intrinsic_matrix = new Mat_<double>(Size(3, 3), CV_64FC1);
vector<Point2f>* image_points = new vector<Point2f>;
double t[] = {
70, 95, 120
};
double object[] = {
150, 200, 400,
0,0,0,
0,0,0,
0,0,0,
0,0,0,
0,0,0,
0,0,0,
0,0,0,
0,0,0,
0,0,0
};
double rotation[] = {
0, 1, 0,
-1, 0, 0,
0, 0, 1
};
double intrinsic[] = {
-500, 0, 320,
0, -500, 240,
0, 0, 1
};
int main() {
for (int i = 0; i < 30; i++) {
(*object_points)[i/3][i%3] = object[i];
}
for (int i = 0; i < 9; i++) {
(*rotation_vector)[i/3][i%3] = rotation[i];
(*intrinsic_matrix)[i/3][i%3] = intrinsic[i];
}
for (int i = 0; i < 3; i++) {
(*translation_vector)[0][i] = t[i];
}
projectPoints(
object_points,
rotation_vector,
translation_vector,
intrinsic_matrix,
0,
image_points
);
}
This simply will not compile. What is wrong with the parameters to projectPoints
?
The documentation I've found gives the following declarations for projectPoints
:
void projectPoints(const Mat& objectPoints, const Mat& rvec, const Mat& tvec, const Mat& cameraMatrix, const Mat& distCoeffs, vector<Point2f>& imagePoints);
void projectPoints(const Mat& objectPoints, const Mat& rvec, const Mat& tvec, const Mat& cameraMatrix, const Mat& distCoeffs, vector<Point2f>& imagePoints, Mat& dpdrot, Mat& dpdt, Mat& dpdf, Mat& dpdc, Mat& dpddist, double aspectRatio=0);
In all cases, you're passing pointers to these objects, not the objects themselves.
Questions aside as to why you're using dynamic allocation here — it's almost certainly not necessary, and you probably have memory leaks — you need to dereference the pointers before passing anything to projectPoints
:
projectPoints(
*object_points,
*rotation_vector,
*translation_vector,
*intrinsic_matrix,
0,
*image_points
);
You then need to find something to pass for the distCoeffs
parameter (perhaps an empty Mat
object?) because 0
is not a const Mat&
.
Hope that helps.
精彩评论