此博客仅记录重建系统所遇到的bug以及解决方案
推荐两个学习openCV的地址:
https://learnopencv.com/read-display-and-write-an-image-using-opencv/
https://docs.opencv.org/4.2.0/db/da9/tutorial_aruco_board_detection.html
标定相机内参
标定相机内参我使用的是张正友标定法,使用棋盘格做的这件事。
code:
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
// Defining the dimensions of checkerboard
// 定义棋盘格的尺寸
int CHECKERBOARD[2]{ 6,9 };
int main()
{
// Creating vector to store vectors of 3D points for each checkerboard image
// 创建矢量以存储每个棋盘图像的三维点矢量
std::vector<std::vector<cv::Point3f> > objpoints;
// Creating vector to store vectors of 2D points for each checkerboard image
// 创建矢量以存储每个棋盘图像的二维点矢量
std::vector<std::vector<cv::Point2f> > imgpoints;
// Defining the world coordinates for 3D points
// 为三维点定义世界坐标系
std::vector<cv::Point3f> objp;
for (int i{ 0 }; i < CHECKERBOARD[1]; i++)
{
for (int j{ 0 }; j < CHECKERBOARD[0]; j++)
{
objp.push_back(cv::Point3f(j, i, 0));
}
}
// Extracting path of individual image stored in a given directory
// 提取存储在给定目录中的单个图像的路径
std::vector<cv::String> images;
// Path of the folder containing checkerboard images
// 包含棋盘图像的文件夹的路径
std::string path = "C:\\Users\\admin\\Desktop\\new\\*.jpg";
// 使用glob函数读取所有图像的路径
cv::glob(path, images);
cv::Mat frame, gray;
// vector to store the pixel coordinates of detected checker board corners
// 存储检测到的棋盘转角像素坐标的矢量
std::vector<cv::Point2f> corner_pts;
bool success;
// Looping over all the images in the directory
// 循环读取图像
for (int i{ 0 }; i < images.size(); i++)
{
frame = cv::imread(images[i]);
if (frame.empty())
{
continue;
}
if (i == 40)
{
int b = 1;
}
cout << "the current image is " << i << "th" << endl;
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
// Finding checker board corners
// 寻找角点
// If desired number of corners are found in the image then success = true
// 如果在图像中找到所需数量的角,则success = true
// opencv4以下版本,flag参数为CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE
success = cv::findChessboardCorners(gray, cv::Size(CHECKERBOARD[0], CHECKERBOARD[1]), corner_pts, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FAST_CHECK | CALIB_CB_NORMALIZE_IMAGE);
/*
* If desired number of corner are detected,
* we refine the pixel coordinates and display
* them on the images of checker board
*/
// 如果检测到所需数量的角点,我们将细化像素坐标并将其显示在棋盘图像上
if (success)
{
// 如果是OpenCV4以下版本,第一个参数为CV_TERMCRIT_EPS | CV_TERMCRIT_ITER
cv::TermCriteria criteria(TermCriteria::EPS | TermCriteria::Type::MAX_ITER, 30, 0.001);
// refining pixel coordinates for given 2d points.
// 为给定的二维点细化像素坐标
cv::cornerSubPix(gray, corner_pts, cv::Size(11, 11), cv::Size(-1, -1), criteria);
// Displaying the detected corner points on the checker board
// 在棋盘上显示检测到的角点
cv::drawChessboardCorners(frame, cv::Size(CHECKERBOARD[0], CHECKERBOARD[1]), corner_pts, success);
objpoints.push_back(objp);
imgpoints.push_back(corner_pts);
}
//cv::imshow("Image", frame);
//cv::waitKey(0);
}
cv::destroyAllWindows();
cv::Mat cameraMatrix, distCoeffs, R, T;
/*
* Performing camera calibration by
* passing the value of known 3D points (objpoints)
* and corresponding pixel coordinates of the
* detected corners (imgpoints)
*/
// 通过传递已知3D点(objpoints)的值和检测到的角点(imgpoints)的相应像素坐标来执行相机校准
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;
// rvecs
std::cout << "Rotation vector : " << R << std::endl;
// tvecs
std::cout << "Translation vector : " << T << std::endl;
return 0;
}
可能出现的bug:
- 在传输数据的时候,刚开始我为了方便直接用TIM将图片传到电脑,但是再运行时会报错。后来我用微信又重新传输看数据,且用的是原图,报错就没有了。
- 苹果手机好像会自动对焦,这其实会影响我们计算出来的相机内参,所以要解决这个问题。幸好我的手机是安卓的,相机有个专业模式,可以关闭自动对焦、自动调整白平衡等一系列自动化开关,使得所有变量都被控制住再去拍标定板和ARuco码,此时计算到的内参就比较准确了,再用这个内参结合ARuco去计算外参,得到的结果就相当准确了。
标定相机外参
标定相机外参使用的是ARuco码。
code:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <opencv2/aruco.hpp>
#include <stdio.h>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
//内参与畸变矩阵,笔者在前面的博客已经给出求解方法,有需要的可以找找看看
double fx, fy, cx, cy, k1, k2, k3, p1, p2;
fx = 3013.206512710496;
fy = 3005.582040592197;
cx = 1741.93788331738;
cy = 1527.67403998261;
k1 = -0.0203782300259507;
k2 = 0.158652454888744;
k3 = 0.001073970551565643;
p1 = 0.01699165994017346;
p2 = -0.173041141209321;
Mat cameraMatrix = (cv::Mat_<float>(3, 3) <<
fx, 0.0, cx,
0.0, fy, cy,
0.0, 0.0, 1.0);
Mat distCoeffs = (cv::Mat_<float>(5, 1) << k1, k2, p1, p2, k3);
cv::Mat image, imageCopy;
imageCopy = cv::imread("C:\\Users\\admin\\Desktop\\ARuco\\mark.jpg");
cv::Ptr<cv::aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
while (1) {
std::vector<int> ids;
std::vector<std::vector<cv::Point2f>> corners;
cv::aruco::detectMarkers(imageCopy, dictionary, corners, ids);//检测靶标
// if at least one marker detected
if (ids.size() > 0) {
cv::aruco::drawDetectedMarkers(imageCopy, corners, ids);//绘制检测到的靶标的框
std::vector<cv::Vec3d> rvecs, tvecs;
cv::aruco::estimatePoseSingleMarkers(corners, 0.055, cameraMatrix, distCoeffs, rvecs, tvecs);//求解旋转矩阵rvecs和平移矩阵tvecs
cout<<"R :"<<rvecs[0]<<endl;
cout << "T :" << tvecs[0] << endl;
// draw axis for each marker
for (int i = 0; i < ids.size(); i++)
cv::aruco::drawAxis(imageCopy, cameraMatrix, distCoeffs, rvecs[i], tvecs[i], 0.1);
}
//cv::imshow("out", imageCopy);
cv::imwrite("res.jpg", imageCopy);
// cv::waitKey(50);
//if (key == 27)1
break;
}
return 0;
}
标定相机外参需要注意的点,在上面讲到了。需要将相机的自动对焦等功能关闭,使用专业模式拍摄棋盘格图片&ARuco图片。再结合计算出的相机内参,计算出对应的相机外参。