简体   繁体   中英

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

I want to copy a mat data to vector .

So, now I have to that to copy mat data to "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.

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

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> . 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.

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

You shouldn't use a pointer to a vector .

You can convert from/to vector and 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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