OpenCV中基本数据结构(1)_Point

本文详细介绍了OpenCV中Point数据结构的使用,包括Point2和Point3的不同变种,如Point2i、Point2f、Point3d等,以及它们支持的操作如=、dot、cross、inside等。同时,文章提供了C++代码示例,展示了如何在实际应用中使用这些数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了便于对一些常见的数据进行操作,OpenCV定义了一些常见的数据结构(Point ,Scalar等),以方便后续对数据算法的实现,主要分为basic data type、helper objects、large array types等三种数据结构类型,其中basic data type是最常见使用频率最高的数据结构,主要包括一些简单的向量和矩阵等等,主要用于局部变量中,此类型最典型的是数据都是比较小,主要是提供一些方便操作。

Point class

point是OpenCV中最常见数据结构,支持整型、浮点型等数据类型,经常用来表示矩阵中某个点,是OpenCV中最简单的数据结构,按照维度主要分为二维(Point2)和三维(Point3)两个类型,可以再由该数据类型组成固定数量的vector。

Point2产生的变种:为Point2i、Point2l、Point2f、Point2d,其中i代表int,l代表int64, f代表float,d代表double,其实现如下:

Point3变种为:Point3i、Point3f、Point3d,其实现如下:

由上述二维和三维点表示可知 Point2和Point3分别为Point_和 Point3_类,上述两个类相对来说比较简单,支持常用的表达式:

Point_类

Point_类定义如下:

支持=,dot,cross,inside等操作。

Point3_类

Point3_类如下:

与Point2_所支持的操作类型一样

总结

 Point3和Point2直接支持的操作是一样的,表格如下:

OperationDescription
两者提前定义的数据类型

二维:Point2i、Point2l、Point2f、Point2d

三维:Point3i、Point3f、Point3d,其他类型需要开发者自己定义

两者支持的默认构造函数

比如:

二维:cv::Point2i a;

          cv::Point2l b;

          cv::Point2f c;

          cv::Point2d d;

三维:cv::Point3i a;

          cv::Point3f b;

          cv::Point3d c;

数据copy构造函数

二维: cv::Point2i e(d);

 

三维:cv::Point3i d(a);

赋值重构

二维:cv::Point2i g(2,3);

三维:cv:Point3i g(2,3,4);

直接强转成vec

二维:Point2i 相当于cv::Vec2i

          Point2f 相当于cv::Vec2f

三维:Point3i 相当于cv::Vec3i

          Point3f 相当于cv::Vec3f

两则成员

二维:a.x 和a.y分别代码二维情况下的x和y index

三维:a.x,a.y和az分别代码三维情况下的x,y和z

dot

两者都支持dot:

二维:

Point2f a

Point2f b

float x= a.dot(b)

三维:

Point3f a;

Point3f b

float x= a.dot(b)

ddot double精度,其返回值为double

二维:

Point2f a

Point2f b

double x= a.dot(b)

三维:

Point3f a;

Point3f b

double x= a.dot(b)

cross

二维:

二维返回值为double值

三维:

Point3f a;

Point3f b

Point3f x= a.cross(b)

inside:查找b 是否在a的范围之内

只有二维支持:

Point2f a

Rect为矩形框表示方式,后面在Rect部分详细介绍

a.inside(Rect(1,2,3,4));

Point2用例:

#include <stdio.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;


void main()
{
	Point2i int_a;
	Point2l int64_b;
	Point2f float_c;
	Point2d double_d;

	Point2i int_a2(int_a);
	Point2l int64_b2(int64_b);
	Point2f float_c2(float_c);
	Point2d double_d2(double_d);

	int_a2.x = 1;
	int_a2.y = 2;
	cout << "int_a:" << int_a << endl;
	cout << "int_a2:" << int_a2 << endl;

	int64_b2.x = 23;
	int64_b2.y = 123456;
	cout << "int64_b:" << int64_b << endl;
	cout << "int64_b2:" << int64_b2 << endl;

	float_c2.x = 1.23;
	float_c2.y = 5.78;
	cout << "float_c:" << float_c << endl;
	cout << "float_c2:" << float_c2 << endl;

	double_d2.x = 12876;
	double_d2.y = 3787.324;
	cout << "double_d:" << double_d << endl;
	cout << "double_d2:" << double_d2 << endl;

	Point2i int_a3(7,8);
	Point2l int64_b3(234247, 32438);
	Point2f float_c3(23.34,465.8979);
	Point2d double_d3(246435, 463235.892379);
	cout << "int_a3:" << int_a3 << endl;
	cout << "int64_b3:" << int64_b3 << endl;
	cout << "float_c3:" << float_c3 << endl;
	cout << "double_d3:" << double_d3 << endl;

	cout << "int_a2.dot(int_a3):" << int_a2.dot(int_a3) << endl;
	cout << "int64_b2.dot(int64_b3):" << int64_b2.dot(int64_b3) << endl;
	cout << "float_c2.dot(float_c3):" << float_c2.dot(float_c3) << endl;
	cout << "double_d2.dot(double_d3):" << double_d2.dot(double_d2) << endl;

	cout << "int_a2.ddot(int_a3):" << int_a2.ddot(int_a3) << endl;
	cout << "int64_b2.ddot(int64_b3):" << int64_b2.ddot(int64_b3) << endl;
	cout << "float_c2.ddot(float_c3):" << float_c2.ddot(float_c3) << endl;
	cout << "double_d2.ddot(double_d3):" << double_d2.ddot(double_d2) << endl;

	cout << "int_a2.cross(int_a3):" << int_a2.cross(int_a3) << endl;
	cout << "int64_b2.cross(int64_b3):" << int64_b2.cross(int64_b3) << endl;
	cout << "float_c2.cross(float_c3):" << float_c2.cross(float_c3) << endl;
	cout << "double_d2.cross(double_d3):" << double_d2.cross(double_d2) << endl;
	
	cout << "int_a2.inside(int_a3):" << int_a2.inside(Rect(2,3,3,3)) << endl;
	cout << "int64_b2.inside(int64_b3):" << int64_b2.inside(Rect(2, 3, 3, 3)) << endl;
	cout << "float_c2.inside(float_c3):" << float_c2.inside(Rect(2, 3, 3, 3)) << endl;
	cout << "double_d2.inside(double_d3):" << double_d2.inside(Rect(2, 3, 3, 3)) << endl;

}

运行结果:

Point3用例:

#include <stdio.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;


void main()
{
	Point3i int_a;
	Point3f float_c;
	Point3d double_d;

	Point3i int_a2(int_a);
	Point3f float_c2(float_c);
	Point3d double_d2(double_d);

	int_a2.x = 1;
	int_a2.y = 2;
	int_a2.z = 3;
	cout << "int_a:" << int_a << endl;
	cout << "int_a2:" << int_a2 << endl;

	float_c2.x = 1.23;
	float_c2.y = 5.78;
	float_c2.z = 45.2344;
	cout << "float_c:" << float_c << endl;
	cout << "float_c2:" << float_c2 << endl;

	double_d2.x = 12876;
	double_d2.y = 3787.324;
	double_d2.z = 242;
	cout << "double_d:" << double_d << endl;
	cout << "double_d2:" << double_d2 << endl;

	Point3i int_a3(7,8,10);
	Point3f float_c3(23.34,465.8979,127.21);
	Point3d double_d3(246435, 463235.892379,21979.12424123);
	cout << "int_a3:" << int_a3 << endl;
	cout << "float_c3:" << float_c3 << endl;
	cout << "double_d3:" << double_d3 << endl;

	cout << "int_a2.dot(int_a3):" << int_a2.dot(int_a3) << endl;
	cout << "float_c2.dot(float_c3):" << float_c2.dot(float_c3) << endl;
	cout << "double_d2.dot(double_d3):" << double_d2.dot(double_d2) << endl;

	cout << "int_a2.ddot(int_a3):" << int_a2.ddot(int_a3) << endl;
	cout << "float_c2.ddot(float_c3):" << float_c2.ddot(float_c3) << endl;
	cout << "double_d2.ddot(double_d3):" << double_d2.ddot(double_d2) << endl;

	cout << "int_a2.cross(int_a3):" << int_a2.cross(int_a3) << endl;
	cout << "float_c2.cross(float_c3):" << float_c2.cross(float_c3) << endl;
	cout << "double_d2.cross(double_d3):" << double_d2.cross(double_d2) << endl;
}

运行结果:

operator重载

Point_和Point3_类通过C++ operator重载来实现该类的加减乘除操作。

Point_类operator重载

Point2实现通过operator重载实现表达式如下,其实现在modules\core\include\opencv2\core\types.hpp文件中:

operatorMethod
=template<typename _Tp> inline
Point_<_Tp>& Point_<_Tp>::operator = (const Point_& pt)
template<typename _Tp> inline
Point_<_Tp>& Point_<_Tp>::operator = (Point_&& pt)
Vec与Point之间转换template<typename _Tp> inline
Point_<_Tp>::operator Vec<_Tp, 2>
+=template<typename _Tp> static inline
Point_<_Tp>& operator += (Point_<_Tp>& a, const Point_<_Tp>& b)
-=template<typename _Tp> static inline
Point_<_Tp>& operator -= (Point_<_Tp>& a, const Point_<_Tp>& b)
*=template<typename _Tp> static inline
Point_<_Tp>& operator *= (Point_<_Tp>& a, int b)
template<typename _Tp> static inline
Point_<_Tp>& operator *= (Point_<_Tp>& a, double b)
template<typename _Tp> static inline
Point_<_Tp>& operator *= (Point_<_Tp>& a, float b)
/=template<typename _Tp> static inline
Point_<_Tp>& operator /= (Point_<_Tp>& a, int b)
template<typename _Tp> static inline
Point_<_Tp>& operator /= (Point_<_Tp>& a, float b)
template<typename _Tp> static inline
Point_<_Tp>& operator /= (Point_<_Tp>& a, double b)
norm:sqrt((double)pt.x*pt.x + (double)pt.y*pt.y)template<typename _Tp> static inline
double norm(const Point_<_Tp>& pt)
==template<typename _Tp> static inline
bool operator == (const Point_<_Tp>& a, const Point_<_Tp>& b)
!=template<typename _Tp> static inline
bool operator != (const Point_<_Tp>& a, const Point_<_Tp>& b)
+template<typename _Tp> static inline
Point_<_Tp> operator + (const Point_<_Tp>& a, const Point_<_Tp>& b)
-template<typename _Tp> static inline
Point_<_Tp> operator - (const Point_<_Tp>& a, const Point_<_Tp>& b)
template<typename _Tp> static inline
Point_<_Tp> operator - (const Point_<_Tp>& a)
*template<typename _Tp> static inline
Point_<_Tp> operator * (const Point_<_Tp>& a, int b)
*template<typename _Tp> static inline
Point_<_Tp> operator * (int a, const Point_<_Tp>& b)
template<typename _Tp> static inline
Point_<_Tp> operator * (const Point_<_Tp>& a, float b)
template<typename _Tp> static inline
Point_<_Tp> operator * (float a, const Point_<_Tp>& b)
template<typename _Tp> static inline
Point_<_Tp> operator * (const Point_<_Tp>& a, double b)
template<typename _Tp> static inline
Point_<_Tp> operator * (double a, const Point_<_Tp>& b)
template<typename _Tp> static inline
Point_<_Tp> operator * (const Matx<_Tp, 2, 2>& a, const Point_<_Tp>& b)
template<typename _Tp> static inline
Point3_<_Tp> operator * (const Matx<_Tp, 3, 3>& a, const Point_<_Tp>& b)
/template<typename _Tp> static inline
Point_<_Tp> operator / (const Point_<_Tp>& a, int b)
template<typename _Tp> static inline
Point_<_Tp> operator / (const Point_<_Tp>& a, float b)
template<typename _Tp> static inline
Point_<_Tp> operator / (const Point_<_Tp>& a, double b)

 

 Point3_类operator重载

Point3_类支持的重载:

operatorMethod
 =template<typename _Tp> inline
Point3_<_Tp>& Point3_<_Tp>::operator = (const Point3_& pt)
template<typename _Tp> inline
Point3_<_Tp>& Point3_<_Tp>::operator = (Point3_&& pt)
vec与Point转换template<typename _Tp> inline
Point3_<_Tp>::operator Vec<_Tp, 3>()
+=template<typename _Tp> static inline
Point3_<_Tp>& operator += (Point3_<_Tp>& a, const Point3_<_Tp>& b)
-=template<typename _Tp> static inline
Point3_<_Tp>& operator -= (Point3_<_Tp>& a, const Point3_<_Tp>& b)
*=template<typename _Tp> static inline
Point3_<_Tp>& operator *= (Point3_<_Tp>& a, int b)
template<typename _Tp> static inline
Point3_<_Tp>& operator *= (Point3_<_Tp>& a, float b)
template<typename _Tp> static inline
Point3_<_Tp>& operator *= (Point3_<_Tp>& a, double b)
/=template<typename _Tp> static inline
Point3_<_Tp>& operator /= (Point3_<_Tp>& a, int b)
template<typename _Tp> static inline
Point3_<_Tp>& operator /= (Point3_<_Tp>& a, float b)
template<typename _Tp> static inline
Point3_<_Tp>& operator /= (Point3_<_Tp>& a, double b)

norm:

sqrt((double)pt.x*pt.x + (double)pt.y*pt.y + (double)pt.z*pt.z)

template<typename _Tp> static inline
double norm(const Point3_<_Tp>& pt)
==template<typename _Tp> static inline
bool operator == (const Point3_<_Tp>& a, const Point3_<_Tp>& b)
!=template<typename _Tp> static inline
bool operator != (const Point3_<_Tp>& a, const Point3_<_Tp>& b)
+template<typename _Tp> static inline
Point3_<_Tp> operator + (const Point3_<_Tp>& a, const Point3_<_Tp>& b)
-template<typename _Tp> static inline
Point3_<_Tp> operator - (const Point3_<_Tp>& a, const Point3_<_Tp>& b)
template<typename _Tp> static inline
Point3_<_Tp> operator - (const Point3_<_Tp>& a)
*template<typename _Tp> static inline
Point3_<_Tp> operator * (const Point3_<_Tp>& a, int b)
template<typename _Tp> static inline
Point3_<_Tp> operator * (int a, const Point3_<_Tp>& b)
template<typename _Tp> static inline
Point3_<_Tp> operator * (const Point3_<_Tp>& a, float b)
template<typename _Tp> static inline
Point3_<_Tp> operator * (float a, const Point3_<_Tp>& b)
template<typename _Tp> static inline
Point3_<_Tp> operator * (const Point3_<_Tp>& a, double b)
template<typename _Tp> static inline
Point3_<_Tp> operator * (double a, const Point3_<_Tp>& b)
template<typename _Tp> static inline
Point3_<_Tp> operator * (const Matx<_Tp, 3, 3>& a, const Point3_<_Tp>& b)
template<typename _Tp> static inline
Matx<_Tp, 4, 1> operator * (const Matx<_Tp, 4, 4>& a, const Point3_<_Tp>& b)
/template<typename _Tp> static inline
Point3_<_Tp> operator / (const Point3_<_Tp>& a, int b)
template<typename _Tp> static inline
Point3_<_Tp> operator / (const Point3_<_Tp>& a, float b)
template<typename _Tp> static inline
Point3_<_Tp> operator / (const Point3_<_Tp>& a, double b)

下面为Point3一个简单的例子:

#include <stdio.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;


void main()
{
	Point3i int_a(4,5,6);
	

	Point3i int_a2(int_a);
	

	int_a2.x = 1;
	int_a2.y = 2;
	int_a2.z = 3;
	cout << "int_a=:" << int_a << endl;
	cout << "int_a2=:" << int_a2 << endl;
	
	cout << "int_a2- int_a=:" << (int_a2- int_a) << endl;
	cout << "int_a2+ int_a=:" << (int_a2 + int_a) << endl;
	cout << "int_a2*2=:" << int_a2 *2 << endl;
	cout << "int_a2/2=:" << int_a2 /2 << endl;
	cout << "(int_a2==int_a)=:" << (int_a2 ==int_a) << endl;
	
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Huo的藏经阁

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

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

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

打赏作者

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

抵扣说明:

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

余额充值