How do I declare a vector in OpenCV?

18,756

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.

You can use cv::Vec type.

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
  • float type use Vec3f
  • int type use Vec3i
  • short type use Vec3s
  • ushort type use Vec3w
  • uchar type use Vec3b
Share:
18,756
ner
Author by

ner

Updated on June 05, 2022

Comments

  • ner
    ner almost 2 years

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

    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?

  • Pro_gram_mer
    Pro_gram_mer over 2 years
    what about CV_8SC3 ? should it be cv::Vec3b?
  • Miki
    Miki over 2 years
    @Pro_gram_mer as you can see in the source code there is no typedef for char. You can make your own, like: typedef Vec<char, 3> Vec3c;
  • Pro_gram_mer
    Pro_gram_mer over 2 years
    Yeah it works, what about CV_16FC3, CV_16FC1 how can I define this on my own ?
  • Miki
    Miki over 2 years
    @Pro_gram_mer The same, but instead of char put the type corresponding to float16 (which is not available in the C++ standard)