2011-05-07 73 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&

希望有所幫助。