OpenCV 2.4.X最常用的数据类型

1Point_

二维: typedefPoint_<int>Point2i;
       typedefPoint2iPoint;
       typedefPoint_<float> Point2f;
       typedefPoint_<double> Point2d;

Point_<> 模版类

或者,旧版:CvPoint  CvPoint2D32f

 

三维: typedef Point3_<int> Point3i;
       typedef Point3_<float> Point3f;
       typedef Point3_<double> Point3d;

点只可以进行加减和比较,不能进行乘除运算。

 

2Size_

typedef Size_<int> Size2i;
typedef Size2i Size;
typedef Size_<float> Size2f;

用于定义一个长为x,宽为y的图形或者矩形

 

3Rect_ 

typedef Rect_<int> Rect;

 (shiftinga rectangle by a certain offset偏移)

 (expandingor shrinking a rectangle by a certain amount)

rect = rect1 & rect2 (rectangleintersection)

rect = rect1 | rect2 (minimumarea rectangle contain rect2 and rect3 )

Coordinates of the top-left corner. This isa default interpretation of Rect_::x and Rect_::y inOpenCV.

OpenCV typicallyassumes that the top and left boundary of the rectangle are inclusive, whilethe right and bottom boundaries are not.

 

4RotatedRect(旋转矩阵)

RotatedRect rRect =RotatedRect(Point2f(200,200),Size2f(200,100), 60);

Point2f vertices[4];

rRect.points(vertices);

Rect brect = rRect.boundingRect();

 

可以使用points函数返回它的4个顶点,使用boundingRect求出它的外接矩形(非旋转)

 

5Matx

用来记录一些小的矩形,这些矩形在编译前大小就固定了。

template<typename _Tp, int m, int n>classMatx {...};
 
typedef Matx<float,1,2> Matx12f;
typedef Matx<double,1,2> Matx12d;
...
typedef Matx<float,1,6> Matx16f;
typedef Matx<double,1,6> Matx16d;
 
typedef Matx<float,2,1> Matx21f;
typedef Matx<double,2,1> Matx21d;
...
typedef Matx<float,6,1> Matx61f;
typedef Matx<double,6,1> Matx61d;
 
typedef Matx<float,2,2> Matx22f;
typedef Matx<double,2,2> Matx22d;
...
typedef Matx<float,6,6> Matx66f;
typedef Matx<double,6,6> Matx66d;
 
例:Matx33f m(1,2,3,
              4,5,6,
              7,8,9);
    cout<< sum(Mat(m*m.t()))<< endl;

 

6Vec

元素较少的向量

template<typename _Tp, int n>classVec:public Matx<_Tp, n, 1> {...};
 
typedef Vec<uchar,2> Vec2b;
typedef Vec<uchar, 3> Vec3b;
typedef Vec<uchar,4> Vec4b;
 
typedef Vec<short,2> Vec2s;
typedef Vec<short,3> Vec3s;
typedef Vec<short,4> Vec4s;
 
typedef Vec<int,2> Vec2i;
typedef Vec<int,3> Vec3i;
typedef Vec<int,4> Vec4i;
 
typedef Vec<float,2> Vec2f;
typedef Vec<float,3> Vec3f;
typedef Vec<float,4> Vec4f;
typedef Vec<float,6> Vec6f;
 
typedef Vec<double,2> Vec2d;
typedef Vec<double,3> Vec3d;
typedef Vec<double,4> Vec4d;
typedef Vec<double,6> Vec6d;

 

8Scalar_

Vec<tp,4>派生下来的,也就是说,它是一个4元组。

template<typename _Tp>classScalar_:public Vec<_Tp,4> { ... };
typedef Scalar_<double> Scalar;


7Mat

不仅可以把它当做一个储存图像的数据结构,还可以储存二维矩阵,也可以储存高维矩阵。在2.0以上的版本中,使用Mat类来储存一幅图像。数据存在data指针所指向的地址中的。

  1. int main(void)  
  2. {  
  3.     Mat img1 = imread("D:/picture/images/baboon1.jpg",0);//灰度图像  
  4.   
  5.     cout<<img1.rows<<endl;  
  6.     cout<<img1.cols<<endl;  
  7.     cout<<img1.size[0]<<endl;//img1.rows相等  
  8.   
  9.     cout<<(int)img1.at<uchar>(1,2)<<endl;//(1,2)点的灰度值  
  10.     cout<<(int)(*(img1.data + img1.step[0] * 1 + img1.step[1] * 2))<<endl;//(1,2)点的灰度值  
  11.   
  12.     Mat img2 = imread("D:/picture/images/baboon1.jpg");//彩色图像  
  13.     cout<<(int)img2.at<Vec3b>(1,2)[1]<<endl;//(1,2)点的G分量  
  14.     cout<<(int)(*(img2.data + img2.step[0] * 1 + img2.step[1] * 2  + sizeof(uchar)))<<endl;//(1,2)点的G分量  
  15.     return 0;  
  16. }  

因为8bit图像对应的像素值为0~255,所以opencv使用了uchar类型,这样非常节省空间;但是如果想看到具体的像素值,那么得把他转化为int

我们看到,使用at函数和直接求出地址然后对地直接引得到的像素值是相同的。at操作是一个模板函数,所以对于灰度图像,返回的直接就是对应的像素值;而对于彩色图像,返回的是一个3元组Vec3b这个结构是3uchar连续的放在一起的。所以对于彩色图像,直接使用公式获得的是B分量,如果想获得其他分量,那么得在原始的地址上加上一个sizeof(uchar)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值