vector<vector<int>> imageMatrix(rows, vector<int>(cols));
时间: 2024-06-02 18:03:40 浏览: 16
vector<vector<int>> imageMatrix(rows, vector<int>(cols)); 是一个二维矩阵的定义和初始化方式,其中rows表示矩阵的行数,cols表示矩阵的列数。这个矩阵的元素类型是int。
具体来说,vector<vector<int>> 是一个二维向量,每个元素都是一个int类型的向量。通过使用这种方式,我们可以方便地表示和操作二维矩阵。
在上述代码中,imageMatrix 是一个二维矩阵对象,通过调用vector的构造函数,我们创建了一个具有rows行和cols列的二维矩阵。每个元素都被初始化为int类型的默认值0。
如果需要访问或修改矩阵中的元素,可以使用下标运算符[]来进行操作。例如,imageMatrix[i][j] 表示矩阵中第i行第j列的元素。
相关问题
opencvc++进行四个摄像头标定,分别得到并保存每个相机的的内参,外参,以及畸变系数的C++代码
以下是使用OpenCV C++进行四个摄像头标定的代码示例:
```c++
#include <opencv2/opencv.hpp>
#include <opencv2/calib3d.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
const int chess_rows = 9; // 棋盘格行数
const int chess_cols = 6; // 棋盘格列数
int main(int argc, char** argv) {
vector<vector<Point3f>> object_points; // 棋盘格上的三维点
vector<vector<Point2f>> image_points; // 棋盘格上的二维点
vector<Mat> rvecs, tvecs; // 旋转向量和平移向量
vector<float> reproj_errs; // 重投影误差
vector<Point2f> corners; // 棋盘格角点
Size img_size; // 图像尺寸
// 读取图像并查找棋盘格角点
for (int i = 0; i < 4; i++) {
// 读取图像
Mat img = imread("img" + to_string(i) + ".jpg");
if (img.empty()) {
cout << "Failed to read image " << i << endl;
return -1;
}
// 查找棋盘格角点
bool found = findChessboardCorners(img, Size(chess_cols, chess_rows), corners);
if (!found) {
cout << "Failed to find corners in image " << i << endl;
return -1;
}
// 亚像素精确化
cvtColor(img, img, COLOR_BGR2GRAY);
cornerSubPix(img, corners, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.1));
// 显示角点
drawChessboardCorners(img, Size(chess_cols, chess_rows), corners, found);
imshow("corners" + to_string(i), img);
waitKey(0);
// 存储角点
img_size = img.size();
vector<Point3f> obj(chess_rows * chess_cols);
for (int r = 0; r < chess_rows; r++) {
for (int c = 0; c < chess_cols; c++) {
obj[r * chess_cols + c] = Point3f(c * 25.0f, r * 25.0f, 0.0f);
}
}
object_points.push_back(obj);
image_points.push_back(corners);
}
// 标定相机
Mat camera_matrix = Mat::eye(3, 3, CV_64F);
Mat dist_coeffs = Mat::zeros(5, 1, CV_64F);
vector<vector<Point3f>> object_points_vec(4, object_points);
vector<vector<Point2f>> image_points_vec(4, image_points);
double rms = calibrateCameraRO(object_points_vec, image_points_vec, img_size, 0, camera_matrix, dist_coeffs, rvecs, tvecs);
// 输出标定结果
cout << "Camera matrix:" << endl;
cout << camera_matrix << endl;
cout << "Distortion coefficients:" << endl;
cout << dist_coeffs << endl;
for (int i = 0; i < 4; i++) {
cout << "Rotation vector for camera " << i << ":" << endl;
cout << rvecs[i] << endl;
cout << "Translation vector for camera " << i << ":" << endl;
cout << tvecs[i] << endl;
}
// 保存标定结果
FileStorage fs("calibration.xml", FileStorage::WRITE);
fs << "camera_matrix" << camera_matrix;
fs << "dist_coeffs" << dist_coeffs;
for (int i = 0; i < 4; i++) {
fs << "rvecs" + to_string(i) << rvecs[i];
fs << "tvecs" + to_string(i) << tvecs[i];
}
fs.release();
return 0;
}
```
说明:
1. 使用`findChessboardCorners()`函数查找棋盘格角点,并使用`cornerSubPix()`函数对角点进行亚像素精确化;
2. 使用`calibrateCameraRO()`函数对相机进行标定,并输出标定结果;
3. 使用`FileStorage`类保存标定结果到XML文件中。
注意:
1. 在实际使用中,需要根据自己的相机和棋盘格大小修改代码中的参数;
2. 代码中使用了四个相机,如果只有一个相机,可以将循环体中的代码删除,并将`object_points`和`image_points`变量改为单个变量即可。
opencv4.6.0版本进行四路相机标定,并将四路相机的的标定结果内参、外参、畸变系数分别保存为yaml文件的C++代码
以下是使用OpenCV 4.6.0进行四路相机标定并保存标定结果的C++代码,其中假设四路相机的图片已经存储在了vector<vector<Mat>> images中,images[i][j]表示第i路相机的第j张图片:
```c++
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
// 相机参数
const int num_camera = 4;
const int num_board_cols = 7;
const int num_board_rows = 10;
const int num_board_corners = num_board_cols * num_board_rows;
const Size board_size = Size(num_board_cols, num_board_rows);
const float square_size = 0.02f; // 单位:米
// 读取图片
vector<vector<Mat>> images(num_camera);
for (int i = 0; i < num_camera; i++)
{
for (int j = 0; j < num_board_corners; j++)
{
string filename = "camera" + to_string(i) + "/img" + to_string(j) + ".png";
Mat img = imread(filename);
images[i].push_back(img);
}
}
// 检测角点
vector<vector<Point2f>> corners(num_camera);
for (int i = 0; i < num_camera; i++)
{
for (int j = 0; j < num_board_corners; j++)
{
Mat gray;
cvtColor(images[i][j], gray, COLOR_BGR2GRAY);
vector<Point2f> corner;
bool found = findChessboardCorners(gray, board_size, corner);
if (found)
{
cornerSubPix(gray, corner, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.1));
corners[i].push_back(corner);
}
else
{
cout << "Cannot find chessboard corners in image " << j << " of camera " << i << endl;
}
}
}
// 检查角点是否符合要求
bool is_corners_valid = true;
for (int i = 0; i < num_camera; i++)
{
if (corners[i].size() != num_board_corners)
{
is_corners_valid = false;
break;
}
}
if (!is_corners_valid)
{
cout << "Error: the number of corners detected is not correct." << endl;
return -1;
}
// 标定
vector<vector<Point3f>> object_points(1, vector<Point3f>(num_board_corners));
for (int i = 0; i < num_board_rows; i++)
{
for (int j = 0; j < num_board_cols; j++)
{
object_points[0][i * num_board_cols + j] = Point3f(j * square_size, i * square_size, 0);
}
}
vector<Mat> camera_matrices(num_camera);
vector<Mat> dist_coeffs(num_camera);
vector<vector<Mat>> rvecs(num_camera), tvecs(num_camera);
double rms = calibrateCamera(object_points, corners, images[0][0].size(), camera_matrices, dist_coeffs, rvecs, tvecs);
cout << "RMS error: " << rms << endl;
// 保存相机矩阵、畸变系数、旋转矩阵、平移向量
for (int i = 0; i < num_camera; i++)
{
string filename = "camera" + to_string(i) + ".yaml";
FileStorage fs(filename, FileStorage::WRITE);
fs << "camera_matrix" << camera_matrices[i];
fs << "dist_coeffs" << dist_coeffs[i];
fs << "rvecs" << rvecs[i];
fs << "tvecs" << tvecs[i];
fs.release();
}
return 0;
}
```
在上面的代码中,我们首先读取四路相机的图片,并使用`findChessboardCorners`函数检测角点。如果检测到了角点,则使用`cornerSubPix`函数对角点进行亚像素级别的精细化处理。然后,我们使用`calibrateCamera`函数进行标定,并将结果保存到每个相机的yaml文件中。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)