Size数据结构经常被OpenCV用来表示尺寸,其成员为width和height,被用来表示矩阵或者图片的宽和高。

Size

Size预定义个几个类型有:

Size2i:整型int

Size2l:int64

Size2f:float

Size2d:double

源代码如下:

最后都是使用的 Size_类

Size_类

Size_类和Point_,Scalar_类相似,相对Mat比较简单:

总体使用方法如下:

MethodDescription
默认构造函数

cv::Size sz;

cv::Size2i szi;

cv::Size2l szl;

cv::Size2f szf;

cv::Size2d szd;

copy构造函数

Size_(const Size_& sz)

cv::Size sz2(sz);

带参数构造函数

Size_(_Tp _width, _Tp _height)

_width:宽度

_height:高度

cv::Size sz3(4,6)

成员:

_Tp width; 

_Tp height

width:宽度

height:高度

area:计算面积(width*height)

 _Tp area()

返回值为(width*height)

 

aspectRatio:计算斜率(width/height)

double aspectRatio()

返回值为double :(width/height)(4.0以上版本新添加)

empty :是否为空

bool empty()

true: width <= 0或height <= 0

false:width>0 && height > 0

用例:

#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()
{
	
	Size sz;
	Size2i szi;
	Size2l szl;
	Size2f szf;
	Size2d szd;

	cout << "sz.empty=:" << sz.empty() << endl;
	cout << "szi.empty=:" << szi.empty() << endl;
	cout << "szl.empty=:" << szl.empty() << endl;

	Size sz2(200, 300);
	Size2i szi2(300, 300);
	cout << "sz2=:" << sz2 <<endl;
	cout << "szi2=:" << szi2 << endl;
	
	Size2l szl2(21432,124214);
	cout << "szl2=:" << szl2 << endl;

	Size2f szf2(32543.123,523412.12323);
	cout << "szf2=:" << szf2 << endl;

	Size2d szd2(1253298,21323213);
	cout << "szd2=:" << szd2 << endl;


	Size sz3(sz2);
	cout << "sz3=:" << sz3 << endl;
	cout << "sz3.area=:" << sz3.area() << endl;
	//cout << "sz3.aspectRatio=:" << sz3.aspectRatio() << endl;
	cout << "sz3.empty=:" << sz3.empty() << endl;

	Size2l szl3(szl2);
	cout << "szl3=:" << szl3 << endl;
	cout << "szl3.area=:" << szl3.area() << endl;
	cout << "szl3.empty=:" << szl3.empty() << endl;

	Size2f szf3(szf2);
	cout << "szf3=:" << szf3 << endl;
	cout << "szf3.area=:" << szf3.area() << endl;
	cout << "szf3.empty=:" << szf3.empty() << endl;

	Size2d szd3(szd2);
    cout << "szd3=:" << szd3 << endl;
	cout << "szd3.area=:" << szd3.area() << endl;
	cout << "szd3.empty=:" << szd3.empty() << endl;
	
}

运行结果:

 Size_类 operator重构

Size_类通过operator重构的操作有:

operatorMethod
=template<typename _Tp> inline
Size_<_Tp>& Size_<_Tp>::operator = (const Size_<_Tp>& sz)
template<typename _Tp> inline
Size_<_Tp>& Size_<_Tp>::operator = (Size_<_Tp>&& sz)
*=template<typename _Tp> static inline
Size_<_Tp>& operator *= (Size_<_Tp>& a, _Tp b)
*template<typename _Tp> static inline
Size_<_Tp> operator * (const Size_<_Tp>& a, _Tp b)
/=template<typename _Tp> static inline
Size_<_Tp>& operator /= (Size_<_Tp>& a, _Tp b)
/template<typename _Tp> static inline
Size_<_Tp> operator / (const Size_<_Tp>& a, _Tp b)
+=template<typename _Tp> static inline
Size_<_Tp>& operator += (Size_<_Tp>& a, const Size_<_Tp>& b)
+template<typename _Tp> static inline
Size_<_Tp> operator + (const Size_<_Tp>& a, const Size_<_Tp>& b)
-=template<typename _Tp> static inline
Size_<_Tp>& operator -= (Size_<_Tp>& a, const Size_<_Tp>& b)
-template<typename _Tp> static inline
Size_<_Tp> operator - (const Size_<_Tp>& a, const Size_<_Tp>& b)
==template<typename _Tp> static inline
bool operator == (const Size_<_Tp>& a, const Size_<_Tp>& b)
!=template<typename _Tp> static inline
bool operator != (const Size_<_Tp>& a, const Size_<_Tp>& b)

 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐