<commit_before>#include "ofxCv/Calibration.h"
#include "ofxCv/Helpers.h"
#include "ofFileUtils.h"

namespace ofxCv {

	using namespace cv;
	
	void Intrinsics::setup(Mat cameraMatrix, cv::Size imageSize, cv::Size sensorSize) {
		this->cameraMatrix = cameraMatrix;
		this->imageSize = imageSize;
		this->sensorSize = sensorSize;
		calibrationMatrixValues(cameraMatrix, imageSize, sensorSize.width, sensorSize.height,
														fov.x, fov.y, focalLength, principalPoint, aspectRatio);

	}
	
	Mat Intrinsics::getCameraMatrix() const {
		return cameraMatrix;
	}
	
	cv::Size Intrinsics::getImageSize() const {
		return imageSize;
	}
	
	cv::Size Intrinsics::getSensorSize() const {
		return sensorSize;
	}
	
	cv::Point2d Intrinsics::getFov() const {
		return fov;
	}
	
	double Intrinsics::getFocalLength() const {
		return focalLength;
	}
	
	double Intrinsics::getAspectRatio() const {
		return aspectRatio;
	}
	
	Point2d Intrinsics::getPrincipalPoint() const {
		return principalPoint;
	}
	
	void Intrinsics::loadProjectionMatrix(float nearDist, float farDist) const {
		glViewport(0, 0, imageSize.width, imageSize.height);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		float w = imageSize.width;
		float h = imageSize.height;
		float fx = cameraMatrix.at<double>(0, 0);
		float fy = cameraMatrix.at<double>(1, 1);
		float cx = principalPoint.x;
		float cy = principalPoint.y;
		glFrustum(
			nearDist * (-cx) / fx, nearDist * (w - cx) / fx,
			nearDist * (cy - h) / fy, nearDist * (cy) / fy,
			nearDist, farDist);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		gluLookAt(
			0, 0, 0,
			0, 0, 1,
			0, -1, 0);
	}
	
	Calibration::Calibration() :
		patternType(CHESSBOARD),
		patternSize(cv::Size(10, 7)), squareSize(2.5), // based on Chessboard_A4.pdf, assuming world units are centimeters
		fillFrame(true),
		ready(false) {
		
	}
	
	void Calibration::save(string filename, bool absolute) const {
		if(!ready){
			ofLog(OF_LOG_ERROR, "Calibration::save() failed, because your calibration isn't ready yet!");
		}
		FileStorage fs(ofToDataPath(filename, absolute), FileStorage::WRITE);
		cv::Size imageSize = distortedIntrinsics.getImageSize();
		cv::Size sensorSize = distortedIntrinsics.getSensorSize();
		Mat cameraMatrix = distortedIntrinsics.getCameraMatrix();
		fs << "cameraMatrix" << cameraMatrix;
		fs << "imageSize_width" << imageSize.width;
		fs << "imageSize_height" << imageSize.height;
		fs << "sensorSize_width" << sensorSize.width;
		fs << "sensorSize_height" << sensorSize.height;
		fs << "distCoeffs" << distCoeffs;
		fs << "reprojectionError" << reprojectionError;
		fs << "features" << "[";
		for(int i = 0; i < imagePoints.size(); i++) {
			fs << "[:" << imagePoints[i] << "]";
		}
		fs << "]";
	}
	
	void Calibration::load(string filename, bool absolute) {
		imagePoints.clear();
		FileStorage fs(ofToDataPath(filename, absolute), FileStorage::READ);
		cv::Size imageSize, sensorSize;
		Mat cameraMatrix;
		int pointCount;
		fs["cameraMatrix"] >> cameraMatrix;
		fs["imageSize_width"] >> imageSize.width;
		fs["imageSize_height"] >> imageSize.height;
		fs["sensorSize_width"] >> sensorSize.width;
		fs["sensorSize_height"] >> sensorSize.height;
		fs["distCoeffs"] >> distCoeffs;
		fs["reprojectionError"] >> reprojectionError;
		FileNode features = fs["features"];
		for(FileNodeIterator it = features.begin(); it != features.end(); it++) {
			vector<Point2f> cur;
			(*it) >> cur;
			imagePoints.push_back(cur);
		}
		addedImageSize = imageSize;
		calibrate();		
	}
	void Calibration::setPatternType(CalibrationPattern patternType) {
		this->patternType = patternType;
	}
	void Calibration::setPatternSize(int xCount, int yCount) {
		patternSize = cv::Size(xCount, yCount);
	}
	void Calibration::setSquareSize(float squareSize) {
		this->squareSize = squareSize;
	}
	void Calibration::setFillFrame(bool fillFrame) {
		this->fillFrame = fillFrame;
	}
	bool Calibration::add(Mat img) {
		addedImageSize = img.size();
		
		vector<Point2f> pointBuf;
		
		// find corners
		bool found = findBoard(img, pointBuf);<commit_msg>important change to Calibration::load(), it doesn't recalibrate after loading. the calibration file should describe the calibration parameters, it shouldn't serve as a collection of image points for recalibrating.<commit_after>#include "ofxCv/Calibration.h"
#include "ofxCv/Helpers.h"
#include "ofFileUtils.h"

namespace ofxCv {

	using namespace cv;
	
	void Intrinsics::setup(Mat cameraMatrix, cv::Size imageSize, cv::Size sensorSize) {
		this->cameraMatrix = cameraMatrix;
		this->imageSize = imageSize;
		this->sensorSize = sensorSize;
		calibrationMatrixValues(cameraMatrix, imageSize, sensorSize.width, sensorSize.height,
														fov.x, fov.y, focalLength, principalPoint, aspectRatio);

	}
	
	Mat Intrinsics::getCameraMatrix() const {
		return cameraMatrix;
	}
	
	cv::Size Intrinsics::getImageSize() const {
		return imageSize;
	}
	
	cv::Size Intrinsics::getSensorSize() const {
		return sensorSize;
	}
	
	cv::Point2d Intrinsics::getFov() const {
		return fov;
	}
	
	double Intrinsics::getFocalLength() const {
		return focalLength;
	}
	
	double Intrinsics::getAspectRatio() const {
		return aspectRatio;
	}
	
	Point2d Intrinsics::getPrincipalPoint() const {
		return principalPoint;
	}
	
	void Intrinsics::loadProjectionMatrix(float nearDist, float farDist) const {
		glViewport(0, 0, imageSize.width, imageSize.height);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		float w = imageSize.width;
		float h = imageSize.height;
		float fx = cameraMatrix.at<double>(0, 0);
		float fy = cameraMatrix.at<double>(1, 1);
		float cx = principalPoint.x;
		float cy = principalPoint.y;
		glFrustum(
			nearDist * (-cx) / fx, nearDist * (w - cx) / fx,
			nearDist * (cy - h) / fy, nearDist * (cy) / fy,
			nearDist, farDist);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		gluLookAt(
			0, 0, 0,
			0, 0, 1,
			0, -1, 0);
	}
	
	Calibration::Calibration() :
		patternType(CHESSBOARD),
		patternSize(cv::Size(10, 7)), squareSize(2.5), // based on Chessboard_A4.pdf, assuming world units are centimeters
		fillFrame(true),
		ready(false) {
		
	}
	
	void Calibration::save(string filename, bool absolute) const {
		if(!ready){
			ofLog(OF_LOG_ERROR, "Calibration::save() failed, because your calibration isn't ready yet!");
		}
		FileStorage fs(ofToDataPath(filename, absolute), FileStorage::WRITE);
		cv::Size imageSize = distortedIntrinsics.getImageSize();
		cv::Size sensorSize = distortedIntrinsics.getSensorSize();
		Mat cameraMatrix = distortedIntrinsics.getCameraMatrix();
		fs << "cameraMatrix" << cameraMatrix;
		fs << "imageSize_width" << imageSize.width;
		fs << "imageSize_height" << imageSize.height;
		fs << "sensorSize_width" << sensorSize.width;
		fs << "sensorSize_height" << sensorSize.height;
		fs << "distCoeffs" << distCoeffs;
		fs << "reprojectionError" << reprojectionError;
		fs << "features" << "[";
		for(int i = 0; i < imagePoints.size(); i++) {
			fs << "[:" << imagePoints[i] << "]";
		}
		fs << "]";
	}
	
	void Calibration::load(string filename, bool absolute) {
		imagePoints.clear();
		FileStorage fs(ofToDataPath(filename, absolute), FileStorage::READ);
		cv::Size imageSize, sensorSize;
		Mat cameraMatrix;
		int pointCount;
		fs["cameraMatrix"] >> cameraMatrix;
		fs["imageSize_width"] >> imageSize.width;
		fs["imageSize_height"] >> imageSize.height;
		fs["sensorSize_width"] >> sensorSize.width;
		fs["sensorSize_height"] >> sensorSize.height;
		fs["distCoeffs"] >> distCoeffs;
		fs["reprojectionError"] >> reprojectionError;
		FileNode features = fs["features"];
		for(FileNodeIterator it = features.begin(); it != features.end(); it++) {
			vector<Point2f> cur;
			(*it) >> cur;
			imagePoints.push_back(cur);
		}
		addedImageSize = imageSize;
	}
	void Calibration::setPatternType(CalibrationPattern patternType) {
		this->patternType = patternType;
	}
	void Calibration::setPatternSize(int xCount, int yCount) {
		patternSize = cv::Size(xCount, yCount);
	}
	void Calibration::setSquareSize(float squareSize) {
		this->squareSize = squareSize;
	}
	void Calibration::setFillFrame(bool fillFrame) {
		this->fillFrame = fillFrame;
	}
	bool Calibration::add(Mat img) {
		addedImageSize = img.size();
		
		vector<Point2f> pointBuf;
		
		// find corners
		bool found = findBoard(img, pointBuf);
{'fraction_non_alphanumeric': 0.13913358271992277, 'fraction_numerical': 0.0062748883793894055, 'mean_word_length': 4.743589743589744, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4203283', 'n_tokens_mistral': 3119, 'n_tokens_neox': 2762, 'n_words': 603}