C ++ OpenCV的转垫成的一维数组 [英] c++ OpenCV Turn a Mat into a 1 Dimensional Array

查看:3648
本文介绍了C ++ OpenCV的转垫成的一维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的

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

从发生在:

float testDataFloat[386][2];

但我无法弄清楚如何把它变成一个1维数组。

But I can't figure out how to turn it into a 1 Dimensional Array.

任何帮助吗?

推荐答案

样本包括:


  1. 直接法从浮二维数组转换为浮动一维数组。

  2. 的方式来创建一个CV ::垫从2D float数组

  3. 的方式来创建从2D CV ::垫有没有填充(例如步长=单行的大小)
  4. 1D float数组
  1. direct method to convert from float 2d array to float 1d array.
  2. way to create a cv::Mat from 2D float array
  3. way to create 1D float array from a 2D cv::Mat that has no padding (e.g. stepsize = size of a single row)

这一个作品,对我说:

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的转垫成的一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆