在c++中imgPoints.push_back(corners)的push_back是什意思?
时间: 2024-04-27 21:23:50 浏览: 16
在 C++ 中,`push_back()` 是向容器尾部添加元素的成员函数。在这里,`imgPoints` 很可能是一个容器(如 `std::vector`),`corners` 是一个元素,`push_back(corners)` 表示将 `corners` 添加到 `imgPoints` 容器的尾部。换句话说,`imgPoints` 是一个包含多个元素的容器,`push_back()` 函数用于将元素添加到容器的末尾,实现动态扩容。这样做可以方便地管理容器中的元素,同时还可以进行快速的遍历和访问。
相关问题
在c++中vector<Mat> rvecs, tvecs是什么意思?如何查看?完整代码
相机标定是一个比较复杂的过程,需要先读入标定图片,然后提取图片中的特征点,接着进行相机标定,最后输出标定结果。下面是一个完整的相机标定示例代码,包括如何定义和查看`rvecs`和`tvecs`。
```c++
#include <iostream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
// 读入标定图片
vector<string> images;
string folder = "path/to/folder";
for (int i = 1; i <= 10; i++)
{
string filename = folder + "/" + to_string(i) + ".jpg";
images.push_back(filename);
}
// 定义标定板角点
Size boardSize(6, 9);
vector<vector<Point3f>> objectPoints;
vector<vector<Point2f>> imagePoints;
vector<Point3f> obj;
for (int i = 0; i < boardSize.height; i++)
{
for (int j = 0; j < boardSize.width; j++)
{
obj.push_back(Point3f(j, i, 0));
}
}
// 提取角点
Mat image, gray;
vector<Point2f> corners;
bool found;
for (int i = 0; i < images.size(); i++)
{
image = imread(images[i]);
cvtColor(image, gray, COLOR_BGR2GRAY);
found = findChessboardCorners(gray, boardSize, corners,
CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE);
if (found)
{
cornerSubPix(gray, corners, Size(11, 11), Size(-1, -1),
TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.1));
drawChessboardCorners(image, boardSize, corners, found);
imshow("Image", image);
waitKey(100);
imagePoints.push_back(corners);
objectPoints.push_back(obj);
}
}
// 标定相机
Mat cameraMatrix, distCoeffs;
vector<Mat> rvecs, tvecs;
calibrateCamera(objectPoints, imagePoints, gray.size(), cameraMatrix, distCoeffs, rvecs, tvecs);
// 输出标定结果
cout << "Camera matrix:\n" << cameraMatrix << endl;
cout << "Distortion coefficients:\n" << distCoeffs << endl;
for (int i = 0; i < images.size(); i++)
{
cout << "rvecs[" << i << "]:\n" << rvecs[i] << endl;
cout << "tvecs[" << i << "]:\n" << tvecs[i] << endl;
}
return 0;
}
```
上述代码中,我们首先定义了一个`vector<Mat> rvecs`和`vector<Mat> tvecs`,分别用于存储每幅图像的旋转向量和平移向量。在标定完成后,我们使用循环遍历每幅图像,输出其对应的旋转向量和平移向量。
内参标定中理想点 怎么求出畸变点 C++
在相机标定中,我们通过拍摄多张不同姿态下的棋盘格图像,来获得相机的内参和畸变参数。其中,内参包括相机的焦距、主点等,畸变参数则是为了补偿图像中的畸变而引入的。
在畸变参数的求解过程中,我们需要找到理想点和畸变点。理想点是指在没有畸变的情况下,棋盘格上每个角点对应的在相机坐标系下的坐标值。而畸变点则是指由于相机镜头等因素引起的畸变而出现的实际角点坐标值。
下面是一个求解畸变点的示例代码(使用OpenCV库):
```cpp
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
// 定义棋盘格的宽高
int board_width = 9;
int board_height = 6;
// 生成理想点坐标
std::vector<Point3f> obj_points;
for (int i = 0; i < board_height; i++)
{
for (int j = 0; j < board_width; j++)
{
obj_points.push_back(Point3f(j, i, 0));
}
}
// 读取多张棋盘格图像
std::vector<std::vector<Point2f>> img_points;
std::vector<std::string> img_names = {"img1.jpg", "img2.jpg", "img3.jpg"};
for (const auto& img_name : img_names)
{
Mat img = imread(img_name);
cvtColor(img, img, COLOR_BGR2GRAY);
std::vector<Point2f> corners;
bool found = findChessboardCorners(img, Size(board_width, board_height), corners);
if (found)
{
// 计算畸变点坐标
cornerSubPix(img, corners, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::MAX_ITER, 30, 0.1));
img_points.push_back(corners);
}
}
// 求解相机的内参和畸变参数
Mat camera_matrix, dist_coeffs;
std::vector<Mat> rvecs, tvecs;
calibrateCamera(obj_points, img_points, Size(640, 480), camera_matrix, dist_coeffs, rvecs, tvecs);
// 输出结果
std::cout << "camera_matrix: \n" << camera_matrix << std::endl;
std::cout << "dist_coeffs: \n" << dist_coeffs << std::endl;
return 0;
}
```
在上述代码中,我们首先定义了理想点坐标,然后通过 `findChessboardCorners()` 函数找到每张图像中的角点坐标值,再通过 `cornerSubPix()` 函数对角点坐标进行亚像素级别的精确化,最后通过 `calibrateCamera()` 函数求解相机的内参和畸变参数。在输出结果中,`camera_matrix` 就是相机的内参矩阵,`dist_coeffs` 就是相机的畸变参数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)