Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

cv::calibrateCamera calling _rvecs.create(nimages, 1, CV_64FC3) is failed

I use the following code to test cv::calibrateCamera, but there is an fatal error, cv::calibrateCamera calling _rvecs.create(nimages, 1, CV_64FC3) is failed. Curiously, the same code can run correct on Qt+MinGW platform, but can cause an error on VS2015 platform. Why? My openCV version is 3.1.0. Thank you!

void testCalibrate(void)

{ std::vector<std::vector<cv::point3f>> object_points_xyz_; std::vector<std::vector<cv::point2f>> image_points_xy_;

int rows = 1024;
int cols = 1280;

dlp::Image temp_calibration_image;
char buf[512];
cv::Mat calibration_image_cv;
cv::Mat pointBuf;
std::vector<cv::Point2f>  board_feature_locations_xy;
cv::Size board_feature_size(8, 6);

// Values are added during Setup() method. Assumes board is planar and that z = 0.
std::vector<cv::Point3f> calibration_board_feature_points_xyz_;

// Clear pattern points in case it has already been setup
calibration_board_feature_points_xyz_.clear();

// Assume that the first feature is located a 0,0 because the chessboard
// is in real space and has no direct relationship to the camera sensor
for (unsigned int iRow = 0; iRow < 6; iRow++) {
    for (unsigned int iCol = 0; iCol < 8; iCol++) {
        float x_pos = iCol * 5.555556; //this->board_column_distance_
        float y_pos = iRow * 6.071429; //this->board_row_distance_.Get(); 

        cv::Point3f feature_point = cv::Point3f(x_pos, y_pos, 0.0f);
        calibration_board_feature_points_xyz_.push_back(feature_point);
    }
}



for (int i = 0; i < 10; i++)
{
    sprintf(buf, "F:\\calibration_camera\\camera_only_calibration_capture_%d.bmp", i + 1);
    temp_calibration_image.Load(buf);

    if (temp_calibration_image.ConvertToMonochrome().hasErrors())
        return;

    // Convert dlp::Image to cv::Mat
    temp_calibration_image.GetOpenCVData(&calibration_image_cv);

    // Look for the chessboard (checkerboard corners
    if (cv::findChessboardCorners(calibration_image_cv,
        board_feature_size,
        pointBuf,
        CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS)) {

        // Board was found. Refine the corner positions
        // NOTE : Many of these arguments would be good parameter settings for calibration
        cv::cornerSubPix(calibration_image_cv,
            pointBuf,
            cv::Size(11, 11),
            cv::Size(-1, -1),
            cv::TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));

        board_feature_locations_xy.assign((cv::Point2f*)pointBuf.datastart, (cv::Point2f*)pointBuf.dataend);

        // Add this calibration image's corners to the list
        image_points_xy_.push_back(board_feature_locations_xy);
        object_points_xyz_.push_back(calibration_board_feature_points_xyz_);

        //clear
        temp_calibration_image.Clear();
        calibration_image_cv.release();
        pointBuf.release();
        board_feature_locations_xy.clear();
    }
}

// Create calibration flags
int cv_calibration_flags = 0;

// Determine which calibration data should be updated
cv::Mat intrinsic;
cv::Mat distortion;
cv::Mat extrinsic;
cv::Mat homography;

// std::vector<std::vector<cv::point3f>> object_points_xyz_; // std::vector<std::vector<cv::point2f>> image_points_xy_;

intrinsic.create(3, 3, CV_64FC1); //CV_INTRINSIC_SETUP
intrinsic.setTo(cv::Scalar(0));

distortion.create(5, 1, CV_64FC1);
distortion.setTo(cv::Scalar(0));

extrinsic.create(2, 3, CV_64FC1);
extrinsic.setTo(cv::Scalar(0));

homography.create(3, 3, CV_64FC1);
homography.setTo(cv::Scalar(0));



// Calibrate the camera
double               reproj_error;
std::vector<cv::Mat> rotation_vector;
std::vector<cv::Mat> translation_vector;
/// cv::Mat rotation;
/// cv::Mat translation;
cv::Size calibration_model_size = cv::Size(8, 6);

// Perform calibration
reproj_error = cv::calibrateCamera(object_points_xyz_,    // chessboard corners coordinate x, y, z = 0 in mm
    image_points_xy_,      // chessboard corners coordinate x, y in camera pixels
    calibration_model_size,
    intrinsic,                   // in pixels
    distortion,                  // in pixels
    rotation_vector,             // unitless direction angle of camera to calibration board
    translation_vector,          // translation vector x,y,z for each calibration board in mm
    cv_calibration_flags);

// Copy the rotation and translation vector to the extrinsic calibration data object.
// Only copy the vectors from the first pattern board.
cv::transpose(rotation_vector.at(0), extrinsic.row(dlp::Calibration::Data::EXTRINSIC_ROW_ROTATION));
cv::transpose(translation_vector.at(0), extrinsic.row(dlp::Calibration::Data::EXTRINSIC_ROW_TRANSLATION));

}

cv::calibrateCamera calling _rvecs.create(nimages, 1, CV_64FC3) is failed

I use the following code to test cv::calibrateCamera, but there is an fatal error, cv::calibrateCamera calling _rvecs.create(nimages, 1, CV_64FC3) is failed. Curiously, the same code can run correct on Qt+MinGW platform, but can cause an error on VS2015 platform. Why? My openCV version is 3.1.0. Thank you!

 void testCalibrate(void)

testCalibrate(void) { std::vector<std::vector<cv::point3f>> std::vector<std::vector<cv::Point3f>> object_points_xyz_; std::vector<std::vector<cv::point2f>> image_points_xy_;

    std::vector<std::vector<cv::Point2f>> image_points_xy_;

        int rows = 1024;
 int cols = 1280;

 dlp::Image temp_calibration_image;
 char buf[512];
 cv::Mat calibration_image_cv;
 cv::Mat pointBuf;
 std::vector<cv::Point2f>  board_feature_locations_xy;
 cv::Size board_feature_size(8, 6);

 // Values are added during Setup() method. Assumes board is planar and that z = 0.
 std::vector<cv::Point3f> calibration_board_feature_points_xyz_;

 // Clear pattern points in case it has already been setup
 calibration_board_feature_points_xyz_.clear();

 // Assume that the first feature is located a 0,0 because the chessboard
 // is in real space and has no direct relationship to the camera sensor
 for (unsigned int iRow = 0; iRow < 6; iRow++) {
     for (unsigned int iCol = 0; iCol < 8; iCol++) {
         float x_pos = iCol * 5.555556; //this->board_column_distance_
         float y_pos = iRow * 6.071429; //this->board_row_distance_.Get(); 

         cv::Point3f feature_point = cv::Point3f(x_pos, y_pos, 0.0f);
         calibration_board_feature_points_xyz_.push_back(feature_point);
     }
 }



 for (int i = 0; i < 10; i++)
 {
     sprintf(buf, "F:\\calibration_camera\\camera_only_calibration_capture_%d.bmp", i + 1);
     temp_calibration_image.Load(buf);

     if (temp_calibration_image.ConvertToMonochrome().hasErrors())
         return;

     // Convert dlp::Image to cv::Mat
     temp_calibration_image.GetOpenCVData(&calibration_image_cv);

     // Look for the chessboard (checkerboard corners
     if (cv::findChessboardCorners(calibration_image_cv,
         board_feature_size,
         pointBuf,
         CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS)) {

         // Board was found. Refine the corner positions
         // NOTE : Many of these arguments would be good parameter settings for calibration
         cv::cornerSubPix(calibration_image_cv,
             pointBuf,
             cv::Size(11, 11),
             cv::Size(-1, -1),
             cv::TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));

         board_feature_locations_xy.assign((cv::Point2f*)pointBuf.datastart, (cv::Point2f*)pointBuf.dataend);

         // Add this calibration image's corners to the list
         image_points_xy_.push_back(board_feature_locations_xy);
         object_points_xyz_.push_back(calibration_board_feature_points_xyz_);

         //clear
         temp_calibration_image.Clear();
         calibration_image_cv.release();
         pointBuf.release();
         board_feature_locations_xy.clear();
     }
 }

 // Create calibration flags
 int cv_calibration_flags = 0;

 // Determine which calibration data should be updated
 cv::Mat intrinsic;
 cv::Mat distortion;
 cv::Mat extrinsic;
 cv::Mat homography;

// std::vector<std::vector<cv::point3f>> // std::vector<std::vector<cv::Point3f>> object_points_xyz_; // std::vector<std::vector<cv::point2f>> image_points_xy_;

    //      std::vector<std::vector<cv::Point2f>> image_points_xy_;

        intrinsic.create(3, 3, CV_64FC1); //CV_INTRINSIC_SETUP
 intrinsic.setTo(cv::Scalar(0));

 distortion.create(5, 1, CV_64FC1);
 distortion.setTo(cv::Scalar(0));

 extrinsic.create(2, 3, CV_64FC1);
 extrinsic.setTo(cv::Scalar(0));

 homography.create(3, 3, CV_64FC1);
 homography.setTo(cv::Scalar(0));



 // Calibrate the camera
 double               reproj_error;
 std::vector<cv::Mat> rotation_vector;
 std::vector<cv::Mat> translation_vector;
 /// cv::Mat rotation;
 /// cv::Mat translation;
 cv::Size calibration_model_size = cv::Size(8, 6);

 // Perform calibration
 reproj_error = cv::calibrateCamera(object_points_xyz_,    // chessboard corners coordinate x, y, z = 0 in mm
     image_points_xy_,      // chessboard corners coordinate x, y in camera pixels
     calibration_model_size,
     intrinsic,                   // in pixels
     distortion,                  // in pixels
     rotation_vector,             // unitless direction angle of camera to calibration board
     translation_vector,          // translation vector x,y,z for each calibration board in mm
     cv_calibration_flags);

 // Copy the rotation and translation vector to the extrinsic calibration data object.
 // Only copy the vectors from the first pattern board.
 cv::transpose(rotation_vector.at(0), extrinsic.row(dlp::Calibration::Data::EXTRINSIC_ROW_ROTATION));
 cv::transpose(translation_vector.at(0), extrinsic.row(dlp::Calibration::Data::EXTRINSIC_ROW_TRANSLATION));

    }

}