Converting a std::vector<float> to mxArray using ocvMxArrayFromVector

12 views (last 30 days)
Hello,
I'm currently working with mex matlab and OpenCV libraries. I'm using the results of the computation of HOG features, which is of type std::vector<float>, and need it as an output of the mex function.
I'm therefore trying to use the ocvMxArrayFromVector function, which is supposed to conver a vector of a given type to the required mxArray data type output.
The part of code I'm using is:
vector<float> descriptorValues(outDim);
hog->compute(croppedImg, descriptorValues, Size(0,0), Size(0,0), locations);
plhs[0] = ocvMxArrayFromVector(descriptorValues);
But when compiling it returns the following error:
Error using mexOpenCV (line 122)
/tmp/mex_1394440023880_2588/HOGDescriptorOCV.o: In function `computeHOGFeatures(int, mxArray_tag**, mxArray_tag
const**)':
HOGDescriptorOCV.cpp:(.text+0xb24): undefined reference to `ocvMxArrayFromVector(std::vector<float,
std::allocator<float> > const&)'
collect2: error: ld returned 1 exit status
I'm compiling using g++ on an Ubuntu, matlab version R2016b.
I would be very thankful for your help.
David

Answers (2)

James Tursa
James Tursa on 28 Sep 2016
I don't know about the ocvMxArrayFromVector routine, or how to advise you to link everything in properly, but here is a short example that takes a C++ vector of floats and copies it into an mxArray for output:
#include <vector>
using namespace std;
#include <string.h>
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int i, n=5;
vector<float> descriptorValues(n);
// Load up some sample data
for( i=0; i<n; i++ ) {
descriptorValues[i] = (float) (i + 1);
}
// Pretend we don't know the size and get it dynamically
n = descriptorValues.size();
// Create the output mxArray and copy the data
plhs[0] = mxCreateNumericMatrix( 1, n, mxSINGLE_CLASS, mxREAL );
memcpy( mxGetData(plhs[0]), &descriptorValues[0], n*sizeof(float) );
}

xingxingcui
xingxingcui on 27 Mar 2019
Edited: xingxingcui on 27 Mar 2019

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!