您的位置:首页 > 运维架构

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

2014-01-14 09:33 447 查看
1Point_

二维:typedefPoint_<int>Point2i;

typedefPoint2iPoint;

typedefPoint_<float>Point2f;

typedefPoint_<double>Point2d;


Point_<>模版类

或者,旧版:CvPointCvPoint2D32f

三维:typedefPoint3_<int>Point3i;

typedefPoint3_<float>Point3f;

typedefPoint3_<double>Point3d;


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

2、Size_

typedefSize_<int>Size2i;

typedefSize2iSize;

typedefSize_<float>Size2f;


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

3、Rect_

typedefRect_<int>Rect;


(shiftingarectanglebyacertainoffset偏移)

(expandingorshrinkingarectanglebyacertainamount)

rect=rect1&rect2(rectangleintersection)

rect=rect1|rect2(minimumarearectanglecontainrect2andrect3)

Coordinatesofthetop-leftcorner.ThisisadefaultinterpretationofRect_::xandRect_::yinOpenCV.

OpenCVtypicallyassumesthatthetopandleftboundaryoftherectangleareinclusive,whiletherightandbottomboundariesarenot.

4、RotatedRect(旋转矩阵)

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

Point2fvertices[4];

rRect.points(vertices);

Rectbrect=rRect.boundingRect();

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

5、Matx

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

template<typename_Tp,intm,intn>classMatx{...};


typedefMatx<float,1,2>Matx12f;

typedefMatx<double,1,2>Matx12d;

...

typedefMatx<float,1,6>Matx16f;

typedefMatx<double,1,6>Matx16d;


typedefMatx<float,2,1>Matx21f;

typedefMatx<double,2,1>Matx21d;

...

typedefMatx<float,6,1>Matx61f;

typedefMatx<double,6,1>Matx61d;


typedefMatx<float,2,2>Matx22f;

typedefMatx<double,2,2>Matx22d;

...

typedefMatx<float,6,6>Matx66f;

typedefMatx<double,6,6>Matx66d;


例:Matx33fm(1,2,3,

4,5,6,

7,8,9);

cout<<sum(Mat(m*m.t()))<<endl;


6、Vec

元素较少的向量

template<typename_Tp,intn>classVec:publicMatx<_Tp,n,1>{...};


typedefVec<uchar,2>Vec2b;

typedefVec<uchar,3>Vec3b;

typedefVec<uchar,4>Vec4b;


typedefVec<short,2>Vec2s;

typedefVec<short,3>Vec3s;

typedefVec<short,4>Vec4s;


typedefVec<int,2>Vec2i;

typedefVec<int,3>Vec3i;

typedefVec<int,4>Vec4i;


typedefVec<float,2>Vec2f;

typedefVec<float,3>Vec3f;

typedefVec<float,4>Vec4f;

typedefVec<float,6>Vec6f;


typedefVec<double,2>Vec2d;

typedefVec<double,3>Vec3d;

typedefVec<double,4>Vec4d;

typedefVec<double,6>Vec6d;


8、Scalar_

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

template<typename_Tp>classScalar_:publicVec<_Tp,4>{...};

typedefScalar_<double>Scalar;


7、Mat

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

intmain(void)
{
Matimg1=imread("D:/picture/images/baboon1.jpg",0);//灰度图像

cout<<img1.rows<<endl;
cout<<img1.cols<<endl;
cout<<img1.size[0]<<endl;//与img1.rows相等

cout<<(int)img1.at<uchar>(1,2)<<endl;//(1,2)点的灰度值
cout<<(int)(*(img1.data+img1.step[0]*1+img1.step[1]*2))<<endl;//(1,2)点的灰度值

Matimg2=imread("D:/picture/images/baboon1.jpg");//彩色图像
cout<<(int)img2.at<Vec3b>(1,2)[1]<<endl;//(1,2)点的G分量
cout<<(int)(*(img2.data+img2.step[0]*1+img2.step[1]*2+sizeof(uchar)))<<endl;//(1,2)点的G分量
return0;
}

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

我们看到,使用at函数和直接求出地址然后对地直接引得到的像素值是相同的。at操作是一个模板函数,所以对于灰度图像,返回的直接就是对应的像素值;而对于彩色图像,返回的是一个3元组Vec3b。这个结构是3个uchar连续的放在一起的。所以对于彩色图像,直接使用公式获得的是B分量,如果想获得其他分量,那么得在原始的地址上加上一个sizeof(uchar)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: