/*------------------------------------------------------------------------------------------*\
This file contains material supporting chapter 9 of the cookbook:
Computer Vision Programming using the OpenCV Library.
by Robert Laganiere, Packt Publishing, 2011.
This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user.
Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*/
#include "CameraCalibrator.h"
// Open chessboard images and extract corner points
int CameraCalibrator::addChessboardPoints(
const std::vector<std::string>& filelist,
cv::Size & boardSize) {
// the points on the chessboard
std::vector<cv::Point2f> imageCorners;
std::vector<cv::Point3f> objectCorners;
// 3D Scene Points:
// Initialize the chessboard corners
// in the chessboard reference frame
// The corners are at 3D location (X,Y,Z)= (i,j,0)
for (int i=0; i<boardSize.height; i++) {
for (int j=0; j<boardSize.width; j++) {
objectCorners.push_back(cv::Point3f(i, j, 0.0f));
}
}
// 2D Image points:
cv::Mat image; // to contain chessboard image
int successes = 0;
// for all viewpoints
for (int i=0; i<filelist.size(); i++) {
// Open the image
image = cv::imread(filelist[i],0);
// Get the chessboard corners
bool found = cv::findChessboardCorners(
image, boardSize, imageCorners);
// Get subpixel accuracy on the corners
cv::cornerSubPix(image, imageCorners,
cv::Size(5,5),
cv::Size(-1,-1),
cv::TermCriteria(cv::TermCriteria::MAX_ITER +
cv::TermCriteria::EPS,
30, // max number of iterations
0.1)); // min accuracy
// If we have a good board, add it to our data
if (imageCorners.size() == boardSize.area()) {
// Add image and scene points from one view
addPoints(imageCorners, objectCorners);
successes++;
}
//Draw the corners
cv::drawChessboardCorners(image, boardSize, imageCorners, found);
cv::imshow("Corners on Chessboard", image);
cv::waitKey(100);
}
return successes;
}
// Add scene points and corresponding image points
void CameraCalibrator::addPoints(const std::vector<cv::Point2f>& imageCorners, const std::vector<cv::Point3f>& objectCorners) {
// 2D image points from one view
imagePoints.push_back(imageCorners);
// corresponding 3D scene points
objectPoints.push_back(objectCorners);
}
// Calibrate the camera
// returns the re-projection error
double CameraCalibrator::calibrate(cv::Size &imageSize)
{
// undistorter must be reinitialized
mustInitUndistort= true;
//Output rotations and translations
std::vector<cv::Mat> rvecs, tvecs;
// start calibration
return
calibrateCamera(objectPoints, // the 3D points
imagePoints, // the image points
imageSize, // image size
cameraMatrix, // output camera matrix
distCoeffs, // output distortion matrix
rvecs, tvecs, // Rs, Ts
flag); // set options
// ,CV_CALIB_USE_INTRINSIC_GUESS);
}
// remove distortion in an image (after calibration)
cv::Mat CameraCalibrator::remap(const cv::Mat &image) {
cv::Mat undistorted;
if (mustInitUndistort) { // called once per calibration
cv::initUndistortRectifyMap(
cameraMatrix, // computed camera matrix
distCoeffs, // computed distortion matrix
cv::Mat(), // optional rectification (none)
cv::Mat(), // camera matrix to generate undistorted
cv::Size(640,480),
// image.size(), // size of undistorted
CV_32FC1, // type of output map
map1, map2); // the x and y mapping functions
mustInitUndistort= false;
}
// Apply mapping functions
cv::remap(image, undistorted, map1, map2,
cv::INTER_LINEAR); // interpolation type
return undistorted;
}
// Set the calibration options
// 8radialCoeffEnabled should be true if 8 radial coefficients are required (5 is default)
// tangentialParamEnabled should be true if tangeantial distortion is present
void CameraCalibrator::setCalibrationFlag(bool radial8CoeffEnabled, bool tangentialParamEnabled) {
// Set the flag used in cv::calibrateCamera()
flag = 0;
if (!tangentialParamEnabled) flag += cv::CALIB_ZERO_TANGENT_DIST;
if (radial8CoeffEnabled) flag += cv::CALIB_RATIONAL_MODEL;
}
//! [compute_errors]
double CameraCalibrator::computeReprojectionErrors(const std::vector<std::vector<cv::Point3f> >& objectPoints,
const std::vector<std::vector<cv::Point2f> >& imagePoints,
const std::vector<cv::Mat>& rvecs, const std::vector<cv::Mat>& tvecs,
const cv::Mat& cameraMatrix, const cv::Mat& distCoeffs,
std::vector<float>& perViewErrors)
{
std::vector<cv::Point2f> imagePoints2;
size_t totalPoints = 0;
double totalErr = 0, err;
perViewErrors.resize(objectPoints.size());
for (size_t i = 0; i < objectPoints.size(); ++i)
{
cv::projectPoints(objectPoints[i], rvecs[i], tvecs[i], cameraMatrix, distCoeffs, imagePoints2);
err = cv::norm(imagePoints[i], imagePoints2, cv::NORM_L2);
size_t n = objectPoints[i].size();
perViewErrors[i] = (float)std::sqrt(err*err / n);
totalErr += err*err;
totalPoints += n;
}
return std::sqrt(totalErr / totalPoints);
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于OpenCV实现的相机标定C++程序
共46个文件
jpg:20个
tlog:6个
pdb:2个
需积分: 5 2 下载量 28 浏览量
2024-01-14
21:36:47
上传
评论
收藏 38.27MB ZIP 举报
温馨提示
基于Window系统下VS2017+OpenCV4.5.2开发实现的单目相机标定C++程序,含测试图片,标定模板图为9x6棋盘格图像。
资源推荐
资源详情
资源评论
收起资源包目录
cameraCalibration.zip (46个子文件)
cameraCalibration
.vs
cameraCalibration
v15
Browse.VC.db 13.55MB
.suo 36KB
ipch
AutoPCH
CAMERACALIBRATION-70bac754
CAMERACALIBRATOR-8a8cfe86
CAMERACALIBRATOR.ipch 68.56MB
CALIBRATE-b6354601
CALIBRATE.ipch 71.44MB
x64
Debug
cameraCalibration.exe 248KB
cameraCalibration.pdb 1.87MB
cameraCalibration.ilk 1.62MB
cameraCalibration.sln 1KB
cameraCalibration
cameraCalibration.vcxproj 7KB
CameraCalibrator.cpp 6KB
cameraCalibration.vcxproj.filters 1KB
CameraCalibrator.h 3KB
x64
Debug
vc141.pdb 1.18MB
CameraCalibrator.obj 995KB
vc141.idb 923KB
cameraCa.8D265DAC.tlog
CL.write.1.tlog 2KB
CL.command.1.tlog 1KB
link.command.1.tlog 2KB
cameraCalibration.lastbuildstate 207B
link.read.1.tlog 4KB
link.write.1.tlog 726B
CL.read.1.tlog 63KB
cameraCalibration.Build.CppClean.log 1KB
calibrate.obj 721KB
cameraCalibration.log 275B
calibrate.cpp 3KB
images
2.jpg 272KB
10.jpg 335KB
13.jpg 292KB
14.jpg 339KB
6.jpg 293KB
15.jpg 320KB
16.jpg 304KB
1.jpg 246KB
17.jpg 346KB
12.jpg 309KB
5.jpg 318KB
8.jpg 292KB
20.jpg 302KB
18.jpg 276KB
11.jpg 305KB
19.jpg 333KB
3.jpg 281KB
7.jpg 327KB
9.jpg 280KB
4.jpg 298KB
共 46 条
- 1
资源评论
诗眼天涯
- 粉丝: 30
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功