2011-05-07 145 views
0

我想将cvProjectPoints2的简单代码转换为C++,所以我使用cv :: ProjectPoints。我usingcv命名空间,以避免一切前缀与cv::将projectPoints从C转换为C++

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 
    ); 
} 

这根本不会编译。 projectPoints的参数有什么问题?

+0

而错误信息是...? – 2011-05-07 22:49:16

回答

0

documentation我发现给出了以下声明为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); 

在任何情况下,你传递指针这些对象,而不是对象本身。

问题放在一边,为什么你正在使用动态分配这里—它几乎肯定不是必要的,你可能有—你传递什么projectPoints之前需要取消引用指针内存泄漏:

projectPoints(
    *object_points, 
    *rotation_vector, 
    *translation_vector, 
    *intrinsic_matrix, 
    0, 
    *image_points 
); 

你那么需要为distCoeffs参数(可能是空的Mat对象?)找到要传递的内容,因为0不是const Mat&

希望有所帮助。