简体   繁体   English

如何将Mat(CV_32FC1)复制到矢量 <float> *

[英]How to copy Mat(CV_32FC1) to vector<float>*

I want to copy a mat data to vector . 我想将mat数据复制到vector。

So, now I have to that to copy mat data to "vector* Vf2" 因此,现在我必须将数据复制到“ 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<float>* 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. 我写了这样的代码。

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* 为什么,我想将垫子数据复制到矢量*

You defined a pointer to vector<float> . 您定义了一个指向 vector<float>指针 Then you accessed it without allocating it first. 然后,您无需先分配即可访问它。 You need to initialize the pointer: vector<float> *Vf2 = new vector<float>; 您需要初始化指针: vector<float> *Vf2 = new vector<float>; and the access the object to which it points. 并访问它指向的对象。

Don't forget to release it once you are done ( delete Vf2; )! 完成后不要忘记释放它( delete Vf2; )! Or consider using smart pointer . 或考虑使用智能指针

You shouldn't use a pointer to a vector . 你不应该使用指向vector的指针。

You can convert from/to vector and Mat : 您可以从/转换为vectorMat

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM