c++计算vector<vector<string>> Point;行数和列数
时间: 2023-05-22 15:01:37 浏览: 58
这个问题可以通过以下代码来计算:
int rows = Point.size();
int cols = Point[0].size();
其中,rows 表示 Point 的行数,cols 表示 Point 的列数。
相关问题
在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`,分别用于存储每幅图像的旋转向量和平移向量。在标定完成后,我们使用循环遍历每幅图像,输出其对应的旋转向量和平移向量。
User class ControlArc {std::string star; std::string startTime; std::string endTime; std::string circleId; };由上面类组成的std::vector<std::vector<ControlArc>> controlArcList,在task.h文件的plan类中已经通过读外部json实例化,遗传算法每个染色体由4个基因,每个基因都是controlArcList 随机产生一定个数的vector<ControlArc>组成的数组,适应度函数是每条染色体上(endTime-startTime)值最小,请给出完整的c++遗传算法代码,其中要写.h文件和.cpp文件
下面是遗传算法的C++实现,包括.h文件和.cpp文件,代码中使用了遗传算法的常用基本操作,包括初始化种群、选择、交叉、变异等。其中,适应度函数为计算每个染色体上(endTime-startTime)值的最小值。
task.h:
```c++
#ifndef TASK_H
#define TASK_H
#include <vector>
#include <string>
class ControlArc {
public:
std::string star;
std::string startTime;
std::string endTime;
std::string circleId;
};
class Plan {
public:
std::vector<std::vector<ControlArc>> controlArcList;
Plan() {}
Plan(std::vector<std::vector<ControlArc>> controlArcList) {
this->controlArcList = controlArcList;
}
};
#endif // TASK_H
```
ga.h:
```c++
#ifndef GA_H
#define GA_H
#include <vector>
#include "task.h"
class GA {
public:
GA(int popSize, int maxGen, double pc, double pm, int geneSize, int arcNum, std::vector<std::vector<ControlArc>> controlArcList);
void run();
double fitness(Plan& p);
private:
int popSize; // 种群规模
int maxGen; // 最大迭代次数
double pc; // 交叉概率
double pm; // 变异概率
int geneSize; // 染色体长度
int arcNum; // vector<ControlArc> 数组中元素的个数
std::vector<std::vector<ControlArc>> controlArcList; // 控制弧列表
std::vector<Plan> pop; // 种群
Plan bestIndiv; // 最优个体
double bestFitness; // 最优适应度
Plan decode(std::vector<std::vector<ControlArc>> gene);
std::vector<std::vector<ControlArc>> encode(Plan indiv);
void initPop();
void select(std::vector<Plan>& newPop);
void crossover(Plan& p1, Plan& p2);
void mutate(Plan& indiv);
};
#endif // GA_H
```
ga.cpp:
```c++
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include "ga.h"
GA::GA(int popSize, int maxGen, double pc, double pm, int geneSize, int arcNum, std::vector<std::vector<ControlArc>> controlArcList) {
this->popSize = popSize;
this->maxGen = maxGen;
this->pc = pc;
this->pm = pm;
this->geneSize = geneSize;
this->arcNum = arcNum;
this->controlArcList = controlArcList;
this->bestFitness = 9999999.0;
}
void GA::run() {
srand((unsigned int)time(NULL)); // 随机数生成器初始化
initPop(); // 初始化种群
for (int i = 0; i < maxGen; i++) { // 迭代maxGen次
std::vector<Plan> newPop; // 新种群
select(newPop); // 选择
for (int j = 0; j < popSize / 2; j++) { // 交叉
Plan& p1 = newPop[j * 2];
Plan& p2 = newPop[j * 2 + 1];
crossover(p1, p2);
}
for (int j = 0; j < popSize; j++) { // 变异
mutate(newPop[j]);
}
pop = newPop;
for (int j = 0; j < popSize; j++) { // 更新最优个体和最优适应度
double fit = fitness(pop[j]);
if (fit < bestFitness) {
bestFitness = fit;
bestIndiv = pop[j];
}
}
std::cout << "第" << i + 1 << "代,最优适应度:" << bestFitness << std::endl;
}
}
double GA::fitness(Plan& p) {
std::vector<std::vector<ControlArc>> gene = encode(p);
double minEndTime = 9999999.0;
for (int i = 0; i < gene.size(); i++) {
std::vector<ControlArc>& arcs = gene[i];
double endTime = 0.0;
for (int j = 0; j < arcs.size(); j++) {
double t = atof(arcs[j].endTime.c_str()) - atof(arcs[j].startTime.c_str());
if (t > 0) { // 时间差小于0的不计算
endTime += t;
}
}
if (endTime < minEndTime) {
minEndTime = endTime;
}
}
return minEndTime;
}
Plan GA::decode(std::vector<std::vector<ControlArc>> gene) {
return Plan(gene);
}
std::vector<std::vector<ControlArc>> GA::encode(Plan indiv) {
return indiv.controlArcList;
}
void GA::initPop() {
for (int i = 0; i < popSize; i++) {
std::vector<std::vector<ControlArc>> gene(geneSize, std::vector<ControlArc>(arcNum));
for (int j = 0; j < geneSize; j++) {
for (int k = 0; k < arcNum; k++) {
gene[j][k] = controlArcList[rand() % controlArcList.size()][rand() % controlArcList[0].size()];
}
}
pop.push_back(decode(gene));
}
bestIndiv = pop[0];
bestFitness = fitness(bestIndiv);
}
void GA::select(std::vector<Plan>& newPop) {
double sumFitness = 0.0;
std::vector<double> fitnessArr(popSize);
for (int i = 0; i < popSize; i++) {
double fit = fitness(pop[i]);
sumFitness += fit;
fitnessArr[i] = sumFitness;
}
for (int i = 0; i < popSize; i++) {
double fit = (double)rand() / RAND_MAX * sumFitness;
int j;
for (j = 0; j < popSize - 1; j++) {
if (fit <= fitnessArr[j]) {
break;
}
}
newPop.push_back(pop[j]);
}
}
void GA::crossover(Plan& p1, Plan& p2) {
if ((double)rand() / RAND_MAX < pc) { // 满足概率要求
int point1 = rand() % geneSize;
int point2 = rand() % geneSize;
if (point1 > point2) { // 确保point1小于point2
int temp = point1;
point1 = point2;
point2 = temp;
}
std::vector<std::vector<ControlArc>> gene1 = encode(p1);
std::vector<std::vector<ControlArc>> gene2 = encode(p2);
for (int i = point1; i <= point2; i++) { // 交叉基因
std::vector<ControlArc> temp = gene1[i];
gene1[i] = gene2[i];
gene2[i] = temp;
}
p1 = decode(gene1);
p2 = decode(gene2);
}
}
void GA::mutate(Plan& indiv) {
if ((double)rand() / RAND_MAX < pm) { // 满足概率要求
std::vector<std::vector<ControlArc>> gene = encode(indiv);
int point = rand() % geneSize;
for (int i = 0; i < arcNum; i++) { // 变异基因
gene[point][i] = controlArcList[rand() % controlArcList.size()][rand() % controlArcList[0].size()];
}
indiv = decode(gene);
}
}
```
使用方法:
```c++
#include <vector>
#include <fstream>
#include "ga.h"
#include "task.h"
using namespace std;
int main()
{
int popSize = 50; // 种群规模
int maxGen = 100; // 最大迭代次数
double pc = 0.8; // 交叉概率
double pm = 0.2; // 变异概率
int geneSize = 4; // 染色体长度
int arcNum = 10; // vector<ControlArc> 数组中元素的个数
// 读取控制弧列表
ifstream fin("controlArcList.json");
nlohmann::json j;
fin >> j;
vector<vector<ControlArc>> controlArcList = j.get<vector<vector<ControlArc>>>();
GA ga(popSize, maxGen, pc, pm, geneSize, arcNum, controlArcList);
ga.run();
return 0;
}
```