2013-02-12 196 views
4

嗨,我正在做一個項目,做一個圖像三維重建。我是校準相機的階段,這需要很長時間才能完成。但是,當我編譯代碼並在相機前面顯示棋盤時,它會直接處理異常錯誤。攝像機校準opencv

當圖片不在框架中時,只要框架中沒有錯誤,未處理的錯誤就會發生,我不知道爲什麼。

我問過很多人,沒有任何身體可以幫忙。

這裏是我的代碼

#include <cv.h> 
#include <highgui.h> 
#include <vector> 
#include <stdlib.h> 
#include <stdio.h> 

using namespace cv; 
using namespace std; 

int main() 
{ 
    int numBoards = 0; 
    int numCornersHor; 
    int numCornersVer; 

    printf("Enter number of corners along width: "); 
    scanf("%d", &numCornersHor); 

    printf("Enter number of corners along height: "); 
    scanf("%d", &numCornersVer); 

    printf("Enter number of boards: "); 
    scanf("%d", &numBoards); 

    int numSquares = numCornersHor * numCornersVer; 
    Size board_sz = Size(numCornersHor, numCornersVer); 
    VideoCapture capture = VideoCapture(0); 

    vector<vector<Point3d>> object_points; 
    vector<vector<Point2d>> image_points; 

    vector<Point2d> corners; 
    int successes=0; 

    Mat image; 
    Mat gray_image; 
    capture >> image; 

    vector<Point3d> obj; 
    for(int j=0;j<numSquares;j++) 
     obj.push_back(Point3d(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); 
     } 

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

     capture >> image; 

     int key = waitKey(1); 

     if(key==27) 
      return 0; 

     if(key==' ' && found!=0) 
     { 
      image_points.push_back(corners); 
      object_points.push_back(obj); 
      printf("Snap stored!\n"); 

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

    Mat imageUndistorted; 
    while(1) 
    { 
     capture >> image; 
     undistort(image, imageUndistorted, intrinsic, distCoeffs); 

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

     waitKey(1); 
    } 

    capture.release(); 

    return 0; 
} 

我得到的控制檯上的錯誤是

OpenCV的錯誤:斷言失敗(ncorners> = 0 & & corners.depth()== CV_32F)未知功能文件,文件..... \ SRC \ OpenCV的\模塊\ imgproc的\ src \ cornersubpix.cpp,線257

和錯誤對話框說

basiccalibration.exe中的0x769afc16未處理的異常:Microsoft C++異常:cv ::內存位置0x0021f51c處的異常..

幫助將不勝感激。 謝謝

+0

opencv軟件包內有一個示例代碼來校準攝像頭,您使用的是? – 2013-02-13 21:22:49

+0

不,我想寫我自己的代碼。我試過,但它有太多的錯誤,如未定義的函數。 – 2013-02-15 15:16:16

+0

我正在嘗試運行與您相同的代碼。其實我的代碼有點不一樣,但是我修改了它來讓一些東西接近你並嘗試在這裏解決我的問題。一些問題: 在你的最終版本中,你真的使用numCornersHor兩次?爲什麼你不使用numCornersVer作爲第二個參數? 當你聲明它們時你是否將它們定義爲float?謝謝你的幫助! – user2414816 2013-07-19 14:46:52

回答

3

使用Point2f和Point3f而不是Point2d和Point3d。請閱讀斷言文本。它需要一個CV_32F深度結構。

+0

即時通訊新的,所以我不明白的錯誤,所以我不是你的意思,要求CV_32F深度結構。當我使用點2f和3f它說警告C4244:'參數':從'int'轉換爲'浮動',可能丟失線數據'obj.push_back(Point3f(j/numCornersHor,j%numCornersHor,0.0f ));」它顯示了兩次,所以建築失敗,這就是爲什麼我將它改爲point3d和point2d。 – 2013-02-15 15:17:18

+0

所以我得到錯誤。任何建議,感謝您的答覆。希望我能把這個整理出來。 – 2013-02-15 15:24:01

+0

你知道,警告是存在的,因爲你將一個整數分配給一個浮點數爲'(j/numCornersHor,j%cornersHor)'的地方。關於深度,它在[cv :: Mat.depth()']的文檔(http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-depth) – morynicz 2013-02-15 16:46:53