OpenCV中Point类和常见的vector容器类型使用方法

10 篇文章 1 订阅

一:Point类
Point是一个模板类,其模板类的定义如下

template<typename _Tp> class Point_
{
public:
    typedef _Tp value_type;
 
    //! default constructor
    Point_();
    Point_(_Tp _x, _Tp _y);
    Point_(const Point_& pt);
    Point_(const Size_<_Tp>& sz);
    Point_(const Vec<_Tp, 2>& v);
 
    Point_& operator = (const Point_& pt);
    //默认构造函数
    template<typename _Tp2> operator Point_<_Tp2>() const;
 
    //转换为旧式的C结构
    operator Vec<_Tp, 2>() const;
 
    //点积
    _Tp dot(const Point_& pt) const;
    //用双精度算术计算的点积
    double ddot(const Point_& pt) const;
    //叉积
    double cross(const Point_& pt) const;
    //检查点是否在指定矩形内
    bool inside(const Rect_<_Tp>& r) const;
    _Tp x; //点的x坐标
    _Tp y; //点的y坐标
};

在OpenCV中使用重命名进行了如下定义

typedef Point_<int> Point2i;     //int类型Point_
typedef Point_<int64> Point2l;   //int64类型Point_
typedef Point_<float> Point2f;   //flota类型Point_
typedef Point_<double> Point2d;   //double类型Point_
typedef Point2i Point;         //int类型Point,因为int类型使用相对多些,再次重取名,编 

下面结合一些例子进行理解

#pragma once
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<iostream>

using namespace std;
using namespace cv;


int main() {
	//使用默认构造函数
	Point2i p1;//[0,0]
	Point2l p2;//[0,0]
	Point2f p3;//[0,0]
	Point2d p4;//[0,0]
	//使用默认构造函数进行赋值操作
	Point2i p5(1,3);//[1, 3]
	Point2l p6(256,300);//[256, 300]
	Point2f p7(2.9,3.6);//[2.9, 3.6]
	Point2d p8(2.69,5.36);//[2.69, 5.36]

	Point3i p9(1, 2, 3);//[1, 2, 3]
	Point3f p10(1, 2, 3);//[1, 2, 3]
	Point3d p11(1, 2, 3);//[1, 2, 3]
	
	//进行隐式类型转换,转换为Vec3i
	Vec3i p12 = p9;
	cout << p12 << endl;//[1, 2, 3]

	//访问成员函数
	cout << p5.x << " " << p5.y << endl;//1 3
	cout << p9.x << " " << p9.y << " " << p9.z << endl;//1 2 3

	//点乘--计算向量之间的夹角
	Point3f f1(2.1, 2.2, 2.3);
	Point3f f2(4.1, 4.2, 4.3);
	float f1_f2;
	f1_f2 = f1.dot(f2);//2.1*4.1+2.2*4.2+2.3*4.3=27.74
	cout << f1_f2 << endl;
	
	//叉乘,计算三维点影像
	//结果是两个向量的垂直向量
	Point3i i1(1, 2, 3);
	Point3i i2(5, 6, 7);
	Point3i i1xi2;
	i1xi2 = i1.cross(i2);
	cout << i1xi2 << endl;//[-4, 8, -4],该向量与i1和i2分别相乘的结果均为0,表示该向量与另外两个向量分别垂直

	//判断一个点是否在矩阵内
	Rect2f rest(1, 1, 2, 2);
	Point2f f3(1.5, 1.5);
	bool flag = f3.inside(rest);
	cout << flag << endl;//true
	//改变一下点的位置
	f3.y = 6.2;
	flag = f3.inside(rest);
	cout << flag << endl;//false
	
	return 0;
}

下面说一说

这几种输出类型
vector<vector<Point>>
vector<Rect>
vector<Vec4i>
vector<RotateRect>

先把实验图像放上去
在这里插入图片描述
然后上代码,代码不是我敲的,我用的是opencv4,懒得敲了,借用了作者:Ahuuua链接https://blog.csdn.net/Ahuuua/article/details/80593388的代码,代码不难,之前我的一篇博客计算轮廓区域面积中都用到过,都是opencv自带的函数,不过里面通过for循环找轮廓点的地方值得大家看一下。

#include <opencv2/opencv.hpp> 
#include <iostream>  

using namespace cv;
using namespace std;

int demovector() {
	Mat src, gray_src, drawImg, bin_output;

	src = imread("apple.jpg");
	//namedWindow("input", CV_WINDOW_AUTOSIZE);
	//namedWindow("output", CV_WINDOW_AUTOSIZE);
	imshow("input", src);

	cvtColor(src, gray_src, COLOR_BGR2GRAY);
	blur(gray_src, gray_src, Size(10, 10), Point(-1, -1), BORDER_DEFAULT);

	//这些个类型
	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];//储存单个旋转矩形的四个点
	cout << "----------Point2f pst[4]------------" << endl;
	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);

	cout << "----------vector<vector<Point>> contours_poly------------" << endl;
	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;

	cout << "----------vector<Vec4i> hierarchy------------" << endl;
	for (size_t i = 0; i < hierarchy.size(); i++)
	{
		cout << hierarchy[i] << endl;
	}
	cout << endl;

	cout << "----------vector<Rect> poly_rects------------" << endl;
	for (size_t i = 0; i < poly_rects.size(); i++)
	{
		cout << poly_rects[i] << endl;
	}
	cout << endl;

	cout << "---------vector<RotatedRect> minRect------------" << 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;
}

下面是实验图像
在这里插入图片描述
最后面对应下面几种类型的输出格式

vector<vector<Point>>
vector<Vec4i>
vector<Rect>
vector<RotateRect>

在这里插入图片描述
好了,就这样吧。

  • 2
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是小峰呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值