How to Pass Mat Data (or Vector Data) to Shared Memory

Asked 1 years ago, Updated 1 years ago, 110 views

I am having trouble knowing how to pass OpenCV Mat data or Vector data to shared memory.
I may not understand how to use the pointer, but I would appreciate it if you could give me some advice.

I wanted to use boost/interprocess/managed_shared_memory to save Mat's data on shared memory and pass it to another process, so when I gave it to you as below, it fell in the assign.
There were no errors at all.

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

interprocess_mutex*mxd=shmd.construction<interprocess_mutex>("TheMutex")();

std::vector<float>*ptrd=shmd.construction<std::vector<float>>("DepthImage")(); 
// Lock
mxd->lock(); 
// Write to shared memory
ptrd->assign(float*)decodedDepthData2.datastart,(float*)decodedDepthData2.dataend);
// Unlocked
mxd->unlock();

c cv:: Mat decodedDepthData is the depth image of the CV_32FC1 side: WIDTH_PIC vertical: HEIGHT_PIC.

If you don't make it into a pointer type, you can copy it.

You can enter a value for a vector type variable as follows:

 cv::Mat=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);   

However, if you try to copy to the pointer type vector variable as shown below,
Just like the shared memory I wrote at the beginning, it falls off without any errors in the middle of the assignment.

 cv::Mat=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 can I pass Mat's data to shared memory?

Please give me some advice.

c++ opencv pointer boost stl

2022-09-30 20:56

2 Answers

std::vector<float>, but the default template argument is exactly std::vector<float,std::allocator<float>>.
This means that vector management space (such as size information) is reserved in shared memory, but real data (float array) is not shared memory because it is reserved from std::allocator.

You must specify Allocator provided by Boost.Interprocess.I use boost/interprocess/containers/vector.hpp, but I have a sample of Creating vectors in shared memory.

typedef allocator<float, managed_shared_memory::segment_manager>ShmemAllocator;
typeedef vector <float, ShmemAllocator > MyVector;

const ShmemAllocator alloc_inst(shmd.get_segment_manager());
MyVector*ptrd=shmd.construct<MyVector>("DepthImage")(alloc_inst);


2022-09-30 20:56

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

Even an uninitialized pointer is of no use, so it's only natural that it falls.

 std::vector<float>*ptrd=shmd.construction<std::vector<float>>("DepthImage")(); 

You just created a vector<float> body on the shared memory.
In other words, vector<float>v; is the situation in which only vector administrative information is created.
What you want to keep in the shared memory is not only vector but also vector.
vector standard allocator allocates content to normal memory, so it behaves differently.
You need vector with an allocator that allocates content to shared memory.

Now that I've written so far, there's already an appropriate answer...
Please read @sayuri's comments.


2022-09-30 20:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.