Пример #1
0
void CalibrateThread::saveCameraParams( const string& filename,
                       Size imageSize, Size boardSize,
                       float squareSize, float aspectRatio, int flags,
                       const Mat& cameraMatrix, const Mat& distCoeffs,
                       const vector<Mat>& rvecs, const vector<Mat>& tvecs,
                       const vector<float>& reprojErrs,
                       const vector<vector<Point2f> >& imagePoints,
                       double totalAvgErr )
{
    FileStorage fs( filename, FileStorage::WRITE );

    time_t t;
    time( &t );
    struct tm *t2 = localtime( &t );
    char buf[1024];
    strftime( buf, sizeof(buf)-1, "%c", t2 );

    fs << "calibration_time" << buf;

    if( !rvecs.empty() || !reprojErrs.empty() )
        fs << "nframes" << (int)std::max(rvecs.size(), reprojErrs.size());
    fs << "image_width" << imageSize.width;
    fs << "image_height" << imageSize.height;
    fs << "board_width" << boardSize.width;
    fs << "board_height" << boardSize.height;
    fs << "square_size" << squareSize;

    if( flags & CV_CALIB_FIX_ASPECT_RATIO )
        fs << "aspectRatio" << aspectRatio;

    if( flags != 0 )
    {
        sprintf( buf, "flags: %s%s%s%s",
            flags & CV_CALIB_USE_INTRINSIC_GUESS ? "+use_intrinsic_guess" : "",
            flags & CV_CALIB_FIX_ASPECT_RATIO ? "+fix_aspectRatio" : "",
            flags & CV_CALIB_FIX_PRINCIPAL_POINT ? "+fix_principal_point" : "",
            flags & CV_CALIB_ZERO_TANGENT_DIST ? "+zero_tangent_dist" : "" );
        cvWriteComment( *fs, buf, 0 );
    }

    fs << "flags" << flags;

    fs << "camera_matrix" << cameraMatrix;
    fs << "distortion_coefficients" << distCoeffs;

    fs << "avg_reprojection_error" << totalAvgErr;
    if( !reprojErrs.empty() )
        fs << "per_view_reprojection_errors" << Mat(reprojErrs);

    if( !rvecs.empty() && !tvecs.empty() )
    {
        CV_Assert(rvecs[0].type() == tvecs[0].type());
        Mat bigmat((int)rvecs.size(), 6, rvecs[0].type());
        for( int i = 0; i < (int)rvecs.size(); i++ )
        {
            Mat r = bigmat(Range(i, i+1), Range(0,3));
            Mat t = bigmat(Range(i, i+1), Range(3,6));

            CV_Assert(rvecs[i].rows == 3 && rvecs[i].cols == 1);
            CV_Assert(tvecs[i].rows == 3 && tvecs[i].cols == 1);
            //*.t() is MatExpr (not Mat) so we can use assignment operator
            r = rvecs[i].t();
            t = tvecs[i].t();
        }
        cvWriteComment( *fs, "a set of 6-tuples (rotation vector + translation vector) for each view", 0 );
        fs << "extrinsic_parameters" << bigmat;
    }

    if( !imagePoints.empty() )
    {
        Mat imagePtMat((int)imagePoints.size(), imagePoints[0].size(), CV_32FC2);
        for( int i = 0; i < (int)imagePoints.size(); i++ )
        {
            Mat r = imagePtMat.row(i).reshape(2, imagePtMat.cols);
            Mat imgpti(imagePoints[i]);
            imgpti.copyTo(r);
        }
        fs << "image_points" << imagePtMat;
    }
}
Пример #2
0
void CameraCalibration::saveCameraParams( Settings& s, Size& imageSize, Mat& cameraMatrix, Mat& distCoeffs,
                              const vector<Mat>& rvecs, const vector<Mat>& tvecs,
                              const vector<float>& reprojErrs, const vector<vector<Point2f> >& imagePoints,
                              double totalAvgErr )
{
    FileStorage fs( s.outputFileName, FileStorage::WRITE );

    time_t tm;
    time( &tm );
    struct tm *t2 = localtime( &tm );
    char buf[1024];
    strftime( buf, sizeof(buf)-1, "%c", t2 );

    fs << "calibration_Time" << buf;

    if ( !rvecs.empty() || !reprojErrs.empty() ) {
        fs << "nrOfFrames" << (int)max( rvecs.size(), reprojErrs.size() );
    }
    fs << "image_Width" << imageSize.width;
    fs << "image_Height" << imageSize.height;
    fs << "board_Width" << s.boardSize.width;
    fs << "board_Height" << s.boardSize.height;
    fs << "square_Size" << s.squareSize;

    if ( s.flag & CALIB_FIX_ASPECT_RATIO ) {
        fs << "FixAspectRatio" << s.aspectRatio;
    }

    if ( s.flag ) {
        sprintf( buf, "flags: %s%s%s%s",
            s.flag & CALIB_USE_INTRINSIC_GUESS ? " +use_intrinsic_guess" : "",
            s.flag & CALIB_FIX_ASPECT_RATIO ? " +fix_aspectRatio" : "",
            s.flag & CALIB_FIX_PRINCIPAL_POINT ? " +fix_principal_point" : "",
            s.flag & CALIB_ZERO_TANGENT_DIST ? " +zero_tangent_dist" : "" );
        //cvWriteComment( *fs, buf, 0 );
    }

    fs << "flagValue" << s.flag;
    fs << "Camera_Matrix" << cameraMatrix;
    fs << "Distortion_Coefficients" << distCoeffs;
    fs << "Avg_Reprojection_Error" << totalAvgErr;
    if ( !reprojErrs.empty() ) {
        fs << "Per_View_Reprojection_Errors" << Mat(reprojErrs);
    }

    if ( !rvecs.empty() && !tvecs.empty() ) {
        CV_Assert( rvecs[0].type() == tvecs[0].type() );
        Mat bigmat( (int)rvecs.size(), 6, rvecs[0].type() );
        for ( int i = 0; i < (int)rvecs.size(); i++ ) {
            Mat r = bigmat( Range( i, i + 1 ), Range( 0, 3 ) );
            Mat t = bigmat( Range( i, i + 1 ), Range( 3, 6 ) );

            CV_Assert( rvecs[i].rows == 3 && rvecs[i].cols == 1 );
            CV_Assert( tvecs[i].rows == 3 && tvecs[i].cols == 1 );
            //*.t() is MatExpr (not Mat) so we can use assignment operator
            r = rvecs[i].t();
            t = tvecs[i].t();
        }
        //cvWriteComment( *fs, "a set of 6-tuples (rotation vector + translation vector) for each view", 0 );
        fs << "Extrinsic_Parameters" << bigmat;
    }

    if ( !imagePoints.empty() ) {
        Mat imagePtMat( (int)imagePoints.size(), (int)imagePoints[0].size(), CV_32FC2 );
        for ( int i = 0; i < (int)imagePoints.size(); i++ ) {
            Mat r = imagePtMat.row(i).reshape( 2, imagePtMat.cols );
            Mat imgpti( imagePoints[i] );
            imgpti.copyTo( r );
        }
        fs << "Image_points" << imagePtMat;
    }
}
void CameraCalibration::saveCameraParams(Settings& s, Size& imageSize, Mat& cameraMatrix, 
									Mat& distCoeffs, vector<Mat>& rvecs, vector<Mat>& tvecs, 
									vector<float>& reprojErrs, vector<vector<Point2f>>& imagePoints,
									double totalAvgErr) 
{
	FileStorage fs(s.outputFileName, FileStorage::WRITE);

	time_t tm;
	time(&tm);
	struct tm *t2 = localtime(&tm);
	char buf[1024];
	strftime(buf, sizeof(buf) - 1, "%c", t2);

	fs << "Results"; 
	fs << "{" <<"calibration_Time" << buf;

	if (!rvecs.empty() || !reprojErrs.empty())
		fs << "nrOfFrames" << (int)std::max(rvecs.size(), reprojErrs.size());
	fs << "image_Width" << imageSize.width;
	fs << "image_Height" << imageSize.height;
	fs << "board_Width" << s.boardSize.width;
	fs << "board_Height" << s.boardSize.height;
	fs << "square_Size" << s.squareSize;
	fs << "Stereo_Baseline" << s.StereoBaseline;
	fs << "Sensor_size_Width" << s.sensorSizeWidth;
	fs << "Sensor_size_Height" << s.sensorSizeHeight;
	fs << "Mx" << s.Mx;
	fs << "My" << s.My;

	if (s.flag & CALIB_FIX_ASPECT_RATIO)
		fs << "FixAspectRatio" << s.aspectRatio;

	if (s.flag)
	{
		sprintf_s(buf, "flags: %s%s%s%s",
			s.flag & CALIB_USE_INTRINSIC_GUESS ? " +use_intrinsic_guess" : "",
			s.flag & CALIB_FIX_ASPECT_RATIO ? " +fix_aspectRatio" : "",
			s.flag & CALIB_FIX_PRINCIPAL_POINT ? " +fix_principal_point" : "",
			s.flag & CALIB_ZERO_TANGENT_DIST ? " +zero_tangent_dist" : "");
		//cvWriteComment(*fs, buf, 0);
		
	}

	fs << "flagValue" << s.flag;

	fs << "Camera_Matrix" << cameraMatrix;
	fs << "Distortion_Coefficients" << distCoeffs;

	fs << "Avg_Reprojection_Error" << totalAvgErr;
	if (!reprojErrs.empty())
		fs << "Per_View_Reprojection_Errors" << Mat(reprojErrs);

	if (!rvecs.empty() && !tvecs.empty())
	{
		CV_Assert(rvecs[0].type() == tvecs[0].type());
		Mat bigmat((int)rvecs.size(), 6, rvecs[0].type());
		for (int i = 0; i < (int)rvecs.size(); i++)
		{
			Mat r = bigmat(Range(i, i + 1), Range(0, 3));
			Mat t = bigmat(Range(i, i + 1), Range(3, 6));

			CV_Assert(rvecs[i].rows == 3 && rvecs[i].cols == 1);
			CV_Assert(tvecs[i].rows == 3 && tvecs[i].cols == 1);
			//*.t() is MatExpr (not Mat) so we can use assignment operator
			r = rvecs[i].t();
			t = tvecs[i].t();
		}
		//cvWriteComment(*fs, "a set of 6-tuples (rotation vector + translation vector) for each view", 0);
		fs << "Extrinsic_Parameters" << bigmat;
	}

	if (!imagePoints.empty())
	{
		Mat imagePtMat((int)imagePoints.size(), (int)imagePoints[0].size(), CV_32FC2);
		for (int i = 0; i < (int)imagePoints.size(); i++)
		{
			Mat r = imagePtMat.row(i).reshape(2, imagePtMat.cols);
			Mat imgpti(imagePoints[i]);
			imgpti.copyTo(r);
		}
		fs << "Image_points" << imagePtMat;//Image_points

	}

	if (!DataFromCirclesPattern.empty()){

		// save the circle pattern positions for this camera and the corresponding calibration images
		// as a vector 1x6 <circleID, (x,y) 2D position,(X,Y,Z) 3D position> for each circle found
		cv::Mat circlesDataMatrix;

		for (int k = 0; k < DataFromCirclesPattern.size(); k++){

			int cameraID = DataFromCirclesPattern.at(k).cameraID;
			int imageID = DataFromCirclesPattern.at(k).imageID;

			cv::Mat circlesData = cv::Mat::zeros(1, 6, CV_64F);
			 
			circlesData.at<double>(0, 0) = cameraID;
			circlesData.at<double>(0, 1) = imageID;
			circlesData.at<double>(0, 2) = 0;
			circlesData.at<double>(0, 3) = 0;
			circlesData.at<double>(0, 4) = 0;
			circlesData.at<double>(0, 5) = 0;

			// save ID header row
			circlesDataMatrix.push_back(circlesData);

			for (int i = 0; i < DataFromCirclesPattern.at(k).circlesData.size(); i++){

				int circleID = DataFromCirclesPattern.at(k).circlesData.at(i).circleID;
				cv::Point2f circlePosition = DataFromCirclesPattern.at(k).circlesData.at(i).circlePosition;
				cv::Point3f circle3DPosition = DataFromCirclesPattern.at(k).circlesData.at(i).circle3DPosition;

				// build the matrix data
				circlesData.at<double>(0, 0) = circleID;
				circlesData.at<double>(0, 1) = circlePosition.x;
				circlesData.at<double>(0, 2) = circlePosition.y;
				circlesData.at<double>(0, 3) = circle3DPosition.x;
				circlesData.at<double>(0, 4) = circle3DPosition.y;
				circlesData.at<double>(0, 5) = circle3DPosition.z;

				circlesDataMatrix.push_back(circlesData);
			}

		}
		Mat finalCirclesDataMatrix(circlesDataMatrix.rows, 6, CV_64F);
		circlesDataMatrix.copyTo(finalCirclesDataMatrix);

		fs << "Circle_Data" << finalCirclesDataMatrix;		
		
	}


		
}