转载:https://blog.csdn.net/weixin_41767419/article/details/116204595
创建高维数组
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(){ int p = 1; int q = 2; int t = 3; int u = 4; int sizes[] = {p,q,t,u}; int all = p*q*t*u; float *d1 = new float[all]; for(int i = 0; i < all; i++) { d1[i] = i * 1.0f; } Mat a = Mat(4, sizes, CV_32S, d1); int n, c, h, w, id; for (n = 0; n<p; n++){ for (c=0; c<q; c++){ for (h=0; h<t; h++){ for (w=0; w<u; w++){ id = a.step[0] * n + a.step[1] * c + a.step[2] * h + w * a.step[3]; //cout << id << endl; float *p = (float*)(a.data + id); cout << *p << endl; } } } }
int dim = a.dims; // =4
int n = a.size[0]; // =p
int c = a.size[1]; // =q
int h = a.size[2]; // =t
int w = a.size[3]; // =u
cout << "a.step[0] = " << a.step[0] << endl; cout << "a.step.p[0] = " << a.step.p[0] << endl; // 两者结果相同,都是96,意思为0维,一行有多少字节 cout << "a.step[1] = " << a.step[1] << endl; cout << "a.step.p[1] = " << a.step.p[1] << endl; cout << "a.step[2] = " << a.step[2] << endl; cout << "a.step[3] = " << a.step[3] << endl; cout << "a.size[0] = " << a.size[0] << endl; // 1 cout << "a.size[1] = " << a.size[1] << endl; // 2 cout << "a.size[2] = " << a.size[2] << endl; // 3 cout << "a.size[3] = " << a.size[3] << endl; // 4 cout << "a.cols = " << a.cols << endl; // 二维以上cols和rows都是-1 cout << "a.rows = " << a.rows << endl; return 0; }
创建高维图片数组
这样就可以引入多batch的图片
int main(){ int p = 1; int q = 2; int t = 2; // int u = 3; int sizes[] = {p,q,t}; // 3维 int all = 3 * p*q*t; // 这里 ×3, 因为每个元素是CV_8UC3 unsigned char* d1 = new unsigned char[all]; for(int i = 0; i < all; i++) { d1[i] = i; cout << (int)d1[i] << i << endl; } Mat a = Mat(3, sizes, CV_8UC3, d1); // 这里是3维,因为每个元素都是CV_8UC3, 是3个数 cout << "a.step[0] = " << a.step[0] << endl; cout << "a.step.p[0] = " << a.step.p[0] << endl; cout << "a.step[1] = " << a.step[1] << endl; cout << "a.step.p[1] = " << a.step.p[1] << endl; cout << "a.step[2] = " << a.step[2] << endl; // cout << "a.step[3] = " << a.step[3] << endl; cout << "a.size[0] = " << a.size[0] << endl; cout << "a.size[1] = " << a.size[1] << endl; cout << "a.size[2] = " << a.size[2] << endl; // cout << "a.size[3] = " << a.size[3] << endl; // cout << "a.cols = " << a.cols << endl; // cout << "a.rows = " << a.rows << endl; cout << "a.at<Vec3b>(0,1,1) = " << a.at<Vec3b>(0,1,1) << endl; // 这里输出为 [9, 10, 11] return 0; }
从现有mat, 建立子mat
int main(){ float A[4][3] = { 0 }; A[0][0] = 1.0f; A[1][0] = 2.0f; A[2][0] = 3.0f; A[3][0] = 4.0f; Mat A_mat = Mat(4, 3, CV_32F, A); Mat row1(1, 3, CV_32F, A_mat.ptr<float>(1));//获取第二行首地址 Mat row1 = Mat(1, 3, CV_32F, A_mat.ptr<float>(1)); // 这两种初始化都可以 auto p = A_mat.data; cout << (float)*p << endl; // 0 cout << row1.at<float>(0, 0) << endl; // 2 return 0; }
标签:size,mat,int,opencv,数组,d1,CV,高维,Mat From: https://www.cnblogs.com/hansjorn/p/17393449.html