如何将Mat(CV_32FC1)复制到vector *

时间:2021-11-22 21:42:10

I want to copy a mat data to vector .

我想将mat数据复制到vector。

So, now I have to that to copy mat data to "vector* Vf2"

所以,现在我必须将mat数据复制到“vector * Vf2”

And I wrote this code.

我写了这段代码。


cv::Mat M=Mat(480,480,CV_32FC1,「the data ....」);   

//copy vector to mat   

vector< float> *Vf2;   

//copy mat to vector   

Vf2->assign((float*)M.datastart, (float*)M.dataend);  

But, It fell without error while assign method.

但是,在分配方法时它没有错误地下降。

And if vector isn't pointer.

如果vector不是指针。

So this below code success.

所以这下面的代码成功了。


 cv::Mat M=Mat(480,480,CV_32FC1,「the data ....」);   

 //copy vector to mat   

 vector< float> Vf2;   

 //copy mat to vector   

 Vf2.assign((float*)M.datastart, (float*)M.dataend);   

How to copy mat to vector<float>* Vf2

如何将mat复制到vector * Vf2

Tell me someone

告诉我一个人

sorry actuary I want to do is that copy the mat data to shared memory. and I wrote like this code.

对不起精算师我想做的是将mat数据复制到共享内存。我写的就像这段代码。

managed_shared_memory shmd(create_only, DEPTHNAME, WIDTH_PIC * HEIGHT_PIC * 4 + 1024);

std::vector<float> *ptrd=shmd.construct< std::vector<float> >("DepthImage")(); mxd->lock(); 

ptrd->assign((float*)decodedDepthData2.datastart,(float*)decodedDepthData2.dataend);

mxd->unlock();

By why, I want to copy the mat data to vector*

为什么,我想将mat数据复制到vector *

2 个解决方案

#1


0  

You defined a pointer to vector<float>. Then you accessed it without allocating it first. You need to initialize the pointer: vector<float> *Vf2 = new vector<float>; and the access the object to which it points.

您定义了一个指向vector 的指针。然后你先访问它而不先分配它。你需要初始化指针:vector * Vf2 = new vector ;并访问它指向的对象。

Don't forget to release it once you are done (delete Vf2;)! Or consider using smart pointer.

一旦完成,不要忘记释放它(删除Vf2;)!或者考虑使用智能指针。

#2


0  

You shouldn't use a pointer to a vector.

您不应该使用指向矢量的指针。

You can convert from/to vector and Mat:

你可以转换为/到vector和Mat:

#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;

int main()
{
    vector<float> v1{1, 2, 3, 4, 5};

    // vector to Mat

    Mat1f m1(v1);       // not copying data, just creating the matrix header
    Mat1f m2(v1, true); // copying data

    // Mat to vector

    vector<float> v2(m2.begin(), m2.end()); // copying data


    // If you really need a pointer to a vector, then

    vector<float>* v3 = new vector<float>(m2.begin(), m2.end());
    // ... do your stuff
    delete v3;

    return 0;
}

#1


0  

You defined a pointer to vector<float>. Then you accessed it without allocating it first. You need to initialize the pointer: vector<float> *Vf2 = new vector<float>; and the access the object to which it points.

您定义了一个指向vector 的指针。然后你先访问它而不先分配它。你需要初始化指针:vector * Vf2 = new vector ;并访问它指向的对象。

Don't forget to release it once you are done (delete Vf2;)! Or consider using smart pointer.

一旦完成,不要忘记释放它(删除Vf2;)!或者考虑使用智能指针。

#2


0  

You shouldn't use a pointer to a vector.

您不应该使用指向矢量的指针。

You can convert from/to vector and Mat:

你可以转换为/到vector和Mat:

#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;

int main()
{
    vector<float> v1{1, 2, 3, 4, 5};

    // vector to Mat

    Mat1f m1(v1);       // not copying data, just creating the matrix header
    Mat1f m2(v1, true); // copying data

    // Mat to vector

    vector<float> v2(m2.begin(), m2.end()); // copying data


    // If you really need a pointer to a vector, then

    vector<float>* v3 = new vector<float>(m2.begin(), m2.end());
    // ... do your stuff
    delete v3;

    return 0;
}