1.Mat::imread()

C++: Mat imread(const string& filename, int flags=1 )

filename – Name of file to be loaded.

flags –

Flags specifying the color type of a loaded image:

  • CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
  • CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
  • CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
  • >0 Return a 3-channel color image.
  • Note In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.
  • =0 Return a grayscale image.
  • <0 Return the loaded image as is (with alpha channel).
  • 2.Mat::create()

  P36 “使用输入输出函数”一节中,提到的“额外的复制操作可以通过一种实现技巧来避免”,额外的复制操作是指:Mat imageClone = image.clone();
这里不是说不需要复制,而是说不需要显式的分配一个同等大小的数组内存,我们可以利用Mat::create成员函数,加入我们定义一个Mat copy,此时copy一定不会和image同等大小(copy为空),那么create就会为copy分配与image同等大小的数组,为什么要这样做呢?文中提到这样做可以使用户决定是否采用in-place操作,采用in-place操作,势必会改变原图像,而当我们输入输出都制定为image时,create函数将会直接返回。此时函数中image1(形参image)和image2(形参result)都引用了同一个变量image,指向同一块内存地址,而image1为常量引用,image2为普通引用,虽然不能通过image1进行in-place操作(const引用无法更改变量值),但我们可以通过image2来进行in-place操作。(假如a常量引用b,c普通引用b,a不能更改b但是不代表所有b的引用都不能,c就可以改变b的值)

Mat::create() create()函数只在当前数组大小或者类型与指定的不一样时才分配新的数组。
Allocates new array data if needed.
C++: void Mat::create(int rows, int cols, int type)

C++: void Mat::create(Size size, int type)

C++: void Mat::create(int ndims, const int* sizes, int type)
Parameters:

  • ndims – New array dimensionality.
  • rows – New number of rows.
  • cols – New number of columns.
  • size – Alternative new matrix size specification: Size(cols, rows)
  • sizes – Array of integers specifying a new array shape.
  • type – New matrix type.

This is one of the key Mat methods. Most new-style OpenCV functions and methods that produce arrays call this method for each output array. The method uses the following algorithm:

  • If the current array shape and the type match the new ones, return immediately. Otherwise, de-reference the previous data by callingMat::release().
  • Initialize the new header.
  • Allocate the new data of total()*elemSize() bytes.
  • Allocate the new, associated with the data, reference counter and set it to 1.

Such a scheme makes the memory management robust and efficient at the same time and helps avoid extra typing for you. This means that usually there is no need to explicitly allocate output arrays. That is, instead of writing:

  1. Mat color;
  2. ...
  3. Mat gray(color.rows, color.cols, color.depth()); --没必要的,因为cvtColor 会帮我们先调用一个create方法。
  4. cvtColor(color, gray, CV_BGR2GRAY);
  5. you can simply write:
  6. Mat color;
  7. ...
  8. Mat gray;
  9. cvtColor(color, gray, CV_BGR2GRAY);

because cvtColor , as well as the most of OpenCV functions, calls Mat::create() for the output array internally.

3. saturate_cast截断

4. img.row() Scalar()

【Opencv】Mat基础的更多相关文章

  1. 【VS开发】C++ opencv Mat基础

    OpenCV2:Mat 1.Mat基础 在计算机内存中,数字图像是已矩阵的形式保存的.OpenCV2中,数据结构Mat是保存图像像素信息的矩阵,它主要包含两部分:矩阵头和一个指向像素数据的矩阵指针. ...

  2. Matlab to OpenCV Mat

    convert Matlab matrix to OpenCV Mat. Support CV_32FC3 only currently. The Code int matlab2opencv(cv: ...

  3. OpenCV Mat数据类型及位数总结(转载)

    OpenCV Mat数据类型及位数总结(转载) 前言 opencv中很多数据结构为了达到內存使用的最优化,通常都会用它最小上限的空间来分配变量,有的数据结构也会因为图像文件格式的关系而给予适当的变量, ...

  4. opencv mat

    mat基础教程: http://blog.csdn.net/sinat_31802439/article/details/50083291 mat 初始化: Mat M(,,CV_32FC1); Ma ...

  5. OpenCV Mat数据类型指针ptr的使用

    OpenCV Mat数据类型指针ptr的使用 cv::Mat image = cv::Mat(400, 600, CV_8UC1); //宽400,长600 uchar * data00 = imag ...

  6. Qt QImage与OpenCV Mat转换

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51029382 应一个朋友的要求,整理总 ...

  7. 快速遍历OpenCV Mat图像数据的多种方法和性能分析 | opencv mat for loop

    本文首发于个人博客https://kezunlin.me/post/61d55ab4/,欢迎阅读! opencv mat for loop Series Part 1: compile opencv ...

  8. 【转】OpenCV Mat数据结构

    转载自xiahouzuoxin原文 OpenCV基础篇之Mat数据结构 程序及分析 /* * FileName : MatObj.cpp * Author : xiahouzuoxin @163.co ...

  9. opencv Mat 像素操作

    1 cv::Mat cv::Mat是一个n维矩阵类,声明在<opencv2/core/core.hpp>中.   class CV_EXPORTS Mat { public: //a lo ...

随机推荐

  1. Netty(三):线程模型

    Netty中支持单线程模型,多线程模型,主从多线程模型. 1 单线程模型 在ServerBootstrap调用方法group的时候,传递的参数是同一个线程组,且在构造线程组的时候,构造参数为1,这种开 ...

  2. Content Security Policy

    资料来源:阮一峰博客 一.背景 XSS最常见,危害最大的网页安全漏洞,“网页安全政策”从根本上解决问题 二.简介 CSP的实质是白名单制度,明确告诉客户端那些外部资源可以加载和执行. CSP 大大增强 ...

  3. jsp获取web.xml 里的配置项

    ServletContext servletContext = request.getSession().getServletContext();                String titl ...

  4. HandlerMethodArgumentResolver 参数解析器

    关于springMvc中的参数解析器 springMvc中的HandlerAdapter会检测所有的 HandlerMethodArgumentResolver(对参数的解析器) HandlerMet ...

  5. 玩转 eclipse:[2]代码重构

    Java 程序重构的目标就是进行全系统程序代码变更, 使得工程更符合常用设计思想,它不但不会影响程序的行为 ,反而使程序的结构更为清晰合理. Eclipse 提供一系列非常高效并且有易于重构程序代码的 ...

  6. Android锁屏状态下弹出activity,如新版qq的锁屏消息提示

    在接收消息广播的onReceive里,跳转到你要显示的界面.如: Intent intent = new Intent(arg0,MainActivity.class); intent.addFlag ...

  7. 说明sizeof和strlen之间的区别。

    解析:由以下几个例子我们说明sizeof和strlen之间的区别.第1个例子: sizeof(ss)结果为4,ss是指向字符串常量的字符指针.sizeof(*ss)结果为1,*ss是第一个字符.第2个 ...

  8. NVR硬件录像机web无插件播放方案功能实现之相关接口注意事项说明

    该篇博文主要用来说明EasyNVR硬件录像回放版本的相关接口说明和调用的demo: 方便用户的二次开发和集成. 软件根目录会包含接口文档的,因此,本文主要是对一些特定接口的说明和接口实现功能的讲解以及 ...

  9. EasyPlayerPro Windows播放器本地快照抓拍截图功能实现方法

    背景描述 作为一个播放器,截图功能必不可少; 下面主要记录一下截图功能的实现: 实现流程 将解码后的帧进行格式转换(目标格式为RGB24); 采用独立的线程进行截图处理; 截图可保存为BMP或JPG两 ...

  10. Vue设置导航栏为公共模块并在登录页不显示

    1.公共模块的内容可以放在App.vue中但是通常登录页面是不需要导航的,那么就需要规避登录页这时,就可以采用keep-alive结合$route.meta来实现这个功能.keep-alive 是 V ...