初识 OpenCV 之Mat
初始opencv 第二场开篇。。。。。。
入门必看?
反正我是在看了
Mat对象OpenCV2.0之后引进的图像数据结构、自动分配内存、不存在内存泄漏的问题,是面向对象的数据结构。分了两个部分,头部与数据部分
IplImage是从2001年OpenCV发布之后就一直存在,是C语言风格的数据结构,需要开发者自己分配与管理内存,对大的程序使用它容易导致内存泄漏问题
openCV的基础知识
Mat对象的要点:
1 输出图像的内存是自动分配的
2 使用OpenCV的C++接口,不需要考虑内存分配问题 赋值操作和拷贝构造函数只会复制头部分
3 使用clone与copyTo两个函数实现数据完全复制
部分复制:一般情况下只会复制Mat对象的头和指针部分,不会复制数据部分 Mat A= imread(imgFilePath); Mat B(A)
完全复制:如果想把Mat对象的头部和数据部分一起复制,可以通过如下两个API实现Mat clone() 和void copyTo(Mat mat)
Mat 对象创建
1 Mat image = imread("D:/test.jpg", IMREAD_GRAYSCALE);
2 Mat M; M.create(3,3, 3, CV_8UC3);
3 Mat M(3, 3, CV_8UC3, Scalar(0, 1, 0));
4 Mat C = (Mat_<double>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
5 Mat array1 = Mat::eye(3, 3, CV_64F)
Mat array2 = Mat::ones(3,3 , CV_32F);
Mat array3 = Mat::zeros(3, 3, CV_8UC1)
6 Mat m4 = Mat(C.size(), C.type());
7 Mat m4 = Mat(Mat);
下面列举一下在“opencv2/core/ma.hpp“的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);
Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());
Mat(const Mat& m, const Rect& roi);
Mat(const Mat& m, const Range* ranges);
CV_EXPORTS_W void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 );
cvtColor用于颜色空间的转换 这个可以打印出来看看各个图片之间的差别
cvtColor(src,src1,CV_RGB2HLS);
cvtColor(src, src2, CV_RGB2HSV);
cvtColor(src, src3, CV_RGB2BGR);
cvtColor(src, src4, CV_RGB2YCrCb);
cvtColor(src, src5, CV_RGB2YUV);
cvtColor(src, src6, CV_RGB2HLS_FULL);
imshow("HLS", src1);
imshow("HLSFULL", src6);
imshow("HSV", src2);
imshow("BGR", src3);
imshow("YCrCb", src4);
imshow("YUV", src5);
resize(frame,frame_temp,Size(width,height)); 改变图像大小
void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;
/** @brief Provides a functional form of convertTo.
This is an internally used method called by the @ref MatrixExpressions engine.
@param m Destination array.
@param type Desired destination array depth (or -1 if it should be the same as the source type).
*/
int channels() 获取通道数
int row() 获取行数
int col()获取列数
int depth() 获取深度
bool empty();判断是否为空
一个个自己试一下会比较好玩