OpenCV中Mat的尺寸信息:高、宽、通道数、维度
float* Idata = new float[480 * 640 * 3];
Mat I(480, 640, CV_32FC3, Idata);
cout << "rows: " << I.rows << endl; // 480
cout << "cols: " << I.cols << endl; // 640
cout << "channels: " << I.channels() << endl; // 3 C3=3通道
cout << "size: " << I.size << endl; // 480x640
cout << "width: " << I.size().width << endl; // 640
cout << "height: " << I.size().height << endl; // 480
cout << "dims: " << I.dims << endl; // 2 C1和C3 dims维度都是2 矩阵的维度
cout << "depth: " << I.depth() << endl; // 5 与type有关 8U=0 8S=1 16U=2 16S=3 32S=4 32F=5 64F=6
uchar arr[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};
Mat src(2, 3, CV_8UC3, arr);
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
for (int c = 0; c < src.channels(); c++)
{
cout << static_cast(src.at(i, j)[c]) << endl;
}
}
}
// cout 0,1,2,3,...,17
uchar arr2[] = { 0,1,2,3,4,5 };
Mat img(2, 3, CV_8UC1, arr2);
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
cout << static_cast(img.at(i, j)) << endl;
}
}
// cout 0,1,2,...,5
OpenCV中Mat指定位置的元素值(分为单通道和三通道)
#include
#include
using namespace std;
using namespace cv;
int main()
{
cout << "Hello!" << endl;
Mat src(2, 3, CV_8UC3);
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
for (int c = 0; c < src.channels(); c++)
{
src.at(i, j)[c] = i * src.cols + j + c * src.cols * src.rows; // i行j列c通道
}
}
}
cout << static_cast(src.at(1, 0)[2]) << endl; // 打印第2行第1列第3通道元素值
Mat img(2, 3, CV_8UC1);
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
img.at(i, j) = i * img.cols + j; // i行j列
}
}
cout << static_cast(img.at(1, 0)) << endl; // 打印第2行第1列元素值
Mat im(2, 3, CV_8UC3);
Mat_::iterator it = im.begin(), itEnd = im.end(); // 遍历所有位置 rows*cols
for (; it != itEnd; ++it)
{
(*it)[0] = 2; // 第1个通道
(*it)[1] = 25; // 第2个通道
(*it)[2] = 255; // 第3个通道
}
cout << static_cast(im.at(1, 0)[2]) << endl; // 打印第2行第1列第3通道元素值
return 0;
}
OpenCV中矩阵Mat操作
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
// 创造矩阵
Mat image(240, 320, CV_8UC3); // Create a matrix
image.create(480, 640, CV_8UC3); // (re)allocate a pre-declared matrix
// 创造带常数的矩阵
Mat A33(3, 3, CV_32F, Scalar(5));
Mat B33(3, 3, CV_32F); B33 = Scalar(5);
Mat C33 = Mat::ones(3, 3, CV_32F) * 5;
Mat D33 = Mat::zeros(3, 3, CV_32F) + 5.;
// 创造带指定元素值的矩阵
double a = CV_PI / 3;
Mat A22 = (Mat_(2, 2) << cos(a), -sin(a), sin(a), cos(a));
float B22data[] = { cos(a), -sin(a), sin(a), cos(a) };
Mat B22 = Mat(2, 2, CV_32F, B22data).clone();
// 初始化随机矩阵
randu(image, Scalar(0), Scalar(256)); // uniform dist
randn(image, Scalar(128), Scalar(10)); // Gaussian dist
// 指针转Mat
float* Idata = new float[480 * 640 * 3];
Mat I(480, 640, CV_32FC3, Idata);
// vector转Mat
vector iptvec(10);
Mat iP(iptvec); // iP – 10x1 CV_32SC2
// 修改元素值 A33 dyImage image
A33.at(1, 2) = A33.at(2, 1) + 1; // A33 CV_32F
Mat dyImage(image.size(), image.type()); // image CV_8UC3
for (int y = 1; y < image.rows - 1; y++) // row
{
Vec3b* prevRow = image.ptr(y - 1); // Vec3b相当于长度为3类型为uchar的vector
Vec3b* nextRow = image.ptr(y + 1); // ptr可以获得指向每一行首地址的指针
for (int x = 0; x < image.cols; x++) // col
for (int c = 0; c < 3; c++) // channel
dyImage.at(y, x)[c] = saturate_cast(nextRow[x][c] - prevRow[x][c]); // 防止溢出
}
Mat_::iterator it = image.begin(), itEnd = image.end(); // 遍历所有位置 rows*cols
for (; it != itEnd; ++it)
{
(*it)[1] ^= 255; // 第二个通道做处理
}
return 0;
}