CV_INLine CvMat cvMat((introws, intcols, inttype, void* data CV_DEFAULT);
CvMat * cvInitMatHeader(CvMat * mat, introws, intcols, inttype, void* data CV_DEFAULT(NULL), intstep CV_DEFAULT(CV_AUTOSTEP));
对矩阵数据进行访问:
cvmGet( constCvMat* mat, introw, intcol );
CvScalar cvGet2D(constCvArr * arr, intidx0, intidx1); //CvArr只作为函数的形参void cvSet2D(CvArr* arr, int idx0, int idx1, CvScalar value);
CvMat * cvmat = cvCreateMat(4, 4, CV_32FC1);
cvmat->data.fl[row * cvmat->cols + col] = (float)3.0;
CvMat * cvmat = cvCreateMat(4, 4, CV_64FC1);
cvmat->data.db[row * cvmat->cols + col] = 3.0;CvMat * cvmat = cvCreateMat(4, 4, CV_64FC1);
CV_MAT_ELEM(*cvmat, double, row, col) = 3.0; if(CV_MAT_DEPTH(cvmat->type) == CV_32F)
CV_MAT_ELEM_CN(*cvmat, float, row, col * CV_MAT_CN(cvmat->type) + ch) = (float)3.0; //ch为通道值
if(CV_MAT_DEPTH(cvmat->type) == CV_64F)
CV_MAT_ELEM_CN(*cvmat, double, row, col * CV_MAT_CN(cvmat->type) + ch) = 3.0; //ch为通道值
for(introw = 0; row < cvmat->rows; row++)
{
p = cvmat ->data.fl + row * (cvmat->step / 4);
for(intcol = 0; col < cvmat->cols; col++)
{
*p = (float) row + col;
*(p+1) = (float)row + col + 1;
*(p+2) = (float)row + col + 2;
p += 3;
}
}
CvMat * vector = cvCreateMat(1,3, CV_32SC2);CV_MAT_ELEM(*vector, CvPoint, 0, 0) = cvPoint(100,100);
CvMat * vector = cvCreateMat(1,3, CV_64FC4);CV_MAT_ELEM(*vector, CvScalar, 0, 0) = CvScalar(0, 0, 0, 0);
CvMat* M2;
M2=cvCloneMat(M1);
3.Mat
Mat是opencv2.0推出的处理图像的新的,现在越来越有趋势取代之前的cvMat和lplImage,相比之下Mat最大的好处就是能够更加方便的进行内存管理,不再需要员手动管理内存的释放。opencv2.3中提到Mat是一个多维的密集数据数组,可以用来处理向量和矩阵、图像、直方图等等常见的多维数据。
{
public:
intflags;(Note :目前还不知道flags做什么用的)
intdims;
introws,cols;
uchar *data;
int* refcount;
...
};
从以上结构体可以看出Mat也是一个矩阵头,默认不分配内存,只是指向一块内存(注意读写保护)。初始化使用create函数或者Mat构造函数,以下整理自opencv2.3.1 Manual:
M.create(nrows, ncols, type);例子:
Mat M(7,7,CV_32FC2,Scalar(1,3));
M.create(100, 60, CV_8UC(15));
intsz[] = {100, 100, 100};
Mat bigCube(3, sz, CV_8U, Scalar:all(0));
doublem[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}};
Mat M = Mat(3, 3, CV_64F, m).inv();
Mat img(Size(320,240),CV_8UC3);
Mat img(height, width, CV_8UC3, pixels, step);
IplImage* img = cvLoadImage("greatwave.jpg", 1);
Mat mtx(img,0); //convert IplImage* -> Mat;
M.row(3) = M.row(3) + M.row(5) * 3;
Mat M1 = M.col(1);
M.col(7).copyTo(M1);
Mat M;
M.at<double>(i,j);
M.at(uchar)(i,j);
Vec3i bgr1 = M.at(Vec3b)(i,j)
Vec3s bgr2 = M.at(Vec3s)(i,j)
Vec3w bgr3 = M.at(Vec3w)(i,j)
doublesum = 0.0f;
for(introw = 0; row < M.rows; row++)
{
constdouble* Mi = M.ptr<double>(row);
for(intcol = 0; col < M.cols; col++)
sum += std::max(Mi[j], 0.);
}
doublesum=0;
MatConstIterator<double> it = M.begin<double>(), it_end = M.end<double>();
for(; it != it_end; ++it)
sum += std::max(*it, 0.);
imshow(conststringframeName, InputArray mat); //显示图像
imwrite (conststring& filename, InputArray img); //储存图像