一、Mat对象的发展史
在早期的OpenCV1.x版本中,图像的处理是通过IplImage(该名称源于Intel的另一个开源库Intel Image Processing Library ,缩写成IplImage)结构来实现的。早期的OpenCV是用C语言编写,因此提供的借口也是C语言接口,其源代码完全是C的编程风格。IplImage结构是OpenCV矩阵运算的基本数据结构。
到OpenCV2.x版本,OpenCV开源库引入了面向对象编程思想,大量源代码用C++重写,Mat类 (Matrix的缩写) 是OpenCV用于处理图像而引入的一个封装类。从功能上讲,Mat类在IplImage结构的基础上进一步增强,并且,由于引入C++高级编程特性,Mat类的扩展性大大提高,Mat类的内容在后期的版本中不断丰富,如果你查看Mat类的定义的话
该对象会自动分配内存、自动管理内存分配和释放,不存在内存泄漏的问题,OpenCV2.x版本以后,越来越多的函数实现了MatLab所具有的功能,甚至干脆连函数名都一模一样(如 imread, imshow,imwriter等), 这些属性和方法如果你学过Matlab掌握起来一定会很快。接下来介绍一下Mat对象的一些重要的属性和方法。
二、Mat对象的构造函数
Mat的构造函数
Mat(); // 默认构造函数
Mat(int rows, int cols, int type); //指定行数、列数、类型的构造函数
Mat(Size size, int type); // 指定矩阵大小、类型的构造函数
Mat(int rows, int cols, int type, const Scalar& s); // 指定矩阵行列、类型、颜色的构造函数
Mat(Size size, int type, const Scalar& s);// 指定矩阵大小、类型、颜色的构造函数
Mat(int ndims, const int* sizes, int type);
Mat(int ndims, const int* sizes, int type, const Scalar& s);
Mat(const Mat& m);
Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);
Mat(Size size, int type, void* data, size_t step=AUTO_STEP);
Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0);
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
const char *INPUT_WIN = "input";
const char *OUTPUT_WIN = "output";
int main(int argc, char **argv){
Mat src, dst;
src = imread("3.png");
if (!src.data){
cout << "can not load image" <<