[]: https://docs.opencv.org/3.4.5/dc/d84/group__core__basic.html
1.Point
首先介绍2维点对Point_,它的是一个模板类。我们可以直接访问数据成员x,y。它不仅定了+、-、==、!=这4个基本的操作,还定义了点乘、叉乘等操作。特别的这个类还提供了inside函数来判断一个点是否在矩形区域内。此外,还定义了一些其他的类型转化函数,比如转化为1.X版本的CvPoint。
为了方便使用,opencv又对常用的类型进行了定义:
1
2
3
4
5typedef Point_<int> Point2i; typedef Point2i Point; typedef Point_<float> Point2f; typedef Point_<double> Point2d;
同理还有Point3_,只不过它是一个3维点(x,y,z)而已。它的常用类型是:
1
2
3
4typedef Point3_<int> Point3i; typedef Point3_<float> Point3f; typedef Point3_<double> Point3d;
2.Vec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18typedef 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;
轮廓数据类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main() { Mat src, gray_src, drawImg, bin_output; src = imread("1.jpg"); cvtColor(src, gray_src, CV_BGR2GRAY); blur(gray_src, gray_src, Size(10, 10), Point(-1, -1), BORDER_DEFAULT); //contours和 hierarchy数据类型 vector<vector<Point>> contours; vector<Vec4i> hierarchy; threshold(gray_src, bin_output, 144, 255, 0); findContours(bin_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0)); //这些个类型 vector<vector<Point>> contours_poly(contours.size()); vector<Rect> poly_rects(contours.size()); vector<RotatedRect> minRect(contours.size()); //取点 for (size_t i = 0; i < contours.size(); i++) { approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true); //减少轮廓点数 poly_rects[i]=boundingRect(contours_poly[i]);//获取绘制矩形数据 if (contours_poly[i].size() > 5) { minRect[i] = minAreaRect(contours_poly[i]);//获取绘制旋转矩形数据 } } //开始绘制 src.copyTo(drawImg); Point2f pst[4];//储存单个旋转矩形的四个点 for (size_t i = 0; i < contours.size(); i++) { rectangle(drawImg, poly_rects[i], Scalar(255, 0, 0), 2, 8);//绘制矩形框 minRect[i].points(pst);//用线段画矩形,将RotatedRect类型转化为四个点 for (size_t u = 0; u < 4; u++) { line(drawImg, pst[u], pst[(u + 1) % 4], Scalar(0, 255, 0), 2, 8); cout << pst[u]; //显示pst的数据 } cout << endl; Rect brect = minRect[i].boundingRect(); //返回包含旋转矩形的最小矩形 rectangle(drawImg, brect,Scalar(0, 0, 255)); } cout << endl; imshow("output", drawImg); for (size_t i = 0; i < contours_poly.size(); i++) { cout << "第" << i << "行:"; for (size_t j = 0; j < contours_poly[i].size(); j++) { cout<<contours_poly[i][j]; } cout << endl; } cout << endl; for (size_t i = 0; i < hierarchy.size(); i++) { cout << hierarchy[i] << endl; } cout << endl; for (size_t i = 0; i < poly_rects.size(); i++) { cout << poly_rects[i]<<endl; } cout << endl; for (size_t i = 0; i <minRect.size(); i++) //显示一下点minRect { cout <<"angle:"<< minRect[i].angle<<" center:"<< minRect[i].center<<" size:"<< minRect[i].size<<endl; } cout << endl; waitKey(0); return 0; }
3.Scalar
Scalar_类其实是用Vec<tp,4>派生下来的,也就是说,它是一个4元组:typedef Scalar_ Scalar;
他通常用来传递像素。
Scalar(a, b, c):省略透明通道,由于OpenCV的彩色影像通常为BGR的顺序,a代表蓝色、b代表绿色、c代表红色。
Scalar(a):通常用于灰度图,像素强度为a
4.Size
模板类Size可表示一幅图像或一个矩形的大小。它包含宽、高2个成员:width , height还有一个有用的面积函数area()
1
2
3
4
5
6cv::Size size(int w, int h); //或者 cv::Size size; size.width = w; size.height = h;
1
2
3
4
5
6typedef Size_<int> Size2i; typedef Size_<int64> Size2l; typedef Size_<float> Size2f; typedef Size_<double> Size2d; typedef Size2i Size;
1
2
3
4
5
6
7Size(int width, int height) Size size1(150, 100); Size size2; size2.width = 150; size2.height = 100; int myArea = size2.area();
5.Rect
Rect是另一个用于定义2维矩形的模板类。它由两个参数定义:
- 矩形左上角坐标: (x,y)
- 矩形的宽和高: width, height
Rect可以用来定义图像的ROI区域。
1
2
3
4
5typedef Rect_<int> Rect2i; typedef Rect_<float> Rect2f; typedef Rect_<double> Rect2d; typedef Rect2i Rect;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16Mat img = imread("test.jpg", 1);//矩形类的表示 Rect rect1(2,5,10,10); cout << rect1 << endl; //输出的是 [10 × 10 from (2,5)] cout << rect1.area() << endl; //输出的是100 cout << rect1.tl() << "t" << rect1.br() << endl; //输出的是[2,5],[12,15] cout << rect1.size() << endl; //输出的是 [10 × 10] Point point1(3,7),point2(0,0); cout << rect1.contains(point1) << endl; //返回的是1 cout << rect1.contains(point2) << endl; //返回的是0
tl() 和 br() 分别表示矩形的左上角和右下角的坐标值
contains(Point) 判断点是是否在矩形内部;rect.contains(point),如果在,返回1,如果不在,返回0
6.RotatedRect
- 矩形中心点(质心) (point2f center)
- 边长(长和宽) (Size2f size)
- 旋转角度(float angle)
1
2
3
4
5
6
7
8
9
10
11
12
13
14Mat test_image(200, 200, CV_8UC3, Scalar(0)); RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30); Point2f vertices[4]; rRect.points(vertices); for (int i = 0; i < 4; i++) { line(test_image, vertices[i], vertices[(i+1)%4], Scalar(0,255,0), 3); line(test_image, vertices[i], Point2f(100,100), Scalar(0,0,255), 1); } Rect brect = rRect.boundingRect(); rectangle(test_image, brect, Scalar(255,0,0), 3); imshow("rectangles", test_image); waitKey(0);
最后
以上就是动人项链最近收集整理的关于opencv数据结构1.Point2.Vec3.Scalar4.Size5.Rect6.RotatedRect的全部内容,更多相关opencv数据结构1内容请搜索靠谱客的其他文章。
发表评论 取消回复