gpt4 book ai didi

c++ OpenCV将垫子变成一维数组

转载 作者:行者123 更新时间:2023-11-28 06:29:01 30 4
gpt4 key购买 nike

我有这个Mat:

Mat testDataMat(386, 2, CV_32FC1, testDataFloat);

取自:

float testDataFloat[386][2];

但我不知道如何将它变成一维数组。

有什么帮助吗?

最佳答案

示例包括:

  1. 将浮点二维数组转换为浮点一维数组的直接方法。
  2. 从二维 float 组创建 cv::Mat 的方法
  3. 从没有填充的二维 cv::Mat 创建一维 float 组的方法(例如,步长 = 单行的大小)

这个对我有用:

int main()
{
const int width = 2;
const int height = 386;
float testDataFloat[height][width];

// create/initialize testdata
for(unsigned int j=0; j<height; ++j)
for(unsigned int i=0; i<width; ++i)
{
if(j%5 == 0)
testDataFloat[j][i] = 0.0f;
else
testDataFloat[j][i] = 1.0f;
}

// -----------------------------------------------------------
// Direct convert from 2D array to 1D array:
float * testData1DDirect = (float*)testDataFloat;



// -----------------------------------------------------------
// create Mat with 2D array as input:
cv::Mat testDataMat(height, width, CV_32FC1, testDataFloat);

// convert from Mat to 1D array
// this works only if there is no padding in the matrix.
float * testData1D = (float*)testDataMat.data;


// test whether the arrays are correct
for(unsigned int i=0; i<width*height; ++i)
{
if(testData1D[i] != testData1DDirect[i])
std::cout << "ERROR at position: " << i << std::endl;
}

// output the Mat as an image:
cv::imshow("test", testDataMat);
cv::waitKey(0);

}

关于c++ OpenCV将垫子变成一维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27981571/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com