2014-03-05 207 views
0

失敗時總是收到一個assertion failed(0 < =我& &我<(INT)vv.size())in cv::_InputArray::getMat,文件matrix.cpp試圖運行projectPoints時。我已經檢查了所有我能想到的東西,但我無法弄清楚什麼是錯的。我的猜測是calibratecamera出了問題,但我不知道我會如何確切地發現問題。這是我的代碼,謝謝。斷言試圖運行projectPoints

using namespace cv; 
using namespace std; 
std::vector<cv::Point3f> Generate3DPoints() 
{ 
    std::vector<cv::Point3f> points; 

    float x,y,z; 

    x=.5;y=.5;z=.5; 
    points.push_back(cv::Point3f(x,y,z)); 

    x=0;y=0;z=0; 
    points.push_back(cv::Point3f(x,y,z)); 

    x=-0;y=0;z=.5; 
    points.push_back(cv::Point3f(x,y,z)); 

    x=0;y=.5;z=.5; 
    points.push_back(cv::Point3f(x,y,z)); 

    x=0;y=-.5;z=0; 
    points.push_back(cv::Point3f(x,y,z)); 

    x=.5;y=0;z=.5; 
    points.push_back(cv::Point3f(x,y,z)); 

    x=.5;y=0;z=0; 
    points.push_back(cv::Point3f(x,y,z)); 

    /* 
    for(unsigned int i = 0; i < points.size(); ++i) 
    { 
    std::cout << points[i] << std::endl; 
    } 
    */ 
    return points; 
} 
int main() 
{ 
    int numBoards = 4; 
    int numCornersHor = 6; 
    int numCornersVer = 9; 
    int numSquares = numCornersHor * numCornersVer; 
    Size board_sz = Size(numCornersHor, numCornersVer); 

    VideoCapture capture = VideoCapture(0); 

    vector<vector<Point3f>> object_points; 
    vector<vector<Point2f>> imagePoints; 
    vector<vector<Point2f>> image_points; 
    vector<Point3f> objectPoints1 = Generate3DPoints(); 

    vector<Point2f> corners; 
    int successes=0; 

    Mat image; 
    Mat gray_image; 
    //capture >> image; 
    Sleep(1000); 
    capture.read(image); 

    imshow("Welcome", image); 
    vector<Point3f> obj; 
    for(int j=0;j<numSquares;j++) 
    obj.push_back(Point3f(j/numCornersHor, j%numCornersHor, 0.0f)); 

    while(successes<numBoards) 
    { 
     cvtColor(image, gray_image, CV_BGR2GRAY); 
     bool found = findChessboardCorners(image, board_sz, corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS); 

     if(found) 
     { 
      cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1)); 
      drawChessboardCorners(gray_image, board_sz, corners, found); 
      cout << "SUCESS\n"; 

     } 
     else 
      cout << "FAIL\n"; 

     imshow("win1", image); 
     imshow("win2", gray_image); 

     capture.read(image); 
     image_points.push_back(corners); 
     object_points.push_back(obj); 
     printf("Snap stored!\n"); 
     char continues; 
     cout << "Press c to continue\n"; 
     cin >> continues; 
     successes++; 

     //if(successes>=numBoards) 
     // break; 


    } 

    Mat intrinsic = Mat(3, 3, CV_32FC1); 
    Mat distCoeffs; 
    vector<Mat> rvecs; 
    vector<Mat> tvecs; 

    intrinsic.ptr<float>(0)[0] = 1; 
    intrinsic.ptr<float>(1)[1] = 1; 

    calibrateCamera(object_points, image_points, image.size(), intrinsic, distCoeffs, rvecs, tvecs); 

    cout << "Done\n"; 


    projectPoints(objectPoints1, rvecs, tvecs, intrinsic, distCoeffs, imagePoints); 

    cout << "done2"; 

    return 0; 
} 
+2

*哪個*斷言失敗? – berak

+0

對不起[此處爲剪貼簿](http://i.imgur.com/zhH7JBt.png)。 – OneTwo

+1

將它作爲文本添加到問題 – bolov

回答

1

您正在試圖通過rvecstvecs的功能projectPoints,其中預計Mat而不是vector<Mat>

基本上,calibrateCamera函數使用幾個棋盤圖像來估計相機的固有矩陣和失真係數。該函數還返回extrinsics參數的多個版本(旋轉+平移),每個圖像使用一個。這些外在參數存儲在rvecstvecs中。

然而,要在只有這些圖像中的一個項目的三維點,所以你應該選擇什麼樣的設置要相對應的圖像,其中要投影的對象外在參數(即rvecs[i]tvecs[i]i的)並且僅針對這些外部參數調用projectPoints,而不是整個vectors<Mat>

+0

好吧,雖然錯誤仍然存​​在後,我改變了代碼。 .. 'calibrateCamera(object_points,image_points,image.size(),特性,distCoeffs,rvecs,tvecs); \t墊rvecs1 = rvecs [1]; \t墊tvecs1 = tvecs [1]; \t projectPoints (objectPoints1,rvecs1,tvecs1,intrinsic,distCoeffs,imagePoints);' – OneTwo

+0

感謝您的幫助我明白了,這是您所說的並且我的imagePoints被宣佈爲錯誤的組合。不知道爲什麼說實話,但我改變了它到 'std :: vector imagePoints;'它現在起作用了。 – OneTwo

+0

謝謝!這幫助我解決了與solveP2P()類似的錯誤。我正在從calibrateCamera()函數移植類似的輸入參數,但沒有發現變化。 – Hintron