一.cv::Vec類
1.基本概念
cv::Vec類可以用來表示固定長度的向量,是一個模板類,常使用[]來訪問Vec向量成員,其主要用來儲存數值向量,除此之外,還有cv::Matx,它是cv::Vec類的基類,cv::Matx類可以用來處理固定大小的小維度矩陣運算,
2.用法
(1)cv::Vec類可以定義任意型別向量,如int型別,float型別等,其建構式的偽代碼如下:
cv::Vec<資料型別_Tp,int 變數個數cn> 向量名(x0,x1,....xcn-1);//cn的最大值為14
表示構造了一個存放cn個_Tp型變數的向量,示例如下:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
cv::Vec<int, 3> myvec;//定義了一個存放3個int型變數的向量
myvec[0] = 1;
myvec[1] = 1;
myvec[2] = 9;//給三個變數賦值
for (int i = 0; i < 3; i++)
cout << myvec[i] << " ";//輸出三個變數
}
或者也可以在定義時給變數賦值:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
cv::Vec<int, 3> myvec(1,1,9);//定義了一個存放3個int型變數的向量
for (int i = 0; i < 3; i++)
cout << myvec[i] << " ";//輸出三個變數
}
運行結果如下:

此外,cv::Vec類也可以使用以下預定義型別:
typedef cv::Vec<uchar, 2> cv::Vec2b
typedef cv::Vec<double, 2> cv::Vec2d
typedef cv::Vec<float, 2> cv::Vec2f
typedef cv::Vec<int, 2> cv::Vec2i
typedef cv::Vec<short, 2> cv::Vec2s
typedef cv::Vec<ushort, 2> cv::Vec2w
typedef cv::Vec<uchar, 3> cv::Vec3b
typedef cv::Vec<double, 3> cv::Vec3d
typedef cv::Vec<float, 3> cv::Vec3f
typedef cv::Vec<int, 3> cv::Vec3i
typedef cv::Vec<short, 3> cv::Vec3s
typedef cv::Vec<ushort, 3> cv::Vec3w
typedef cv::Vec<uchar, 4> cv::Vec4b
typedef cv::Vec<double, 4> cv::Vec4d
typedef cv::Vec<float, 4> cv::Vec4f
typedef cv::Vec<int, 4> cv::Vec4i
typedef cv::Vec<short, 4> cv::Vec4s
typedef cv::Vec<ushort, 4> cv::Vec4w
typedef cv::Vec<double, 6> cv::Vec6d
typedef cv::Vec<float, 6> cv::Vec6f
typedef cv::Vec<int, 6> cv::Vec6i
typedef cv::Vec<int, 8> cv::Vec8i
使用方法如下:
cv::Vec3f myvec(2.1,2.5,6.7);//定義了一個存放3個float型變數的向量
除此之外,cv::Vec類還可以通過拷貝的方式構造,
(2)Vec類的運算
通過運算子多載和自定義的函式,Vec類支持向量的點積,叉積,加,減,標量乘,邏輯判斷等運算:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
//定義與初始化
cv::Vec<int, 3> myvec(1, 2, 3);
cv::Vec3i v3i(5, 2, 0);
cv::Vec2d v2d(1.5, 3.2);
cv::Vec2d v2d_1(v2d);//通過拷貝構造
cout << "myvec[0]=" << myvec[0] << endl;
cout << "myvec[1]=" << myvec[1] << endl;
cout << "myvec[2]=" << myvec[2] << endl;
cout << "v3i[0]=" << v3i[0] << endl;
cout << "v3i[1]=" << v3i[1] << endl;
cout << "v3i[2]=" << v3i[2] << endl;
cout << "v2d[0]=" << v2d[0] << endl;
cout << "v2d[1]=" << v2d[1] << endl;
cout << "v2d_1[0]=" << v2d_1[0] << endl;
cout << "v2d_1[1]=" << v2d_1(1) << endl;//()與[]都可以訪問
//計算
cv::Vec3f v1(1, 3, 4);
cv::Vec3f v2(5, 2, 0);
cout << "v1=" << v1 << endl;
cout << "v2=" << v2 << endl;
cout << "v1.v2=" << v1.dot(v2) << endl;//點積
cout << "v1*v2=" << v1.cross(v2) << endl;//叉乘
cout << "v1+v2=" << v1 + v2 << endl;//加法
cout << "v1-v2=" << v1 - v2 << endl;//減法
cout << "v1*2=" << v1 * 2 << endl;//標量乘
cout << "(v1==v2)=" << (v1 == v2) << endl;//邏輯判斷
cout << "(v1!=v2)=" << (v1 != v2) << endl;
cout << "(v1+=v2)" << (v1 += v2) << endl;//加法
}
運行結果如下:

Vec類其他成員可以參考下列檔案:Vec類成員函式
二.cv::Point類
1.基本概念
cv::Point類(點類)是一個用來存放兩個或三個int或float等基本型別值的容器,其與固定向量類的主要區別在于其成員可以通過命名變數訪問,例如mypoint.x等,而不是像cv::Vec類一樣通過向量索引(myvec[0],myvec[1]),與cv::Vec類一樣,cv::Point類也可以使用一些預定義型別:
typedef cv::Point<int, 2> cv::Point2i
typedef cv::Point<double, 2> cv::Point2d
typedef cv::Point<float, 2> cv::Point2f
typedef cv::Point<int, 3> cv::Point3i
typedef cv::Point<double, 3> cv::Point3d
typedef cv::Point<float, 3> cv::Point3f
2.用法
(1)cv::Point類的一些構造方法與基本操作如下:
cv::Point2d p1;
cv::Point2d p2;//默認構造方式
cv::Point3f p3(2, 3, 4);//賦值構造
cv::Point3f p4(p3);//拷貝構造
(cv::Vec3f)p4;//將點類轉換為固定向量類
p3.x;
p3.y;
p3.z;//成員訪問
p1.inside(r)//判斷點p1是否在矩形r中(只對二維Point類可用)
(2)運算
與cv::Vec類一樣,cv::Point類也支持加減,點積,叉積等運算,其具體操作如下:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
cv::Point p1(1, 2);
cv::Point p2(3, 4);
cout << "p1=" <<"("<< p1.x << "," << p1.y << ")" << endl;
cout<<"p2=" << p2 << endl;
cv::Point3f p3(1.3, 1.4, 5.2);
cv::Point3f p4(p3);
cout << "p4.x=" << p4.x << endl;
cout << "p4.y=" << p4.y << endl;
cout << "p4.z=" << p4.z << endl;
//計算
cv::Point p = p1 + p2;
cout << "p1+p2=" << p << endl;
cout << "p1.p2=" << p1.dot(p2) << endl;//點乘
cout << "p3*p4=" << p3.cross(p4) << endl;//叉乘
cv::Rect rect(0, 0, 3, 3);//構造一個以(0,0)為頂點,寬和高都是3的矩形
cout << "p1.inside(rect)=" << p1.inside(rect) << endl;//判斷點p1是否在矩形rect中
}
運行結果如下:

cv::Point類中其他成員函式可參考下列檔案:cv::Point類成員函式
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/292812.html
標籤:其他
