OpenCV基础入门 基于官方文档解读(3)--core模块学习

来源:互联网 发布:知乎 美国epic公司 编辑:程序博客网 时间:2024/06/09 21:29

今天正式进入模块的内容,第一个模块必然是我们最基础的core模块啦。core模块涉及的内容不算复杂,主要包括基础数据结构部分,基本函数,绘图函数,XML/YAML文件存储等。虽然说内容不复杂但是零零星星的结构和函数加到一起却很多。官方文档大致用了近百页来讲解这一基础模块,既然是基础,那么必然是重中之重了。在这里一方面为了自己巩固基础知识,另一方面和大家一起探讨关于文档的相关内容和拓展,所以仅仅介绍一部分经常用到的内容,还有些不经常用到的我们可以需要的时候再去翻看查阅。学习总是这样,学得太少了,就会觉得没有自信心,学习的太多了又感觉很烦,其实有些时候完全可以按照自己的方式学习重要部分将核心内容掌握,其它旁枝末节就没有必要花大片时间去学,上个厕所可能顺便就能弄明白的东西,就已经没有必要花大把时间去学了,所以我觉得学习还是需要取舍有度的。

在core模块里有些类是通过模板类建立起来的,所以如果不熟悉C++模板的可以先去了解一下,虽然我之前学过C++但是也是通过OpenCV才认识模板这一强大的功能的,想必也有些人和我一样刚开始遇到了ptr<uchar>后不知怎么回事,后面章节我也会试着总结一下C++模板的内容。相关博文和书籍网上一大堆,不了解的可以去了解一下。

1.Point_类

class Point_template<typename _Tp> class CV_EXPORTS Point_{public:typedef _Tp value_type; //alias//几个构造函数,各有不同的参数输入形式。Point_();Point_(_Tp _x, _Tp _y);Point_(const Point_& pt);Point_(const CvPoint& pt);Point_(const CvPoint2D32f& pt);Point_(const Size_<_Tp>& sz);Point_(const Vec<_Tp, 2>& v);Point_& operator = (const Point_& pt);//转换成其他结构template<typename _Tp2> operator Point_<_Tp2>() const;//旧版本的几种形式operator CvPoint() const;operator CvPoint2D32f() const;operator Vec<_Tp, 2>() const;_Tp dot(const Point_& pt) const;double ddot(const Point_& pt) const;double cross(const Point_& pt) const;//判断该点是否在长方形内  Rect_类是用来表示长方形结构bool inside(const Rect_<_Tp>& r) const;_Tp x, y; //< the point coordinates};

Point_类很简单,最重要的是两个属性 x和y,用来存储二维坐标。上面的7个构造函数表示可以以不同的输入参数进行初始化对象。

对于Point_类的使用,有下面的别名更方便使用和记忆

typedef Point_<int> Point2i;   //整型坐标typedef Point2i  Point;typedef Point_<float> Pointf;   //浮点型坐标typedef Point_<double> Point2d; // double型坐标
例如

#include <opencv2/opencv.hpp>#include <iostream>using namespace std;using namespace cv;int main(){Point2f a(0.3f, 0.f), b(0.f, 0.4f);cout << a << endl << b << endl;}
显示结果如图

对于同数据类型之间的坐标可以进行加减和数乘运算

pt1 = pt2 + pt3;pt1 = pt2 - pt3;pt1 = pt2 * a;pt1 = a * pt2;pt1 += pt2;pt1 -= pt2;pt1 *= a;double value = norm(pt); // L2 normpt1 == pt2;pt1 != pt2;

2. Point3_类

Point3_类和Point2_几乎没有差别,只不过是用来存储三维空间的坐标。

3.Size_类

class Size_template<typename _Tp> class CV_EXPORTS Size_{public:typedef _Tp value_type;//! various constructorsSize_();Size_(_Tp _width, _Tp _height);Size_(const Size_& sz);Size_(const CvSize& sz);Size_(const CvSize2D32f& sz);Size_(const Point_<_Tp>& pt);Size_& operator = (const Size_& sz);//! the area (width*height)_Tp area() const;//! conversion of another data type.template<typename _Tp2> operator Size_<_Tp2>() const;//! conversion to the old-style OpenCV typesoperator CvSize() const;operator CvSize2D32f() const;_Tp width, height; // the width and the height};


Size_类主要用来度量矩阵的宽和高,也就是类里面的两个属性width 和 height。

模板衍生的实例有下面的别名

typedef Size_<int> Size2i;typedef Size2i Size;typedef Size_<float> Size2f;
举例说明

#include <opencv2/opencv.hpp>#include <iostream>using namespace std;using namespace cv;int main(){Scalar S;Mat a(Size(4, 5),CV_8UC1, S(0));cout << a << endl;}
运行结果如图


构建一个Size(4, 5) 宽4 高5的矩阵初始值为0  通道类型为CV_8UC1。

4 Rect_类

矩形结构

class Rect_template<typename _Tp> class CV_EXPORTS Rect_{public:typedef _Tp value_type;//! various constructorsRect_();Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);  //下面的例子里用到这个构造函数Rect_(const Rect_& r);Rect_(const CvRect& r);Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz);Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2);Rect_& operator = ( const Rect_& r );//! the top-left cornerPoint_<_Tp> tl() const;//! the bottom-right cornerPoint_<_Tp> br() const;//! size (width, height) of the rectangleSize_<_Tp> size() const;//! area (width*height) of the rectangle_Tp area() const;//! conversion to another data typetemplate<typename _Tp2> operator Rect_<_Tp2>() const;//! conversion to the old-style CvRectoperator CvRect() const;//! checks whether the rectangle contains the pointbool contains(const Point_<_Tp>& pt) const;_Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle};
矩形结构在OpenCV中主要用于图像的感兴趣区域选取(ROI)。

Rect_模板的int实例化有下面的别名

typedef Rect_<int> Rect;
举例表示Rect_的使用。

#include <opencv2/opencv.hpp>#include <iostream>using namespace std;using namespace cv;int main(){Rect a(80, 20, 180, 160);  //Mat pic, rect_pic;pic = imread("1.jpg");imshow("原始图像", pic);waitKey(30);rect_pic = pic(a);namedWindow("原始图像");namedWindow("ROI");imshow("ROI", rect_pic);waitKey(0);return 0;}
结果如图

rect_pic 变量就是选取的原图的ROI区域。这里面用到了Rect_类的第一个构造函数。

今天先介绍这么多,后续会介绍更为重要的Mat数据结构和vec向量数据结构。
推荐模板编程的书籍 --温宇杰的《深入实践C++模板编程》这里面讲的很细致了。

下一篇文章再见。

0 0
原创粉丝点击