Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;OpenCV Mat的动态二维阵列_C++_Opencv - Fatal编程技术网

C++ C++;OpenCV Mat的动态二维阵列

C++ C++;OpenCV Mat的动态二维阵列,c++,opencv,C++,Opencv,我参考了这篇文章: 它适用于普通二维阵列 但是,如果我使用动态2D数组作为输入,它将失败 int total_row = 2; int total_col = 4; unsigned short **my_arr = NULL; my_arr = new unsigned short*[total_row]; //allocate memory for each 1D array for (int i = 0; i < total_row; i++) { my_arr[i] =

我参考了这篇文章:

它适用于普通二维阵列

但是,如果我使用动态2D数组作为输入,它将失败

int total_row = 2;
int total_col = 4;
unsigned short **my_arr = NULL;
my_arr = new unsigned short*[total_row];

//allocate memory for each 1D array
for (int i = 0; i < total_row; i++)
{
    my_arr[i] = new unsigned short[total_col];
}

//Set value into 2D array
int count = 0;
for (int i = 0; i < total_row; i++)
{
    for (int j = 0; j < total_col; j++)
    {
        my_arr[i][j] = count;
        count++;
    }
}

for (int i = 0; i < total_row; i++)
{
    for (int j = 0; j < total_col; j++)
    {
        cout << my_arr[i][j] << " ";
    }
    cout << endl;
}

//put that into Mat
Mat mat(total_row, total_col, CV_16UC1, my_arr);

for (int i = 0; i < mat.rows; i++)
{
    for (int j = 0; j < mat.cols; j++)
    {
        cout << mat.at<ushort>(i, j) << " ";
    }
    cout << endl;
}
int total_row=2;
总积分=4;
无符号短**my\u arr=NULL;
my_arr=新的无符号短*[总计行];
//为每个1D阵列分配内存
对于(int i=0;i谢谢尼古拉斯·贝特沃斯!
如果使用1D动态数组,它就工作了

unsigned short *my_arr = NULL;
my_arr = new unsigned short[total_row*total_col];
for (int i = 0; i < (total_row*total_col); i++)
{
    *(my_arr + i) = count;
    count++;
}

Mat mat(total_row, total_col, CV_16UC1, my_arr);
unsigned short*my_arr=NULL;
my_arr=新的无符号短[行总数*列总数];
对于(int i=0;i<(行总数*列总数);i++)
{
*(my_arr+i)=计数;
计数++;
}
垫(总行、总列、CV\U 16UC1、我的arr);

感谢尼古拉斯·贝特沃斯! 如果使用1D动态数组,它就工作了

unsigned short *my_arr = NULL;
my_arr = new unsigned short[total_row*total_col];
for (int i = 0; i < (total_row*total_col); i++)
{
    *(my_arr + i) = count;
    count++;
}

Mat mat(total_row, total_col, CV_16UC1, my_arr);
unsigned short*my_arr=NULL;
my_arr=新的无符号短[行总数*列总数];
对于(int i=0;i<(行总数*列总数);i++)
{
*(my_arr+i)=计数;
计数++;
}
垫(总行、总列、CV\U 16UC1、我的arr);

我想OpenCV需要一个指向内存顺序分区的指针。请参阅以获取解决方案。@NicholasBetsworth这就是我需要的!非常感谢!!我想OpenCV需要一个指向内存顺序分区的指针。请参阅以获取解决方案。@NicholasBetsworth这就是我需要的!非常感谢!!