OpenCV数据类的对象
1、Point类
cv::Point point = Point(10, 5);
cout << point.x << endl;
cout << point.y << endl;
cv::Point2f point_2f;
point_2f.x = 1.25;
point_2f.y = 1.45;
cout << point_2f.x << endl;
cout << point_2f.y << endl;
2、Scalar类,是一种四维点类,可以输入0~4个参数
cv::Scalar s(255,255,255);
cout << s[0] << endl;
cout << s[1] << endl;
cout << s[2] << endl;
2、Size类
cv::Size sz(5, 10);
cout << sz.width << endl;
cout << sz.height << endl;
//计算面积
cout << sz.area() << endl;
3、Rect类
矩形类包含Point类的成员x和y(矩形左上角)和size类的成员width和height(代表矩形的大小)
构造函数初始化:
cv::Rect r(1,3,10,10);
起始点和大小构造:
Point p(1, 3);
Size s(10, 10);
Rect r(p, s);
通过对角点构造:
Point p1(2, 2);
Point p2(5, 5);
Rect r(p1, p2);
//计算面积
cout << r.area() << endl;
//提取左上角
cout << r.tl() << endl;
//提取右下角
cout << r.br() << endl;
4、RotetedRect类
旋转矩形,包含了中心点Point2f、大小Size2f、旋转角度float。其中浮点类型float的角度代表图形绕中心点旋转的角度
cv::Point2f p(100, 100);//中心点
cv::Size2f sz(30, 30);//尺寸
float theta = 30;//角度旋转
cv::RotatedRect r(p, sz, theta);
cout << r.angle << r.size << r.center << endl;
//重要的成员访问是访问四点坐标,可用来画出矩形
cv::Point2f vertices[4];
r.points(vertices);
cv::Mat src_img = cv::imread("image.jpg");
line(src_img, (cv::Point)vertices[0], (cv::Point)vertices[1], cv::Scalar(255,255,255));
line(src_img, (cv::Point)vertices[1], (cv::Point)vertices[2], cv::Scalar(255, 255, 255));
line(src_img, (cv::Point)vertices[2], (cv::Point)vertices[3], cv::Scalar(255, 255, 255));
line(src_img, (cv::Point)vertices[3], (cv::Point)vertices[0], cv::Scalar(255, 255, 255));
cv::imshow("src_img", src_img);
cv::waitKey(0);