OpenCV的常用类:Scalar、Vec、Point、Size、Rect、RotatedRect、Ptr

一、颜色:Scalar类

Scalar类用来设定像素的通道值

1.三个值

Scalar(int blue,int green,int red)

每个值的范围是[0,255],负数就是0,超过255就是255。

如:

Scalar color=Scalar(193,0,0);
//对应rgb=(0,0,193),蓝色

2.全部相同all()

Scalar::all(value)

如:

Scalar color=Scalar::all(255);
//纯白

PS:0

Scalar()即0

3.作用

(1)用作颜色

#include<opencv2/opencv.hpp>
using namespace cv;

int main()
{
	Mat dstImage=Mat::zeros(300,400,CV_8UC3);
	line(dstImage,Point(100,200),Point(250,100),Scalar(255,102,0));
	imshow("dstImage",dstImage);
	waitKey();
	return 0;
}

在这里插入图片描述

(2)用作图像

image=Scalar(100,200,150);

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;

int main()
{
	//全白的图像
	Mat image=Mat::ones(300,400,CV_8UC3);
	imshow("origin",image);
	//整个图像设定一种新的颜色
	image=Scalar(100,200,150);
	imshow("image",image);

	waitKey();
	return 0;
}

在这里插入图片描述

PS:颜色表:
http://tool.oschina.net/commons?type=3

二、Vec< T,n >类

1.关系

在这里插入图片描述
Vex<T,n>:n个T类型的值
存储为列向量的形式。

2.例子

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main()
{
	Vec3i v(1,2,3);
	cout<<v<<endl;
	//[1, 2, 3]
	cout<<v.rows<<endl;
	//3
	cout<<v.cols<<endl;
	//1
	cout<<v.row(0)<<endl;
	//[1]
	cout<<v.col(0)<<endl;
	/*
	[1;
	2;
	3]
	*/
	return 0;
}

三、二维点Point_<_Tp>类

1.Point、Point2i、Point2f、Point2d

Point_<_Tp>是模板类,衍生有

  • Point(Point2i):其实就是Point_<int>
  • Point2f:其实就是Point_<float>
  • Point2d:其实就是Point_<double>

2.属性

Point<_Tp>类的成员变量:

  • _Tp x:横
  • _Tp y:纵

构造函数:

  • Point_ ()
    default constructor More…

  • Point_ (_Tp _x, _Tp _y)

  • Point_ (const Point_ &pt)

  • Point_ (const Size_< _Tp > &sz)

  • Point_ (const Vec< _Tp, 2 > &v)

Point<_Tp>类的成员函数:

  • dot()
    dot product
    点乘
template<typename _Tp> inline
_Tp Point_<_Tp>::dot(const Point_& pt) const
{
    return saturate_cast<_Tp>(x*pt.x + y*pt.y);
}
  • ddot()
    dot product computed in double-precision arithmetics
    double运算形式的点乘
template<typename _Tp> inline
double Point_<_Tp>::ddot(const Point_& pt) const
{
    return (double)x*pt.x + (double)y*pt.y;
}
  • cross()
    cross-product
    交叉相乘
template<typename _Tp> inline
double Point_<_Tp>::cross(const Point_& pt) const
{
    return (double)x*pt.y - (double)y*pt.x;
}
  • inside()
    checks whether the point is inside the specified rectangle
    是否在矩形内部(在边上就不是)
template<typename _Tp> inline bool
Point_<_Tp>::inside( const Rect_<_Tp>& r ) const
{
    return r.contains(*this);
}

例子

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main()
{
    Point p1(100,200), p2(300,400);

	cout<< p1.x<<' '<<p1.y<<endl;
	//100 200

	cout<< p1.dot(p2) <<endl;
	//110000
	//100*300+200*400=30000+80000=110000

	cout<< p1.ddot(p2) <<endl;
	//110000
	
	cout<< p1.cross(p2) <<endl;
	//-20000
	//(double)x1*y2 - (double)y1*x2
	//100*400-200*300=40000-6000=-20000

	cout<< p1.inside(Rect(0,0,300,300)) <<endl;
	//1
	cout<< p1.inside(Rect(0,0,200,200)) <<endl;
	//0
	return 0;
}

3.例子

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main()
{
    Point p1(100,200), p2(300,400);

	cout<< p1+p2<<endl;
	//[400, 600]

	cout<< p1*10 <<endl;
	//[1000, 2000]

	cout<< p1/9 <<endl;
	//[11, 22]
	
	cout<< (p1==p2) <<endl;
	//0

	cout<< (p1!=p2) <<endl;
	//1
	return 0;
}

四、三维点Point3_<_Tp>类

typedef Point3_<int> Point3i;
typedef Point3_<float> Point3f;
typedef Point3_<double> Point3d;

就只是多了个成员变量_Tp z

三维浮点

Point3f p1(5.4,6,7.4);
cout<<p1.x<<' '<<p1.y<<' '<<p1.z<<endl;
//5.4 6 7.4
cout<<p1<<endl;
//[5.4, 6, 7.4]

五、尺寸Size_< _Tp >类

1.Size、Size2i、Size2f

衍生有

  • Size(Size2i):其实就是Size_<int>
  • Size2f:其实就是Size_<float>

2.属性

Size_<_Tp>类的成员变量:

  • _Tp width:宽
  • _Tp height:高

构造函数:

  • Size_ ()
    default constructor More…

  • Size_ (_Tp _width, _Tp _height)

  • Size_ (const Size_ &sz)

  • Size_ (const Point_< _Tp > &pt)

Size_<_Tp>的成员函数

  • _Tp area () const
    the area (width*height)。 面积(宽×高)的结果

如:

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;

int main()
{
	Size s=Size(5,6);
	cout<<s.width<<' '<<s.height<<endl;
	//5 6
	cout<<s.area()<<endl;
	//30
	return 0;
}

六、矩形Rect_< _Tp >类

1.Rect、Rect2i、Rect2f、Rect2d

Rect_< _Tp >是模板类,衍生有

  • Rect(Rect2i):其实就是Rect_<int>
  • Rect2f:其实就是Rect_<float>
  • Rect2d:其实就是Rect_<double>

这些衍生物当然具有和Rect_<_Tp>一样的属性

2.属性

Rect_<_Tp>类的成员变量:

  • _Tp x:左上角的横,可以是负数
  • _Tp y:左上角的纵,可以是负数
  • _Tp width:宽。正右负左
  • _Tp height:高。正下负上。

构造函数:

  • Rect_ ()
    default constructor More…

  • Rect_ (_Tp _x, _Tp _y, _Tp _width, _Tp _height)
    左上的横,左上的纵,宽,高

  • Rect_ (const Rect_ &r)

  • Rect_ (const Point_< _Tp > &org, const Size_< _Tp > &sz)
    左上点和右下点

  • Rect_ (const Point_< _Tp > &pt1, const Point_< _Tp > &pt2)

Rect_<_Tp>类的成员函数:

  • Point_< _Tp > tl():以Size类的形式返回左上角的坐标(top-left)
  • Point_< _Tp > br():以Size类的形式返回右下角的坐标(bottom-right)
  • Size_< _Tp > size():长x宽,[width x height]
  • _Tp area():面积
  • bool contains(Point):判断一个点是否包含在矩形区域内

这里拿Rect来做例子

Rect r=Rect(0,0,200,200);
cout<<r.x<<endl;							//0
cout<<r.tl()<<endl;							//[0, 0]
cout<<r.br()<<endl;							//[200, 200]
cout<<r.size()<<endl;						//[200 x 200]
cout<<r.area()<<endl;						//40000
cout<<r.contains(Point(50,50))<<endl;		//1

3.运算

  • rect += point, rect -= point, rect += size, rect -= size (augmenting operations):
    增加左上角点的坐标
  • rect = rect1 & rect2 (rectangle intersection)
    两个矩形区域之间重叠的部分。如果不重叠的话,不会报错,也会返回矩形,[0 x 0 from (0, 0)]
    例如:可以用于只取图片内的部分,可能有图片外界的部分的Rect & Rect(0,0,图片宽,图片高),这样就在图片内了。注意,检测是否不重叠,返回[0 x 0 from (0, 0)]
  • rect = rect1 | rect2 (minimum area rectangle containing rect1 and rect2 )
    一个能包含住两个矩形区域的最小的矩形
  • rect == rect1, rect != rect1 (rectangle comparison)
    矩形区域是否是同一块
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main()
{
    Rect r1(Point(100,100),Point(300,300));
	Point p1(10,20);
	r1+=p1;
	cout<<r1<<endl;
	//[200 x 200 from (110, 120)]

	r1-=p1;
	cout<<r1<<endl;
	//[200 x 200 from (100, 100)]

	Rect r2(Point(50,120),Point(350,280));
	cout<< (r1 & r2) <<endl;
	//[200 x 160 from (100, 120)]

	cout<< (r1 | r2) <<endl;
	//[300 x 200 from (50, 100)]
	
	cout<< (r1 == r2) <<endl;
	//0
	cout<< (r1 != r2) <<endl;
	//1
	return 0;
}

七、RotatedRect

OpenCV之RotatedRect基本用法和角度探究

八、Ptr

万能指针,自动匹配任意类型,自动释放

Ptr <T>

比如:原来的int *b=&a;可以写成Ptr<int> b=&a

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值