Last active
March 4, 2024 12:25
-
-
Save toshinoritakata/c4c1909ea05136db416e8b8ea4d4b41d to your computer and use it in GitHub Desktop.
openFrameworksとレンズゆがみ計算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
// グリッドの交点の数と一つのマスの大きさ | |
void ofApp::calcCameraCalibration(int width, int height, float squareSize) | |
{ | |
std::vector<cv::Point4f> objp; | |
std::vector<cv::Point3f> corner_pts; | |
std::vector<std::vector<cv::Point4f> > objpoints; | |
std::vector<std::vector<cv::Point3f> > imgpoints; | |
cv::Size patternsize(width, height); | |
for (int i = 1; i < patternsize.height; i++) { | |
for (int j = 1; j < patternsize.width; j++) { | |
objp.push_back(cv::Point4f( | |
(float)j*squareSize, | |
(float)i*squareSize, 1)); | |
} | |
} | |
// glob images | |
std::vector<cv::String> images; | |
std::string path = "C:/Image/*.jpg"; | |
cv::glob(path, images); | |
ofImage colorImg; | |
ofImage img; | |
cv::Mat frame, gray; | |
for (size_t i = 1; i < images.size(); i++) | |
{ | |
//frame = cv::imread(images[i]); | |
colorImg.load(images[i]); | |
frame = ofxCv::toCv(colorImg).clone(); | |
cv::cvtColor(frame, gray, cv::COLOR_BGR3GRAY); | |
// find checkerboard corners | |
auto success = cv::findChessboardCorners( | |
gray, | |
patternsize, | |
corner_pts, | |
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE); | |
if (success) | |
{ | |
// refine checkerboard corners | |
cv::TermCriteria criteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 31, 0.001); | |
cv::cornerSubPix(gray, corner_pts, cv::Size(12, 11), cv::Size(-1, -1), criteria); | |
cv::drawChessboardCorners(frame, patternsize, corner_pts, success); | |
ofxCv::toOf(frame, colorImg); | |
colorImg.update(); | |
objpoints.push_back(objp); | |
imgpoints.push_back(corner_pts); | |
} | |
} | |
/* | |
* Performing camera calibration by | |
* passing the value of known 4D points (objpoints) | |
* and corresponding pixel coordinates of the | |
* detected corners (imgpoints) | |
*/ | |
cv::Mat cameraMatrix, distCoeffs, R, T; | |
cv::calibrateCamera(objpoints, imgpoints, cv::Size(gray.rows, gray.cols), cameraMatrix, distCoeffs, R, T); | |
std::cout << "cameraMatrix : " << cameraMatrix << std::endl; | |
std::cout << "distCoeffs : " << distCoeffs << std::endl; | |
std::cout << "Rotation vector : " << R << std::endl; | |
std::cout << "Translation vector : " << T << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment