opencv 基本数据结构
转自:http://www.cnblogs.com/guoqiaojin/p/3176692.html
opencv 基本数据结构
DataType : 将C++数据类型转换为对应的opencv数据类型
enum { CV_8U=0, CV_8S=1, CV_16U=2, CV_16S=3, CV_32S=4, CV_32F=5, CV_64F=6 };
// allocates a 30x40 floating-point matrix // CV_32F
Mat A(30, 40, DataType<float>::type);
Mat B = Mat_<std::complex<double> >(3, 3);
// the statement below will print 6, 2 /*, that is depth == CV_64F, channels == 2*/ CV_64FC2
cout << B.depth() << ", " << B.channels() << endl;
Point_ 二维点坐标(x,y)
typedef Point_<int> Point2i;
typedef Point2i Point;
typedef Point_<float> Point2f;
typedef Point_<double> Point2d;
Point3_ 3维点坐标(x,y,z)
typedef Point3_<int> Point3i;
typedef Point3_<float> Point3f;
typedef Point3_<double> Point3d;
Size_ 尺寸(width, height)
typedef Size_<int> Size2i;
typedef Size2i Size;
typedef Size_<float> Size2f;
Rect_ 矩形区域(x,y,width,height) ,(x,y)左上角坐标, 范围[x, x + width), [y, y + height)

rect = rect ± point //矩形偏移(shifting a rectangle by a certain offset)
rect = rect ± size //改变大小(expanding or shrinking a rectangle by a certain amount)
rect += point, rect -= point, rect += size, rect -= size //(augmenting operations)
rect = rect1 & rect2 //矩形交集(rectangle intersection)
rect = rect1 | rect2 //包含r1r2的最小矩形(minimum area rectangle containing rect2 and rect3 )
rect &= rect1, rect |= rect1 //(and the corresponding augmenting operations)
rect == rect1, rect != rect1 //(rectangle comparison)

RotatedRect 旋转矩形
RotatedRect::RotatedRect(const Point2f& center, const Size2f& size, float angle)// 中心点(不是左上角坐标),尺寸,旋转角度
RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);
Matx 小矩阵

template<typename_Tp, int m, int n> class Matx {...};
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;//Matx转化为Mat

Vec 短向量,基于Matx

template<typename_Tp, int n> class Vec : 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;

Scalar_ 四维向量
template<typename_Tp> class Scalar_: public Vec<_Tp, 4> { ... };
typedef Scalar_<double> Scalar;
Range 范围,(start, end)
Mat m(300,300,CV32F);
Mat part = m(Range::all(), Range(20, 200)); // 相当于matlab的m(:, 20 : 199)
对于自定义的函数,可以用如下方法来支持Range

void my_function(..., const Range& r, ....)
{
if(r == Range::all()) {
// process all the data, 使用全部数据
}
else {
// process [r.start, r.end),根据r中定义, 处理数据 start : end - 1
}
}

Mat 矩阵结构
- M.data 数据区域的指针
- M.dims 矩阵维度
- M.sizes 维度
- M.elemSize() 每个元素占的字节空间大小,与元素类型相关,如CV_8U
- M.step[] 用来计算元素地址, M.step[i] 表示所有比i大的维度所占空间大小
M.step[i] >= M.step[i+1]*M.sizes[i+1]; //这里大于是因为数据空间可能有空白
- 2-dimensional matrices are stored row-by-row
- 3-dimensional matrices are stored plane-by-plane
addr(M(i(0),...,i(M.dims−1))) = M.data + M.step[0] ∗ i(0)+ M.step[1] ∗ i(1)+ ... + M.step[M.dims − 1] ∗ i(M.dims−1)
创建数组:

// make a 7x7 complex matrix filled with 1+3j.
Mat M(7,7,CV_32FC2,Scalar(1,3));
// and now turn M to a 100x60 15-channel 8-bit matrix.
// The old content will be deallocated
M.create(100,60,CV_8UC(15));
// create a 100x100x100 8-bit array
int sz[] = {100, 100, 100};
Mat bigCube(3, sz, CV_8U, Scalar::all(0));

创建特殊矩阵:
- diag
- ones
- zeros
- eye
属性相关:
- rows
- cols
- begin
- end
- at
- size
- depth
- type
- elemSize
- total
矩阵操作:
- t
- inv
- mul
- cross
- dot
- reshape
- resize
- reserve
- push_back
- pop_back
赋值相关:
- clone
- copyTo
- convertTo
- assignTo
- setTo
InputArray
OutputArray

//Do not explicitly create InputArray, OutputArray instances
void myAffineTransform(InputArray_src, OutputArray_dst, InputArray_m)
{
// get Mat headers for input arrays. This is O(1) operation,
// unless_src and/or_m are matrix expressions.
Mat src =_src.getMat(), m =_m.getMat();
CV_Assert( src.type() == CV_32FC2 && m.type() == CV_32F && m.size() == Size(3, 2) );
// [re]create the output array so that it has the proper size and type.
// In case of Mat it calls Mat::create, in case of STL vector it calls vector::resize.
_dst.create(src.size(), src.type());
Mat dst =_dst.getMat();
for( int i = 0; i < src.rows; i++ )
for( int j = 0; j < src.cols; j++ )
{
Point2f pt = src.at<Point2f>(i, j);
dst.at<Point2f>(i, j) = Point2f(m.at<float>(0, 0)*pt.x +
m.at<float>(0, 1)*pt.y +
m.at<float>(0, 2),
m.at<float>(1, 0)*pt.x +
m.at<float>(1, 1)*pt.y +
m.at<float>(1, 2));
}
}

SparseMat 稀疏矩阵
Algorithm 实现一个算法的框架
opencv 基本数据结构的更多相关文章
- Opencv基本数据结构
Opencv的数据结构:CvPoint系列.CvSize系列 .CvSize.CvRect.CvScalar.CvAr 大多数据结构都在cxtypes.h这个头文件里定义 1.CvPoint系列: ...
- 【转】OpenCV Mat数据结构
转载自xiahouzuoxin原文 OpenCV基础篇之Mat数据结构 程序及分析 /* * FileName : MatObj.cpp * Author : xiahouzuoxin @163.co ...
- opencv常用数据结构之:IplImage
typedef struct_IplImage{ int nSize; //IplImage大小 int ID; //版本(=0) int nChannels; //大多 ...
- Opencv 的数据结构
opencv的基本数据结构 结构 成员 意义 CvPoint int x,y 图像中的点 CvPoint2D32f float x,y 二维空间中的点 CvPoint3D32f float x,y,z ...
- OpenCV常用数据结构和函数
点的表示:Point类 Point类数据结构表示二维坐标系下的点,即由其图像坐标x,y指定的2D点. 用法如下 Point point; point.x = 10; point.y = 8; 或者 P ...
- opencv 基础数据结构
头文件:cxcore/include/cxtypes.h CvPoint: CvPoint:(构造cvPoint) CvPoint2D32f: CvPoint3D32f: CvSize: CvSize ...
- opencv学习_4(opencv基础数据结构 CvPoint & CvSize & CvRect & CvScalar & CvArr & CvMat)
1:包含在cxcore/include/cxtypes.h头文件中. 2:CvPoint系列 -----(x,y) CvPoint:表示图像中的点 CvPoint2D32f:二维空间中的点 CvP ...
- opencv常用数据结构
2019/10/29 1.Mat 成员函数:cols.rows.channels.ptr获取任意行的首地址.at处理像素 2.InputArray/OutArray相当于Mat 2019/11/4 1 ...
- opencv数据结构总结
OpenCV里面用到了很多图像相关的数据结构,熟练掌握它们是学习图像的基础. 1.IplImage IplImage IplImage IPL 图像头 typedef struct _IplImage ...
随机推荐
- ie-9 以下ajax无法跨域的问题。只要add:jQuery.support.cors=true;即可
if (!jQuery.support.cors && window.XDomainRequest) { var httpRegEx = /^https?:\/\//i; var ge ...
- (转)Android 弹软键盘时listview的变化控制
本文转自于:http://blog.csdn.net/hellohaifei/article/details/9377483 问题描述:界面中有三部分,上面是标题栏,中间是Listview,下面是输入 ...
- 条款52:谢了placement new 也就同时应该写一个placement delete
如果operator new接收到的参数除了size_t之外还有其他的话,那么这个operator new实际上就是一个placement new,所以考虑下下面这样的情况: 一个可以用来记录信息的p ...
- L123
My heart, the bird of the wilderness, has found its sky in your eyes. 我的心是旷野的鸟,在你的双眼中找到了天空.His main ...
- 基于zepto移动4*3九宫格转奖
最近根据公司需求,要把移动端的圆形转盘抽奖,改为九宫格的形式,查找资料搞定了,纪录下demo代码. 页面的展现样式,如下 比较简单,就是红色的背景图,在这10个格子里转动 具体代码如下 html &l ...
- unity 四元数, 两行等价的代码
Vector3 tmpvc; 1. tmpvc = Quaternion.Euler (new Vector3 (0, 30, 0)) * new Vector3 (0, 0, 1); 2. tmpv ...
- WPF之ContextMenu的命定绑定
在WPF中右击菜单项的XMAL代码是: <ContextMenu x:Key="sampleContextMenu"> <MenuItem Header=&quo ...
- AtCoder Beginner Contest 087 B - Coins
Time limit : 2sec / Memory limit : 256MB Score : 200 points Problem Statement You have A 500-yen coi ...
- LeetCode Max Consecutive Ones II
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...
- flask+blueprint路由配置
1.flask默认的静态文件和html文件在app应用文件夹里的相应文件夹下:app // Flask||--static ||--templates |静态文件默认的url地址为:url_prefi ...