OpenCV的基本數據結構及示例
OpenCV中強大的Mat類型大家已經比較熟悉了。這裏梳理一些在工程中其他經常用到的幾種基本數據類型。包括:
Vec
Scalar
Point
Size
Rect
RotatedRect
1. Vec類
1.1 基本概念
Vec是一個模板類,主要用於存儲數值向量。
1.2 用法
(1)可用它來定義任意類型的向量
- Vec<double, 8> myVector; // 定義一個存放8個double型變量的向量
(2)使用[]訪問Vec向量成員
- myVector[0]=0;
(3)可使用以下預定義的類型
- typedef Vec<uchar, 2> Vec2b;
- typedef Vec<uchar, 3> Vec3b;
- typedef Vec<uchar, 4> Vec4b;
- typedef Vec<short, 2> Vec2s;
- typedef Vec<short, 3> Vec3s;
- typedef Vec<short, 4> Vec4s;
- typedef Vec<int, 2> Vec2i;
- typedef Vec<int, 3> Vec3i;
- typedef Vec<int, 4> Vec4i;
- typedef Vec<float, 2> Vec2f;
- typedef Vec<float, 3> Vec3f;
- typedef Vec<float, 4> Vec4f;
- typedef Vec<float, 6> Vec6f;
- typedef Vec<double, 2> Vec2d;
- typedef Vec<double, 3> Vec3d;
- typedef Vec<double, 4> Vec4d;
- typedef Vec<double, 6> Vec6d;
(4)Vec支持的運算如下:
- v1 = v2 + v3
- v1 = v2 - v3
- v1 = v2 * scale
- v1 = scale * v2
- v1 = -v2
- v1 += v2
- v1 == v2, v1 != v2
- norm(v1) (euclidean norm)
1.3 示例代碼
(1)向量定義與元素的訪問
- // Vec
- cv::Vec<double, 8> myVector;
- for(int i=0; i<myVector.rows;i++)
- myVector[i] = i;
- cout<<"myVector= "<<myVector<<endl;
- cout<<"myVector[0]= "<<myVector[0]<<endl;
- cout<<"myVector[3]= "<<myVector[3]<<endl;
(2)基本運算
- cv::Vec<int, 6> v1,v2,v3;
- for(int i=0; i<v2.rows;i++){ //v2.rows返回向量v2的行數
- v2[i] = i;
- v3[i] = i+1;
- }
- v1 = v2 + v3;
- cout<<"v2 = "<<v2<<endl;
- cout<<"v3 = "<<v3<<endl;
- cout<<"v1=v2+v3= "<<v1<<endl;
- cout<<"v1=v2*2 = "<<v2*2<<endl;
- cout<<"v1=-v2 = "<<-v2<<endl;
- cout<<"v1==v2 = "<<(v1==v2)<<endl;
- cout<<"v1!=v2 = "<<(v1!=v2)<<endl;
- cout<<"norm(v2)= "<<norm(v2)<<endl;
運行結果:
2. Scalar類
2.1 基本概念
Scalar是一個從Vec類引出的模板類,是一個可存放4個元素的向量,廣泛用於傳遞和讀取圖像中的像素值。
2.2 用法
可使用[]訪問Scalar值。或使用如下方式定義BGR三個通道的值。
- cv:: Scalar( B, G, R )
2.3 示例代碼
(1)cv::Scalar結構
- cv::Scalar myScalar;
- myScalar = cv::Scalar(0,255,0);
- cout<<"myScalar = "<<myScalar<<endl;
- system("pause");
運行結果:
(2)讀取彩色圖像像素值
彩色圖像的每個像素對應三個部分:RGB三個通道。因此包含彩色圖像的cv::Mat類會返回一個向量,向量中包含三個8位的數值。OpenCV爲這樣的短向量定義了一種類型,即我們上述的cv::Vec3b。這個向量包含三個無符號字符(unsigned character)類型的數據。
OpenCV存儲通道次序爲:藍色、綠色、紅色即BGR。因此,訪問彩色像素中元素的方法如下:
- cv::Mat pImg = cv::imread("Lena.jpg",1);
- if(!pImg.data)
- return 0;
- int x = 100, y = 100;
- cv::Scalar pixel=pImg.at<Vec3b>(x,y);
- cout<<"B chanel of pixel is = "<<pixel.val[0]<<endl;
- cout<<"G chanel of pixel is = "<<pixel.val[1]<<endl;
- cout<<"R chanel of pixel is = "<<pixel.val[2]<<endl;
- system("pause");
運行結果:
3. Point類
3.1 基本概念
常用於表示2維座標(x,y)。
3.2 用法
(1)圖像座標
對圖像而言,我們可以這樣定義:
- cv::Point pt;
- pt.x = 10;
- pt.y = 8;
- cv::Point pt = Point(10, 8);
或者
- cv::Point pt(10,8);
(2)或使用如下預定義:
- typedef Point_<int> Point2i;
- typedef Point2i Point;
- typedef Point_<float> Point2f;
- typedef Point_<double> Point2d;
(3)基本運算
- pt1 = pt2 + pt3;
- pt1 = pt2 - pt3;
- pt1 = pt2 * a;
- pt1 = a * pt2;
- pt1 += pt2;
- pt1 -= pt2;
- pt1 *= a;
- double value = norm(pt); // L2 norm
- pt1 == pt2;
- pt1 != pt2;
3.3 示例代碼
(1)設置座標點
- // Point
- cv::Point pt;
- pt.x = 278;
- pt.y = 269;
- //或者
- //cv::Point pt (278,269);
- cv::Scalar pix = pImg.at<Vec3b>(pt);
- cout<<"pix("<<pt.x<<","<<pt.y<<") = "<<pix<<endl;
運行結果:
(2)各類運算
- cv::Point pt1(10,20);
- cv::Point pt2(2,3);
- cout<<"pt1 = "<<pt1<<endl;
- cout<<"pt2 = "<<pt2<<endl;
- cout<<"pt1+pt2 = "<<pt1+pt2<<endl;
- cout<<"pt1+=pt2= "<<(pt1+=pt2)<<endl;
- cout<<"pt1-pt2 = "<<pt1-pt2<<endl;
- cout<<"pt2*2 = "<<pt2*2<<endl;
運行結果:
4. Size類
4.1 基本概念
模板類Size可表示一幅圖像或一個矩形的大小。它包含寬、高2個成員:width , height還有一個有用的面積函數area()。
4.2 用法
- cv::Size size(int w, int h);
- //或者
- cv::Size size;
- size.width = w;
- size.height = h;
4.3 示例代碼
- // Size
- cv::Size size1(6,3);
- cv::Size size2;
- size2.width = 4;
- size2.height = 2;
- cv::Mat mat1(size1,CV_8UC1,cv::Scalar(0));
- cv::Mat mat2(size2,CV_8UC3,cv::Scalar(1,2,3));
- cout<<"mat1 = "<<endl<<mat1<<endl;
- cout<<endl<<"mat2 = "<<endl<<mat2<<endl;
- system("pause");
運行結果:
5. Rect類
5.1 基本概念
Rect是另一個用於定義2維矩形的模板類。它由兩個參數定義:
- 矩形左上角座標: (x,y)
- 矩形的寬和高: width, height
Rect可以用來定義圖像的ROI區域。
5.2 用法
- cv::Rect rect(x, y, width, height);
5.3 示例代碼
- // Rect
- cv::Mat pImg = imread("Lena.jpg",1);
- cv::Rect rect(180,200,200,200);//(x,y)=(180,200),w=200,height=200
- cv::Mat roi = cv::Mat(pImg, rect);
- cv::Mat pImgRect = pImg.clone();
- cv::rectangle(pImgRect,rect,cv::Scalar(0,255,0),2);
- cv::imshow("original image with rectangle",pImgRect);
- cv::imshow("roi",roi);
- cv::waitKey();
6. RotatedRect類
6.1 基本概念
最後一個基本數據類是一種特殊的矩形稱爲RotatedRect。這個類通過中心點,寬度和高度和旋轉角度來表示一個旋轉的矩形。
6.2 用法
旋轉矩形類的構造函數:
- RotatedRect(const Point2f& center, const Size2f& size, float angle);
參數:
- center:中心點座標Point2f類型
- size:矩形的寬度和高度,Size2f類型
- angle:順時針方向的旋轉角度(單位°),float類型
6.3 示例代碼
- //RotatedRect
- cv::Point2f center(100,100);
- cv::Size2f size(100,50);
- float angle = 45;// try 10, 30, 45
- RotatedRect rRect(center, size, angle);
- cv::Mat image(200,200,CV_8UC3,cv::Scalar(0));
- Point2f vertices[4];
- rRect.points(vertices);
- for (int i = 0; i < 4; i++)
- line(image, vertices[i], vertices[(i+1)%4], Scalar(0,255,0));
- Rect brect = rRect.boundingRect();
- rectangle(image, brect, Scalar(255,0,0));
- imshow("rectangles", image);
- waitKey(0);
angle = 10, 30, 45
//////////////////////////////////////////////////////////////////
轉載:https://blog.csdn.net/iracer/article/details/51292349