Mat对象
Mat对象与IplImage对象
Mat对象OpenCV2.0之后引进的图像数据结构、自动分配内存、不存在内存泄漏的问题,是面向对象的数据结构,分了两部分,头部和数据部分。
IplImage是从2001年OpenCV发布之后就一直存在,是C语言风格的数据结构,需要开发者自己分配与管理内存,对大的程序使用它容易导致内存泄漏问题。
//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)
//
// 常用方法:
// void copyTo(Mat mat)
// void convertTo(Mat dst,int type)
// Mat clone()
// int channels()
// int depth()
// bool empty()
// uchar* ptr(i=0)
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace std;
using namespace cv;
int main(int argc,char** argv){
Mat src;
src = imread("");
if(src.empty()){
cout<<"could not load image..."<<endl;
return -1;
}
namedWindow("input",CV_WINDOW_AUTOSIZE);
imshow("input",src);
/*
Mat dst;
dst = Mat(src.size(),src.type());
dst = Scalar(127,0,255);//给图像赋值
namedWindow("output",CV_WINDOW_AUTOSIZE);
imshow("output",dst);
*/
Mat dst = src.clone();//克隆
src.copyTo(dst);//复制
cvtColor(src,dst,CV_GRAY2GRAY);
src.channels();//多少通道,函数返回是一个值
const uchar* firstRow = dst.ptr<uchar>(0);
cout<<"first pixel value:"<<*firstRow&