如何在OpenCV中声明向量?

时间:2021-03-09 20:57:26

How do you write a vector in OpenCV? that contains 3 values like this v = [p1,p2,p3]? This is what I tried:

你如何在OpenCV中编写一个向量?包含3个值,如v = [p1,p2,p3]?这是我试过的:

int dim [1] = {3};
Mat v(1,dim,CV_32F, Scalar(p1,p2,p3));

But when I do debug in Qt, I see in the local and expression window that the vector v indeed has 1 column and 3 rows but has also 2 dim. I was wondering if this is due to the Mat type in the declaration. With which type could I replace it to get just a simple vector of 3 values?

但是当我在Qt中进行调试时,我在本地和表达式窗口中看到向量v确实有1列和3行,但也有2个暗淡。我想知道这是否是由于声明中的Mat类型。我可以用哪种类型替换它来获得3个值的简单向量?

1 个解决方案

#1


3  

Which type could I use to get just a simple vector of 3 values? I want to do some operation on this vector like use the norm and change it's elements.

我可以使用哪种类型来获得3个值的简单向量?我想对这个向量做一些操作,比如使用norm并改变它的元素。

You can use cv::Vec type.

您可以使用cv :: Vec类型。

Assuming you're working on double values (the same applies for other types) you can:

假设您正在处理双值(同样适用于其他类型),您可以:

// Create a vector
Vec3d v;

// Assign values / Change elements
v[0] = 1.1;
v[1] = 2.2;
v[2] = 3.3;

// Or initialize in the constructor directly 
Vec3d u(1.1, 2.2, 3.3);

// Read values
double d0 = v[0];

// Compute the norm, using cv::norm 
double norm_L2 = norm(v, NORM_L2); // or norm(v);
double norm_L1 = norm(v, NORM_L1);

For:

对于:

  • double type use Vec3d
  • 双类型使用Vec3d
  • float type use Vec3f
  • float类型使用Vec3f
  • int type use Vec3i
  • int类型使用Vec3i
  • short type use Vec3s
  • 短型使用Vec3s
  • ushort type use Vec3w
  • ushort类型使用Vec3w
  • uchar type use Vec3b
  • uchar类型使用Vec3b

#1


3  

Which type could I use to get just a simple vector of 3 values? I want to do some operation on this vector like use the norm and change it's elements.

我可以使用哪种类型来获得3个值的简单向量?我想对这个向量做一些操作,比如使用norm并改变它的元素。

You can use cv::Vec type.

您可以使用cv :: Vec类型。

Assuming you're working on double values (the same applies for other types) you can:

假设您正在处理双值(同样适用于其他类型),您可以:

// Create a vector
Vec3d v;

// Assign values / Change elements
v[0] = 1.1;
v[1] = 2.2;
v[2] = 3.3;

// Or initialize in the constructor directly 
Vec3d u(1.1, 2.2, 3.3);

// Read values
double d0 = v[0];

// Compute the norm, using cv::norm 
double norm_L2 = norm(v, NORM_L2); // or norm(v);
double norm_L1 = norm(v, NORM_L1);

For:

对于:

  • double type use Vec3d
  • 双类型使用Vec3d
  • float type use Vec3f
  • float类型使用Vec3f
  • int type use Vec3i
  • int类型使用Vec3i
  • short type use Vec3s
  • 短型使用Vec3s
  • ushort type use Vec3w
  • ushort类型使用Vec3w
  • uchar type use Vec3b
  • uchar类型使用Vec3b