http://docs.opencv.org/

opencv 2.x API

opencv包含以下模块

  • core    基本数据机构
  • imgproc    图像处理方法
  • video    视频处理方法
  • calib3d    图形学方法
  • features2d    区域检测算子和描述算子
  • objdetect    目标检测方法
  • highgui    GUI相关
  • gup    GPU加速方法
  • ......

opencv中有些函数名称会与其他包提供的名称冲突,因此:

using namespace cv;

 OpenCV内存管理
opencv的基本数据结构如Mat包含析构函数,在引用数为0时,释放空间;引用数非0时调用则引用数减1。copy只是增加引用,clone才重新分配内存。
原文:

This means that the destructors do not always deallocate the buffers as in case of Mat. They take into account possible data sharing. A destructor decrements the reference counter associated with the matrix data buffer. The buffer is deallocated if and only if the reference counter reaches zero, that is, when no other structures refer to the same buffer. Similarly, when a Mat instance is copied, no actual data is really copied. Instead, the reference counter is incremented to memorize that there is another owner of the same data. There is also the Mat::clone method that creates a full copy of the matrix data. 
// create a big 8Mb matrix
//新建矩阵
Mat A(, , CV_64F); // create another header for the same matrix;
// this is an instant operation, regardless of the matrix size.
// AB共享内存
Mat B = A; // create another header for the 3-rd row of A; no data is copied either
// C指向A和B的第三行
Mat C = B.row(); // now create a separate copy of the matrix
// 新的矩阵,重新分配内存
Mat D = B.clone(); // copy the 5-th row of B to C, that is, copy the 5-th row of A
// to the 3-rd row of A.
//将B的第五行复制到C,也就是复制到A,B的第三行
B.row().copyTo(C); // now let A and D share the data; after that the modified version
// of A is still referenced by B and C.
// 将A指向D, B,C仍然指向第一行申请的内存空间
A = D; // now make B an empty matrix (which references no memory buffers),
// but the modified version of A will still be referenced by C,
// despite that C is just a single row of the original A
// 释放B,但是B所指向的内存空间没有被释放,因为还有C,虽然C只是指向了其中一行
B.release(); // finally, make a full copy of C. As a result, the big modified
// matrix will be deallocated, since it is not referenced by anyone
// 将原先C指向的一行clone,那么第一行申请的1000*1000的矩阵就引用数为0,则自动释放
C = C.clone();

对于自定义的数据类型,opencv提供了类似的机制来管理内存:

// 传统方法
T* ptr = new T(); // 推荐方法 Ptr<T> 分配一个指针和一个引用计数器
Ptr<T> ptr = new T();
template<typename_Tp> class Ptr
{
  public:
    // default constructor
    Ptr();
    // constructor that wraps the object pointer
    Ptr(_Tp*_obj);
    // destructor: calls release()
    ~Ptr();
    // copy constructor; increments ptr’s reference counter
    Ptr(const Ptr& ptr);
    // assignment operator; decrements own reference counter
    // (with release()) and increments ptr’s reference counter
    Ptr& operator = (const Ptr& ptr);
    // increments reference counter
    void addref();
    // decrements reference counter; when it becomes 0,
    // delete_obj() is called
    void release();
    // user-specified custom object deletion operation.
    // by default, "delete obj;" is called
    void delete_obj();
    // returns true if obj == 0;
    bool empty() const;
    // provide access to the object fields and methods
    _Tp* operator -> ();
    const _Tp*operator -> () const;
    // return the underlying object pointer;
    // thanks to the methods, the Ptr<_Tp> can be
    // used instead of_Tp*
    operator _Tp*();
    operator const _Tp*() const;
  protected:
    // the encapsulated object pointer
    _Tp* obj;
    // the associated reference counter
    int* refcount;
};

函数返回值自动分配内存

#include "cv.h"
#include "highgui.h"
using namespace cv;
int main(int, char**)
{
  VideoCapture cap();
  if(!cap.isOpened()) return -;
  Mat frame, edges;
  namedWindow("edges",);
  for(;;)
  {
    cap >> frame;
    cvtColor(frame, edges, CV_BGR2GRAY);
    GaussianBlur(edges, edges, Size(,), 1.5, 1.5);
    Canny(edges, edges, , , );
    imshow("edges", edges);
    if(waitKey() >= ) break;
  }
  return ;
}
  • cap >> frame 自动分配内存
  • cvtColor也会为edges自动分配内存
  • 在for循环中,只有第一次会分配内存(若分辨率改变,则会重新分配)

机制: Mat::create() 会检测包含的数组的大小和类型,若满足要求,则不作处理;若为空,则分配;若不满足要求,则先释放再分配。

例外:Some notable exceptions from this scheme are cv::mixChannels, cv::RNG::fill, and a few other functions and methods. They are not able to allocate the output array, so you have to do this in advance.

像素数据类型
• 8-bit unsigned integer (uchar)
• 8-bit signed integer (schar)
• 16-bit unsigned integer (ushort)
• 16-bit signed integer (short)
• 32-bit signed integer (int)
• 32-bit floating-point number (float)
• 64-bit floating-point number (double)

enum { CV_8U=, CV_8S=, CV_16U=, CV_16S=, CV_32S=, CV_32F=, CV_64F= };

颜色通道:(最大通道数CV_CN_MAX = 512)
• CV_8UC1 ... CV_64FC4 constants (for a number of channels from 1 to 4)
• CV_8UC(n) ... CV_64FC(n) or CV_MAKETYPE(CV_8U, n) ... CV_MAKETYPE(CV_64F, n) macros when the
number of channels is more than 4 or unknown at the compilation time.

CV_32FC1 == CV_32F,
CV_32FC2 == CV_32FC(2) == CV_MAKETYPE(CV_32F, 2),

// 原文中的没看懂。。。 理解应该是 CV_MAKETYPE(depth, n) == (n-1)<<3+x, x=0,...,6对应{ CV_8U=, CV_8S=, CV_16U=, CV_16S=, CV_32S=, CV_32F=, CV_64F= };
CV_MAKETYPE(depth, n) == ((x&)<<) + (n-)
Mat mtx(, , CV_32F); // make a 3x3 floating-point matrix
Mat cmtx(, , CV_64FC2); // make a 10x1 2-channel floating-point
// matrix (10-element complex vector)
Mat img(Size(, ), CV_8UC3); // make a 3-channel (color) image
// of 1920 columns and 1080 rows.
Mat grayscale(image.size(), CV_MAKETYPE(image.depth(), )); // make a 1-channel image of
// the same size and same
// channel type as img

opencv 笔记的更多相关文章

  1. OpenCV笔记大集锦(转载)

    整理了我所了解的有关OpenCV的学习笔记.原理分析.使用例程等相关的博文.排序不分先后,随机整理的.如果有好的资源,也欢迎介绍和分享. 1:OpenCV学习笔记 作者:CSDN数量:55篇博文网址: ...

  2. opencv笔记6:角点检测

    time:2015年10月09日 星期五 23时11分58秒 # opencv笔记6:角点检测 update:从角点检测,学习图像的特征,这是后续图像跟踪.图像匹配的基础. 角点检测是什么鬼?前面一篇 ...

  3. opencv笔记5:频域和空域的一点理解

    time:2015年10月06日 星期二 12时14分51秒 # opencv笔记5:频域和空域的一点理解 空间域和频率域 傅立叶变换是f(t)乘以正弦项的展开,正弦项的频率由u(其实是miu)的值决 ...

  4. opencv笔记4:模板运算和常见滤波操作

    time:2015年10月04日 星期日 00时00分27秒 # opencv笔记4:模板运算和常见滤波操作 这一篇主要是学习模板运算,了解各种模板运算的运算过程和分类,理论方面主要参考<图像工 ...

  5. opencv笔记3:trackbar简单使用

    time:2015年 10月 03日 星期六 13:54:17 CST # opencv笔记3:trackbar简单使用 当需要测试某变量的一系列取值取值会产生什么结果时,适合用trackbar.看起 ...

  6. opencv笔记2:图像ROI

    time:2015年 10月 03日 星期六 12:03:45 CST # opencv笔记2:图像ROI ROI ROI意思是Region Of Interests,感兴趣区域,是一个图中的一个子区 ...

  7. opencv笔记1:opencv的基本模块,以及环境搭建

    opencv笔记1:opencv的基本模块,以及环境搭建 安装系统 使用fedora22-workstation-x86_64 安装opencv sudo dnf install opencv-dev ...

  8. OpenCV基本架构[OpenCV 笔记0]

    最近正在系统学习OpenCV,将不定期发布笔记,主要按照毛星云的<OpenCV3编程入门>的顺序学习,会参考官方教程和文档.学习工具是Xcode+CMake,会对书中一部分内容更正,并加入 ...

  9. 查找并绘制轮廓[OpenCV 笔记XX]

    好久没有更新了,原谅自己放了个假最近又在赶进度,所以...更新的内容是很靠后的第八章,因为最近工作要用就先跳了,后面会更新笔记编号...加油加油! 在二值图像中寻找轮廓 void cv::findCo ...

  10. 访问图像中的像素[OpenCV 笔记16]

    再更一发好久没更过的OpenCV,不过其实写到这个部分对计算机视觉算法有所了解的应该可以做到用什么查什么了,所以后面可能会更的慢一点吧,既然开了新坑,还是机器学习更有研究价值吧... 图像在内存中的存 ...

随机推荐

  1. ActiveMQ测试工具

    1. 测试工具 目前使用两种测试工具进行压力测试 1. Jmeter 测试单客户端收发多主题,测试高并发,大数据量时的接收效率 2. emqtt_benchmark测试多客户端收发主题,测试高吞吐量下 ...

  2. 笔记04 WPF对象引用

    转自:http://www.fx114.net/qa-261-90254.aspx 我们应该都知道,XAML是一种声明式语言,XAML的标签声明的就是对象.一个XAML标签会对应着一个对象,这个对象一 ...

  3. iOS用户是否打开APP通知开关跳转到系统的设置界面

    1.检测用户是否打开推送通知  /** 系统通知是否打开 @return 是否打开 */ //检测通知是否打开iOS8以后有所变化 所以需要适配iOS7 + (BOOL)openThePushNoti ...

  4. 关于TCP通信程序中数据的传递格式

    前言 在之前的回射程序中,实现了字符串的传递与回射.幸运的是,字符串的传递不用担心不同计算机类型的大小端匹配问题,然而,如果传递二进制数据,这就是一个要好好考虑的问题.在客户端和服务器使用不同的字节序 ...

  5. hdu5317 RGCDQ 统计

    // hdu5317 RGCDQ // // 题目大意: // // 给定一个闭区间[l,r],定义f(x)是x的不同的质因子的个数 // 比方: 12 = 2 * 2 * 3,是两种.所以f(x) ...

  6. Android Material Design 中文版

    http://www.google.com/design/spec/animation/authentic-motion.html http://www.oschina.net/question/14 ...

  7. 【BZOJ3728】PA2014Final Zarowki 贪心

    [BZOJ3728]PA2014Final Zarowki Description 有n个房间和n盏灯,你需要在每个房间里放入一盏灯.每盏灯都有一定功率,每间房间都需要不少于一定功率的灯泡才可以完全照 ...

  8. 原来浏览器原生支持JS Base64编码解码 outside of the Latin1 range

    原来浏览器原生支持JS Base64编码解码 « 张鑫旭-鑫空间-鑫生活 https://www.zhangxinxu.com/wordpress/2018/08/js-base64-atob-bto ...

  9. Maximum likelihood from incomplete data via the EM algorithm (1977)

    Maximum likelihood from incomplete data via the EM algorithm (1977)  

  10. 使用Scapy回放报文pcap

    一.准备环境: Ubuntu + python2.7 sudo apt-get install python-scapy   二.准备报文: 先抓取一些报文,本实验使用的是DHCP的报文. 文件-导出 ...