1.Mat的创建
1.1 初始化创建
cv::Mat mat(3000, 4000, CV_8UC3);
//参数一:rows,行
//参数二:cols,列
//参数三:类型
1.2 create创建
cv::Mat mat;
mat.create(3000, 4000, CV_8UC3);
2.Mat的遍历
首先我们需要知道Mat的存储分为两种:
(1) 连续存储
(2) 非连续存储
判断方式:
mat.isContinuous();
2.1 连续空间直接像素访问
2.1.1 方式1
int size = mat.rows * mat.cols * mat.elemSize;
for (int i=0; i<size; i += mat.elemSize)
{
mat.data[i] = 255; //B
mat.data[i+1] = 255; //G
mat.data[i+2] = 255; //R
}
2.1.2 方式2
for (int row=0; row<mat.rows; row += mat.step)
{
for (int col=0; col<mat.cols; col += 1)
{
(&mat.data[row])[col] = 255; //B
(&mat.data[row])[col+1] = 255; //G
(&mat.data[row])[col+2] = 255; //R
}
}
2.2 非连续空间
优势:相对于直接访问像素,使用函数接口的方式可以在程序错误时使用try-cath捕获cv::Exception异常
2.2.1 at接口函数访问(返回一个像素类型的引用)
for (int row=0; row<mat.rows; row += 1)
{
for (int col=0; col<mat.cols; col += 1)
{
Vec3b &pxVec = mat.at<Vec3b>(row, col);
pxVec[0] = 255; //B
pxVec[1] = 255; //G
pxVec[2] = 255; //R
}
}
2.2.2 ptr接口函数访问(返回一个像素的指针)
for (int row=0; row<mat.rows; row += 1)
{
for (int col=0; col<mat.cols; col += 1)
{
Vec3b *pxVec = mat.ptr<Vec3b>(row, col);
(*pxVec)[0] = 255; //B
(*pxVec)[1] = 255; //G
(*pxVec)[2] = 255; //R
}
}
2.2.3 迭代器方式访问
Vec3b::iterator *it = mat.begin<Vec3b>();
Vec3b::iterator *end = mat.end<Vec3b>();
for (;it != end; ++it)
{
(*it)[0] = 255; //B
(*it)[1] = 255; //G
(*it)[2] = 255; //R
}