C++ は 1 次元配列と 2 次元配列を使用して std::vector<cv::Point2d> に値を割り当てます。

1. 1次元配列をベクトルに代入する方法

(1) 最も単純な代入方法は for ループのトラバーサル代入ですが、ここでは省略します。

(2) 配列をベクトルに直接代入する別の方法もあります。

int arr[4] = {
    
    1, 2, 3, 4};
vector<int> v(arr, arr + 4); 

(3) また、STL で提供されているgenerate()メソッドを利用することもできますが、(2)ほど便利ではないのでここでは割愛します。 C ++のベクトル

2. 1 次元の Point2d 配列を Vector<cv::Point2d> に代入します。

1 次元配列を Vectorcv::Point2d に割り当てるのは非常に簡単です。

#include <opencv2\opencv.hpp>
#include <vector>

int main() {
    
    
	cv::Point2d input[3] = {
    
     cv::Point2d(0,0),cv::Point2d(1,1),cv::Point2d(2,2) };
	std::vector<cv::Point2d> output(input, input + 3);
	std::cout << "End!" << std::endl;    // End!
	return 0;
}

3. 2 次元 double 配列を Vector<cv::Point2d> に代入します。

なお、vectorcv::Point2d への 2 次元配列の代入には注意が必要で、上記の 1 次元配列でベクトルに値を代入する方法を依然として参照するとエラーが報告されます。 :

#include <opencv2\opencv.hpp>
#include <vector>

int main() {
    
    
	double input[3][2] = {
    
     {
    
    0,0},{
    
    1,1},{
    
    2,2} };
	std::vector<cv::Point2d> output(input, input + 3);
	std::cout << "End!" << std::endl;
	return 0;
}

/*
报错:
error C2664: “cv::Point_<double>::Point_(const cv::Point_<double> &)”: 
无法将参数 1 从“double [2]”转换为“const cv::Vec<double,2> &”
*/

エラー:

ここに画像の説明を挿入
そして、この種のエラーレポートはエラーの場所を指摘しないため、エラーを見つけるのは非常に困難です。

エラー メッセージには、「引数 1 を "double [2]" から "const cv::Vec<double,2> &" に変換できません」と表示されます。これは、実際には、2 つの 1 次元配列 (double [2]) が変換されていることを意味します。 -次元配列は変換できません cv::Point2d 型に変換します。

したがって、この場合、最も愚かなトラバーサル割り当てのみを使用できます。コードは次のとおりです。

#include <opencv2\opencv.hpp>
#include <vector>

int main() {
    
    
	double input[3][2] = {
    
     {
    
    0,0},{
    
    1,1},{
    
    2,2} };
	std::vector<cv::Point2d> output(3);
	for (int i = 0; i != 3; i++) {
    
    
		output[i].x = input[i][0];
		output[i].y = input[i][1];
	}
	std::cout << "End!" << std::endl;   // End!
	return 0;
}

おすすめ

転載: blog.csdn.net/qq_43799400/article/details/131111352