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:

Mat color;
...
Mat gray(color.rows, color.cols, color.depth()); --没必要的,因为cvtColor 会帮我们先调用一个create方法。
cvtColor(color, gray, CV_BGR2GRAY);
you can simply write:
Mat color;
...
Mat gray;
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. HTML5 手机网页制作笔记

    http://top.css88.com/archives/546 http://www.w3cfuns.com/blog-5470280-5406828.html 最近在卓手机网页,第一次入手.把要 ...

  2. Android_程序未处理异常的捕获与处理

    1.简单介绍 对于程序抛出的未被捕获的异常,可能会导致程序异常退出,界面不友好且应记录关键错误信息上传至server. 这里主要使用UncaughtExceptionHandler 2.代码实现 pu ...

  3. java wait 和notify的用法

    package com.test; public class OutputThread implements Runnable { private int num; private Object lo ...

  4. CentOS下安装python3.x版本

    现在python都到了3.x版本,但是centos中自带的python仍然是2.7版本的,所以想把python换成3.x版本的. 但是这个地方有个坑,你要是直接编译安装了python3.x之后,估计你 ...

  5. 安装Redis图形监控工具---RedisLive

    RedisLive简介 RedisLive是一款用Python编写基于WEB的Redis图形监控工具,也是一款实时监控Redis数据的开源软件,以WEB的形式展现出redis中的key的情况,实例数据 ...

  6. JS常用方法手记

    1.判断arr数组是否含有元素str,没有返回-1 arr.indexOf(str) 2.遍历arr数组,k为键,v为值 arr.map((v, k) => { return;}) 3.arr数 ...

  7. 【Android开发-5】界面装修,五大布局你选谁

    前言:假设要开一家店,门店装修是非常重要的事情.有钱都请专门的建筑设计公司来设计装修,没钱的仅仅能自己瞎折腾.好不好看全凭自己的感觉.像Android开发.在移动端大家看到的界面视觉不咋滴,一般连打开 ...

  8. tomcat+java 占cpu 调试【top命令应用】

    原文出处:http://www.blogjava.net/hankchen 现象: 在tomcat中部署java的web应用程序,过一段时间后出现tomcat的java进程持续占用cpu高达100%, ...

  9. 内存MCE错误导致暴力扩充messages日志 以及chattr记录

    由于放假,好久没登过服务器,今天登上服务器查看日志意外发现:/var/log/messages文件竟然被撑到20多个G!!!赶紧查看是什么情况,首先,20多个G的文件根本无法查看,因此,我想到了spl ...

  10. BCH分叉是一次站队博弈

    BCH分叉在即,很多人说BCH本次分叉实质是大佬间的斗争,主要是本次BCH分叉主要分为两大派别: 一派以BCH用户量最大的客户端Bitcoin ABC开发组为主,要在11月15日展开硬分叉升级,主要升 ...